New remove_none_attributes utility

Returns a new dict with all None values removed.
This commit is contained in:
2016-03-05 23:18:40 +01:00
parent bec21e14e6
commit bc1a146ef2
2 changed files with 23 additions and 2 deletions

View File

@@ -57,3 +57,23 @@ def convert_datetime(item):
item[k] = datetime.strptime(item[k], "%a, %d %b %Y %H:%M:%S %Z")
return item
def remove_none_attributes(attributes):
"""Return a new dict with all None values removed"""
# out = {}
# for k, v in attributes.iteritems():
# if v is not None:
# if type(v) is dict:
# attributes[k] = remove_none_attributes(v)
# else:
# out[k] = v
# return out
if isinstance(attributes, (list, tuple, set)):
return type(attributes)(remove_none_attributes(x) for x in attributes if x is not None)
elif isinstance(attributes, dict):
return type(attributes)((remove_none_attributes(k), remove_none_attributes(v))
for k, v in attributes.items() if k is not None and v is not None)
else:
return attributes