New remove_none_attributes utility
Returns a new dict with all None values removed.
This commit is contained in:
@@ -211,10 +211,11 @@ class Create(Resource):
|
|||||||
>>> node = Node({})
|
>>> node = Node({})
|
||||||
>>> node.create()
|
>>> node.create()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
api = api or self.api
|
api = api or self.api
|
||||||
headers = self.http_headers()
|
headers = self.http_headers()
|
||||||
|
attributes = utils.remove_none_attributes(self.to_dict())
|
||||||
new_attributes = api.post(self.path, self.to_dict(), headers)
|
new_attributes = api.post(self.path, attributes, headers)
|
||||||
self.error = None
|
self.error = None
|
||||||
self.merge(new_attributes)
|
self.merge(new_attributes)
|
||||||
return self.success()
|
return self.success()
|
||||||
|
|||||||
@@ -57,3 +57,23 @@ def convert_datetime(item):
|
|||||||
item[k] = datetime.strptime(item[k], "%a, %d %b %Y %H:%M:%S %Z")
|
item[k] = datetime.strptime(item[k], "%a, %d %b %Y %H:%M:%S %Z")
|
||||||
|
|
||||||
return item
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user