Skip to content

Commit 57ab2cf

Browse files
author
Joel Collins
committed
Option to not run rapply on list elements
1 parent 6964029 commit 57ab2cf

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

labthings/core/utilities.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,26 @@ def rupdate(d, u):
4444
return d
4545

4646

47-
def rapply(data, func):
47+
def rapply(data, func, apply_to_iterables=True):
4848
"""
4949
Recursively apply a function to a dictionary, list, array, or tuple
5050
5151
Args:
5252
data: Input iterable data
5353
func: Function to apply to all non-iterable values
54+
apply_to_iterables (bool): Apply the function to elements in lists/tuples
5455
"""
56+
5557
# If the object is a dictionary
5658
if isinstance(data, collections.abc.Mapping):
57-
return {key: rapply(val, func) for key, val in data.items()}
59+
return {key: rapply(val, func, apply_to_iterables=apply_to_iterables) for key, val in data.items()}
5860
# If the object is iterable but NOT a dictionary or a string
59-
elif (
61+
elif apply_to_iterables and (
6062
isinstance(data, collections.abc.Iterable)
6163
and not isinstance(data, collections.abc.Mapping)
6264
and not isinstance(data, str)
6365
):
64-
return [rapply(x, func) for x in data]
66+
return [rapply(x, func, apply_to_iterables=apply_to_iterables) for x in data]
6567
# if the object is neither a map nor iterable
6668
else:
6769
return func(data)

0 commit comments

Comments
 (0)