@@ -44,24 +44,26 @@ def rupdate(d, u):
44
44
return d
45
45
46
46
47
- def rapply (data , func ):
47
+ def rapply (data , func , apply_to_iterables = True ):
48
48
"""
49
49
Recursively apply a function to a dictionary, list, array, or tuple
50
50
51
51
Args:
52
52
data: Input iterable data
53
53
func: Function to apply to all non-iterable values
54
+ apply_to_iterables (bool): Apply the function to elements in lists/tuples
54
55
"""
56
+
55
57
# If the object is a dictionary
56
58
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 ()}
58
60
# If the object is iterable but NOT a dictionary or a string
59
- elif (
61
+ elif apply_to_iterables and (
60
62
isinstance (data , collections .abc .Iterable )
61
63
and not isinstance (data , collections .abc .Mapping )
62
64
and not isinstance (data , str )
63
65
):
64
- return [rapply (x , func ) for x in data ]
66
+ return [rapply (x , func , apply_to_iterables = apply_to_iterables ) for x in data ]
65
67
# if the object is neither a map nor iterable
66
68
else :
67
69
return func (data )
0 commit comments