treevalue.utils.func

args_iter

treevalue.utils.func.args_iter(*args, **kwargs)[source]
Overview:

Iterate all the arguments with index and value. If argument is in args, the index should be integer increasing from 0. If argument is in kwargs, the index should be string which meaning the argument’s name. The numeric indices will appear before the string indices, and the order of the string indices are not approved.

Arguments:
  • args (Tuple[Any]): Argument list

  • kwargs (Dict[str, Any]): Argument mapping

Example:
>>> for index, value in args_iter(1, 2, 3, a=1, b=2, c=3)):
>>>     print(index, value)

The output should be

>>> 0 1
>>> 1 2
>>> 2 3
>>> a 1
>>> b 2
>>> c 3

dynamic_call

treevalue.utils.func.dynamic_call(func: Callable)[source]
Overview:

Decorate function to dynamic-call-supported function.

Arguments:
  • func (Callable): Original function to be decorated.

Returns:
  • new_func (Callable): Decorated function.

Example:
>>> dynamic_call(lambda x, y: x ** y)(2, 3)  # 8
>>> dynamic_call(lambda x, y: x ** y)(2, 3, 4)  # 8, 3rd is ignored
>>> dynamic_call(lambda x, y, t, *args: (args, (t, x, y)))(1, 2, 3, 4, 5)  # ((4, 5), (3, 1, 2))
>>> dynamic_call(lambda x, y: (x, y))(y=2, x=1)  # (1, 2), key word supported
>>> dynamic_call(lambda x, y, **kwargs: (kwargs, x, y))(1, k=2, y=3)  # ({'k': 2}, 1, 3)

static_call

treevalue.utils.func.static_call(func: Callable, static_ok: bool = True)[source]
Overview:

Static call, anti-calculation of dynamic call.

Arguments:
  • func (Callable): Given dynamic function.

  • static_ok (bool): Allow given function to be static, default is True.

Returns:
  • static (Callable): Static function.

pre_process

treevalue.utils.func.pre_process(processor: Callable)[source]
Overview:

Pre processor for function.

Arguments:
  • processor (Callable): Pre processor.

Returns:
  • decorator (Callable): Function decorator

Example:
>>> @pre_process(lambda x, y: (-x, (x + 2) * y))
>>> def plus(a, b):
>>>     return a + b
>>>
>>> plus(1, 2)  # 5, 5 = -1 + (1 + 2) * 2

post_process

treevalue.utils.func.post_process(processor: Callable)[source]
Overview:

Post processor for function.

Arguments:
  • processor (Callable): Post processor.

Returns:
  • decorator (Callable): Function decorator

Example:
>>> @post_process(lambda x: -x)
>>> def plus(a, b):
>>>     return a + b
>>>
>>> plus(1, 2)  # -3

raising

treevalue.utils.func.raising(func: Union[Callable, BaseException, Type[BaseException]])[source]
Overview:

Decorate function with exception object return value to a raisisng function.

Arguments:
  • func (Union[Callable, BaseException, Type[BaseException]]): Not decorated function or class

Returns:
  • decorated (Callable): Decorated new function

Examples:
>>> raising(RuntimeError)()  # RuntimeError
>>> raising(lambda x: ValueError('value error - %s' % (repr(x), )))(1)  # ValueError, value error - 1

warning_

treevalue.utils.func.warning_(func: Union[Callable, Warning, Type[Warning], str])[source]
Overview:

Decorate function with exception object return value to a warning_ function.

Arguments:
  • func (Union[Callable, Warning, Type[Warning], str]): Not decorated function or class

Returns:
  • decorated (Callable): Decorated new function

Examples:
>>> warning_(RuntimeWarning)()  # RuntimeWarning
>>> raising(lambda x: Warning('value warning - %s' % (repr(x), )))(1)  # Warning, value warning - 1

freduce

treevalue.utils.func.freduce(init=<SingletonMark 'no_initial'>, pass_kwargs: bool = True)[source]
Overview:

Make binary function be reducible.

Arguments:
  • init (Any): Initial data generator or initial data, default is NO_INITIAL which means no initial data. Missing of positional arguments is forbidden.

  • pass_kwargs (bool): Pass kwargs into initial function and wrapped function or not, default is True which means pass the arguments in.

Returns:
  • decorator (Callable): Decorator for the original function.

Example:
>>> @freduce(init=0)
>>> def plus(a, b):
>>>     return a + b
>>>
>>> plus()            # 0
>>> plus(1)           # 1
>>> plus(1, 2)        # 3
>>> plus(1, 2, 3, 4)  # 10