Skip to content

Commit

Permalink
Option to not run rapply on list elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jan 20, 2020
1 parent 6964029 commit 57ab2cf
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions labthings/core/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,26 @@ def rupdate(d, u):
return d


def rapply(data, func):
def rapply(data, func, apply_to_iterables=True):
"""
Recursively apply a function to a dictionary, list, array, or tuple
Args:
data: Input iterable data
func: Function to apply to all non-iterable values
apply_to_iterables (bool): Apply the function to elements in lists/tuples
"""

# If the object is a dictionary
if isinstance(data, collections.abc.Mapping):
return {key: rapply(val, func) for key, val in data.items()}
return {key: rapply(val, func, apply_to_iterables=apply_to_iterables) for key, val in data.items()}
# If the object is iterable but NOT a dictionary or a string
elif (
elif apply_to_iterables and (
isinstance(data, collections.abc.Iterable)
and not isinstance(data, collections.abc.Mapping)
and not isinstance(data, str)
):
return [rapply(x, func) for x in data]
return [rapply(x, func, apply_to_iterables=apply_to_iterables) for x in data]
# if the object is neither a map nor iterable
else:
return func(data)
Expand Down

0 comments on commit 57ab2cf

Please sign in to comment.