Simple function overloading in Python.
Overloads a function based on the specified argument count.
| Parameters: | argc – The argument count. Defaults to None. If None is given, automatically compute the argument count from the given function. |
|---|
Note
Keyword argument counts are NOT checked! In addition, when the argument count is automatically calculated, the keyword argument count is also ignored!
Example:
@overload.argc()
def func(a):
print 'Function 1 called'
@overload.argc()
def func(a, b):
print 'Function 2 called'
func(1) # Calls first function
func(1, 2) # Calls second function
func() # Raises error
Overload a function based on the specified argument types.
| Parameters: | argtypes – The argument types. If None is given, get the argument types from the function annotations(Python 3 only) |
|---|
Example:
@overload.args(str)
def func(s):
print 'Got string'
@overload.args(int, str)
def func(i, s):
print 'Got int and string'
@overload.args()
def func(i:int): # A function annotation example
print 'Got int'
func('s')
func(1)
func(1, 's')
func(True) # Raises error
Create a module object at runtime and insert it into sys.path. If called, same as from_objects().
Create a module at runtime from d.
| Parameters: |
|
|---|
Example: RuntimeModule.from_objects('name', 'doc', a=1, b=2)
Create a module at runtime from s`.
| Parameters: |
|
|---|
A Python switch statement implementation that is used with a with statement.
| Parameters: | value – The value to “switch”. |
|---|
with statement example:
with switch('x'):
if case(1): print 'Huh?'
if case('x'): print 'It works!!!'
Warning
If you modify a variable named “case” in the same scope that you use the with statement version, you will get an UnboundLocalError. The soluction is to use with switch('x') as case: instead of with switch('x'):.
Remove tail recursion from a function.
| Parameters: | spec – A function that, when given the arguments, returns a bool indicating whether or not to exit. If None, tail recursion is always called unless the function returns a value. |
|---|
Note
This function has a slight overhead that is noticable when using timeit. Only use it if the function has a possibility of going over the recursion limit.
Warning
This function will BREAK any code that either uses any recursion other than tail recursion or calls itself multiple times. For example, def x(): return x()+1 will fail.
Example:
@tail_recurse()
def add(a, b):
if a == 0: return b
return add(a-1, b+1)
add(10000000, 1) # Doesn't max the recursion limit.
Copies a funcion.
| Parameters: | f – The function to copy. |
|---|---|
| Returns: | The copied function. |
Deprecated since version 0.4: Use modify_function() instead.
A simple decorator to set docstrings.
| Parameters: | doc – The docstring to tie to the function. |
|---|
Example:
@set_docstring('This is a docstring')
def myfunc(x):
pass
Set function annotations using decorators.
| Parameters: |
|
|---|
Deprecated since version 0.5: Use fannotate() instead.
Safely unpack a sequence to length ln, without raising ValueError. Based on Lua’s method of unpacking. Empty values will be filled in with fill, while any extra values will be cut off.
| Parameters: |
|
|---|
Example:
s = 'a:b'
a, b = safe_unpack(s.split(':'), 2)
# a = 'a'
# b = 'b'
s = 'a'
a, b = safe_unpack(s.split(':'), 2)
# a = 'a'
# b = None
Creates a copy of a function, changing its attributes.
| Parameters: |
|
|---|
Warning
This function can be potentially dangerous.
Assign value to varname and return it. If varname is an attribute and the instance name it belongs to is not defined, a NameError is raised. This can be used to emulate assignment as an expression. For example, this:
if assign('x', 7): ...
is equilavent to this C code:
if (x = 7) ...
Warning
When assigning an attribute, the instance it belongs to MUST be declared as global prior to the assignment. Otherwise, the assignment will not work.
Set function annotations using decorators.
| Parameters: |
|
|---|
Example:
@fannotate('This for the return value', a='Parameter a', b='Parameter b')
def x(a, b):
pass