Skip to content

Commit 5c09fcf

Browse files
committed
Allow args and kwargs in rapply
1 parent 1e87a44 commit 5c09fcf

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

labthings/core/utilities.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def rupdate(d, u):
4545
return d
4646

4747

48-
def rapply(data, func, apply_to_iterables=True):
48+
def rapply(data, func, *args, apply_to_iterables=True, **kwargs):
4949
"""
5050
Recursively apply a function to a dictionary, list, array, or tuple
5151
@@ -58,7 +58,9 @@ def rapply(data, func, apply_to_iterables=True):
5858
# If the object is a dictionary
5959
if isinstance(data, collections.abc.Mapping):
6060
return {
61-
key: rapply(val, func, apply_to_iterables=apply_to_iterables)
61+
key: rapply(
62+
val, func, *args, apply_to_iterables=apply_to_iterables, **kwargs
63+
)
6264
for key, val in data.items()
6365
}
6466
# If the object is iterable but NOT a dictionary or a string
@@ -67,10 +69,13 @@ def rapply(data, func, apply_to_iterables=True):
6769
and not isinstance(data, collections.abc.Mapping)
6870
and not isinstance(data, str)
6971
):
70-
return [rapply(x, func, apply_to_iterables=apply_to_iterables) for x in data]
72+
return [
73+
rapply(x, func, *args, apply_to_iterables=apply_to_iterables, **kwargs)
74+
for x in data
75+
]
7176
# if the object is neither a map nor iterable
7277
else:
73-
return func(data)
78+
return func(data, *args, **kwargs)
7479

7580

7681
def get_by_path(root, items):

0 commit comments

Comments
 (0)