Skip to content

majidaldo/obj2sig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

obj2sig

Use Python objects to define function signatures.

Why?

To programmatically encode relationships between function arguments. Other benefits follow from treating parameters as object properties such as documentation.

How?

Use objects for encoding relationships between arguments. Use a decorator to dynamically create a function.

from obj2sig import paramize, var_property
help(paramize)

Takes an object's property attributes to be used as function arguments as follows:

  • Function argument order will match the order in which the object properties are defined.
  • Property return values are taken as default values. Ellipsis, ..., indicates no default value.
  • Property return types are used as parameter types.
  • Use the special property name 'star' to mark keyword-only arguments.
  • Use var_property to mean a variable length argument.
class Params:
    @property
    def p(self) -> int: return ...
    _star_ = ''
    @property
    def z(self) -> str: return 'z'
    @property
    def a(self): return 'a'
    @var_property
    def k(self): return ...

@paramize(Params())
def f():
    """sdfsdf"""
    return
f
functionf
sdfsdf
def f(p: int, *, z: str = 'z', a='a', **k):
class Params1:
    @var_property
    def l(self): return ...#[]

@paramize(Params1())
def f1():
    """f1"""
    return

f1
functionf1
f1
def f1(*l):

About

Use Python objects to define function signatures

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages