espnet2.utils.kwargs2args.func
Less than 1 minute
espnet2.utils.kwargs2args.func
espnet2.utils.kwargs2args.func(a: int, b, *, c, **kwargs)
Converts a dictionary of keyword arguments into a tuple of positional arguments
for a given function.
This utility function inspects the signature of the provided function and maps the keyword arguments to their respective positional parameters, returning them as a tuple. It supports both positional and keyword-only parameters.
- Parameters:
- func (Callable) – The function whose signature will be inspected.
- kwargs (dict) – A dictionary of keyword arguments to be converted.
- Returns: A tuple containing the positional arguments in the order defined by the function’s signature. If some positional arguments are not provided in kwargs, they will be returned as None.
- Return type: tuple
Examples
>>> def example_func(x, y, *, z):
... return x + y + z
...
>>> kwargs = {'x': 1, 'y': 2, 'z': 3}
>>> kwargs2args(example_func, kwargs)
(1, 2)
>>> kwargs = {'x': 10}
>>> kwargs2args(example_func, kwargs)
(10, None)
NOTE
This function only maps the keyword arguments that match the function’s parameters. If a keyword is not present in the function’s signature, it will be ignored.