espnet2.utils.kwargs2args.kwargs2args
Less than 1 minute
espnet2.utils.kwargs2args.kwargs2args
espnet2.utils.kwargs2args.kwargs2args(func, kwargs)
Convert a dictionary of keyword arguments to a tuple of positional arguments
for a given function.
This function inspects the parameters of the provided function and maps the keyword arguments from the dictionary to the corresponding positional arguments. The resulting tuple will contain the positional arguments in the order defined by the function signature.
- Parameters:
- func (Callable) – The function whose parameters will be inspected.
- kwargs (dict) – A dictionary of keyword arguments to be converted.
- Returns: A tuple containing the positional arguments corresponding to the provided keyword arguments. The length of the tuple will be equal to the number of positional parameters in the function signature, but will only include arguments that were found in the kwargs dictionary.
- Return type: tuple
Examples
>>> def example_func(x, y, *, z):
... pass
>>> kwargs = {'x': 1, 'y': 2, 'z': 3}
>>> kwargs2args(example_func, kwargs)
(1, 2)
>>> kwargs = {'x': 5}
>>> kwargs2args(example_func, kwargs)
(5,)
>>> kwargs = {'z': 10}
>>> kwargs2args(example_func, kwargs)
(None, None)