hbutils.reflection.func

Overview:

Useful functions for processing python functions.

fassign

hbutils.reflection.func.fassign(**assigns)[source]
Overview:

Do assignments to function.

Arguments:
  • assigns: Assignment values.

Returns:
  • decorator: A decorator for assigning.

Examples::
>>> @fassign(__name__='fff')
>>> def func(a, b):
>>>     return a + b

frename

hbutils.reflection.func.frename(new_name: str)[source]
Overview:

Rename the given function.

Arguments:
  • new_name (str): New name of function.

Returns:
  • decorator: Decorator to rename the function.

Examples::
>>> @frename('fff')
>>> def func(a, b):
>>>     return a + b

fcopy

hbutils.reflection.func.fcopy(func)[source]
Overview:

Make a copy of given function.

Arguments:
  • func: Function to be copied.

Returns:
  • new_func: Copied function.

Examples::
>>> def func(a, b):
...     return a + b
>>> nfunc = fcopy(func)
>>> nfunc(1, 2)
3

args_iter

hbutils.reflection.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

sigsupply

hbutils.reflection.func.sigsupply(func, sfunc)[source]
Overview:

A solution for dynamic_call(). When func is a builtin function or method (which means its signature can not be captured by inspect.signature), the signature of sfunc will take the place, and the builtin function will be able to processed properly by function dynamic_call().

Arguments:
  • func: Original function, can be a native function or builtin function.

  • sfunc: Supplemental function, must be a native python function which has signature. Its inner logic has no importance, just provide a lambda with arguments format and None return.

Examples:

>>> dynamic_call(max)([1, 2, 3])  # no sigsupply
ValueError: no signature found for builtin <built-in function max>
>>> dynamic_call(sigsupply(max, lambda x: None))([1, 2, 3])  # use it as func(x) when builtin
3

dynamic_call

hbutils.reflection.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)

Note

Simple dynamic_call() can not support builtin functions because they do not have python signature. If you need to deal with builtin functions, you can use sigsupply() to add a signature onto the function when necessary.

static_call

hbutils.reflection.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.

post_process

hbutils.reflection.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

pre_process

hbutils.reflection.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

freduce

hbutils.reflection.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

raising

hbutils.reflection.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_

hbutils.reflection.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

get_callable_hint

hbutils.reflection.func.get_callable_hint(f: Callable)[source]
Overview:

Get type hint of callable.

Arguments:
  • f (Callable): Callable object.

Returns:
  • hint: Hint of the callable.

Example:
>>> def f1(x: float, y: str) -> int:
>>>     pass
>>> get_callable_hint(f1)  # Callable[[float, str], int]
>>>
>>> def f2(x: float, y: str, *, z: int):
>>>     pass
>>> get_callable_hint(f2)  # Callable[..., Any]