@@ -58,29 +58,12 @@ def sanitize_list(list_to_sanitize, keys=None, values=None, list_recursive=True,
58
58
59
59
def sanitize (obj_to_sanitize , keys = None , values = None , recursive = True , remove_none_values = True ):
60
60
"""Clean up a Python object of type list or dict from specific keys, values and None values if specified"""
61
- if keys is None :
62
- keys = []
63
- if values is None :
64
- values = []
65
- result = deepcopy (obj_to_sanitize )
66
61
if isinstance (obj_to_sanitize , dict ):
67
- for k , v in obj_to_sanitize .items ():
68
- if k in keys :
69
- del result [k ]
70
- elif v in values or (v is None and remove_none_values ):
71
- del result [k ]
72
- elif isinstance (v , dict ) and recursive :
73
- result [k ] = sanitize (v , keys , values )
74
- elif isinstance (v , list ) and recursive :
75
- for index , item in enumerate (v ):
76
- if isinstance (item , dict ):
77
- result [k ][index ] = sanitize (item , keys , values )
62
+ return sanitize_dict (obj_to_sanitize , keys , values , recursive , remove_none_values )
78
63
elif isinstance (obj_to_sanitize , list ):
79
- for index , item in enumerate (obj_to_sanitize ):
80
- result [index ] = sanitize (item , keys , values , recursive , remove_none_values )
64
+ return sanitize_list (obj_to_sanitize , keys , values , recursive , recursive , remove_none_values )
81
65
else :
82
66
raise TypeError ("object to sanitize can only be of type list or dict. Got {}" .format (type (obj_to_sanitize )))
83
- return result
84
67
85
68
86
69
if PY3 :
0 commit comments