lisa.utils.kwargs_dispatcher#

lisa.utils.kwargs_dispatcher(f_map, ignore=None, allow_overlap=True)[source]#

Decorate a function so that it acts as an argument dispatcher between multiple other functions.

Parameters:
  • f_map (dict(collections.abc.Callable, str) or list(collections.abc.Callable)) – Mapping of functions to name of the parameter that will receive the collected parameters. None values will be replaced with f'{f.__name__}_kwargs' for convenience. If passed an non-mapping iterable, it will be transformed into {f: None, f2: None, ...}.

  • ignore (list(str) or None) – Set of parameters to ignore in the f_map functions. They will not be added to the signature and not be collected.

  • allow_overlap (bool) – If True, the functions in f_map are allowed to have overlapping parameters. If False, an TypeError will be raised if there is any overlap.

Example:

def f(x, y):
    print('f', x, y)

def g(y, z):
    print('g', y, z)

# f_kwargs will receive a dict of parameters to pass to "f", same for
# g_kwargs.
# The values will also be passed to the function directly in x, y and z
# parameters.
@kwargs_dispatcher({f: "f_kwargs", g: "g_kwargs"})
def h(x, y, f_kwargs, g_kwargs, z):
    print('f', f_kwargs)
    print('g', g_kwargs)
    print(x, y, z)

h(y=2, x=1, z=3)
h(1,2,3)
h(1,y=2,z=3)