python-benedict/benedict/core/clean.py

20 lines
547 B
Python
Raw Normal View History

from benedict.utils import type_util
2020-02-05 09:30:19 +00:00
def _clean_item(d, key, strings, collections):
value = d.get(key, None)
if not value:
2021-10-12 12:27:35 +00:00
del_none = value is None
del_string = strings and type_util.is_string(value)
del_collection = collections and type_util.is_collection(value)
2020-02-05 09:30:19 +00:00
return any([del_none, del_string, del_collection])
2021-10-12 12:27:35 +00:00
2020-02-05 09:30:19 +00:00
return False
def clean(d, strings=True, collections=True):
keys = list(d.keys())
for key in keys:
2020-02-05 09:30:19 +00:00
if _clean_item(d, key, strings, collections):
del d[key]