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)