Skip to content

Commit 36eb228

Browse files
committed
Added new posargs2kwargs function.
1 parent eaa9c8f commit 36eb228

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

domdf_python_tools/utils.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
#
4545

4646
# stdlib
47+
import inspect
4748
import itertools
4849
import sys
49-
from typing import Any, Generator, Iterable, List, Sequence, Tuple, Union
50+
from typing import Any, Callable, Dict, Generator, Iterable, List, Optional, Sequence, Tuple, Union
5051

5152
__all__ = [
5253
"pyversion",
@@ -68,6 +69,7 @@
6869
"enquote_value",
6970
"Len",
7071
"double_chain",
72+
"posargs2kwargs",
7173
]
7274

7375
pyversion: int = int(sys.version_info.major) # Python Version
@@ -339,3 +341,33 @@ def double_chain(iterable: Iterable[Iterable]):
339341
"""
340342

341343
yield from itertools.chain.from_iterable(itertools.chain.from_iterable(iterable))
344+
345+
346+
def posargs2kwargs(
347+
args: Iterable[Any],
348+
posarg_names: Union[Iterable[str], Callable],
349+
kwargs: Optional[Dict[str, Any]] = None,
350+
) -> Dict[str, Any]:
351+
"""
352+
Convert the positional args in ``args`` to kwargs, based on the relative order of ``args`` and ``posarg_names``.
353+
354+
:param args: List of positional arguments provided to a function.
355+
:param posarg_names: Either a list of positional argument names for the function, or the function object.
356+
:param kwargs: Optional mapping of keyword argument names to values.
357+
The arguments will be added to this dictionary if provided.
358+
:default kwargs: ``{}``
359+
360+
:return: Dictionary mapping argument names to values.
361+
362+
.. versionadded:: 0.4.10
363+
"""
364+
365+
if kwargs is None:
366+
kwargs = {}
367+
368+
if callable(posarg_names):
369+
posarg_names = inspect.getfullargspec(posarg_names).args
370+
371+
kwargs.update(zip(posarg_names, args))
372+
373+
return kwargs

tests/test_utils.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# this package
2121
from domdf_python_tools import utils
2222
from domdf_python_tools.testing import testing_boolean_values
23-
from domdf_python_tools.utils import Len, chunks, double_chain, list2str, list2string, pyversion, str2tuple
23+
from domdf_python_tools.utils import Len, chunks, double_chain, list2str, posargs2kwargs, pyversion, str2tuple
2424

2525

2626
def test_pyversion():
@@ -96,10 +96,6 @@ def test_list2str(value, expects):
9696
assert isinstance(str_representation, str)
9797
assert str_representation == expects
9898

99-
str_representation = list2string(value)
100-
assert isinstance(str_representation, str)
101-
assert str_representation == expects
102-
10399

104100
@pytest.mark.parametrize(
105101
"value, expects",
@@ -115,10 +111,6 @@ def test_list2str_semicolon(value, expects):
115111
assert isinstance(str_representation, str)
116112
assert str_representation == expects
117113

118-
str_representation = list2string(value, sep=';')
119-
assert isinstance(str_representation, str)
120-
assert str_representation == expects
121-
122114

123115
def test_permutations():
124116
data = ["egg and bacon", "egg sausage and bacon", "egg and spam", "egg bacon and spam"]
@@ -384,3 +376,24 @@ def test_len(capsys):
384376
assert captured.out.splitlines() == ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
385377

386378
assert Len("Hello") == range(5)
379+
380+
381+
def demo_function(arg1, arg2, arg3):
382+
pass
383+
384+
385+
@pytest.mark.parametrize(
386+
"args, posarg_names, kwargs, expects",
387+
[
388+
((1, 2, 3), ("arg1", "arg2", "arg3"), {}, {"arg1": 1, "arg2": 2, "arg3": 3}),
389+
((1, 2, 3), ("arg1", "arg2", "arg3"), None, {"arg1": 1, "arg2": 2, "arg3": 3}),
390+
((1, 2, 3), ("arg1", "arg2", "arg3"), {"arg4": 4}, {"arg1": 1, "arg2": 2, "arg3": 3, "arg4": 4}),
391+
((1, 2, 3), demo_function, None, {
392+
"arg1": 1,
393+
"arg2": 2,
394+
"arg3": 3,
395+
}),
396+
]
397+
)
398+
def test_posargs2kwargs(args, posarg_names, kwargs, expects):
399+
assert posargs2kwargs(args, posarg_names, kwargs) == expects

0 commit comments

Comments
 (0)