Improved code quality.
This commit is contained in:
parent
969640bf72
commit
b3caf62c0f
|
@ -3,13 +3,18 @@
|
|||
from benedict.utils import type_util
|
||||
|
||||
|
||||
def _clean_item(d, key, strings, collections):
|
||||
value = d.get(key, None)
|
||||
if not value:
|
||||
del_none = (value is None)
|
||||
del_string = (strings and type_util.is_string(value))
|
||||
del_collection = (collections and type_util.is_collection(value))
|
||||
return any([del_none, del_string, del_collection])
|
||||
return False
|
||||
|
||||
|
||||
def clean(d, strings=True, collections=True):
|
||||
keys = list(d.keys())
|
||||
for key in keys:
|
||||
value = d.get(key, None)
|
||||
if not value:
|
||||
del_none = value is None
|
||||
del_string = strings and type_util.is_string(value)
|
||||
del_collection = collections and type_util.is_collection(value)
|
||||
if any([del_none, del_string, del_collection]):
|
||||
del d[key]
|
||||
if _clean_item(d, key, strings, collections):
|
||||
del d[key]
|
||||
|
|
|
@ -3,19 +3,24 @@
|
|||
from benedict.utils import type_util
|
||||
|
||||
|
||||
def _flatten_key(base_key, key, separator):
|
||||
if base_key and separator:
|
||||
return '{}{}{}'.format(base_key, separator, key)
|
||||
return key
|
||||
|
||||
|
||||
def flatten(d, separator='_', **kwargs):
|
||||
new_dict = d.copy()
|
||||
new_dict.clear()
|
||||
keys = list(d.keys())
|
||||
base_key = kwargs.pop('base_key', '')
|
||||
for key in keys:
|
||||
new_key = '{}{}{}'.format(
|
||||
base_key, separator, key) if base_key and separator else key
|
||||
value = d.get(key, None)
|
||||
new_key = _flatten_key(base_key, key, separator)
|
||||
if type_util.is_dict(value):
|
||||
new_value = flatten(value, separator=separator, base_key=new_key)
|
||||
new_value.update(new_dict)
|
||||
new_dict.update(new_value)
|
||||
else:
|
||||
new_dict[new_key] = value
|
||||
continue
|
||||
new_dict[new_key] = value
|
||||
return new_dict
|
||||
|
|
|
@ -3,13 +3,21 @@
|
|||
from benedict.utils import type_util
|
||||
|
||||
|
||||
def _merge_dict(d, other):
|
||||
for key, value in other.items():
|
||||
_merge_item(d, key, value)
|
||||
|
||||
|
||||
def _merge_item(d, key, value):
|
||||
item = d.get(key, None)
|
||||
if type_util.is_dict(item) and type_util.is_dict(value):
|
||||
_merge_dict(item, value)
|
||||
else:
|
||||
d[key] = value
|
||||
|
||||
|
||||
def merge(d, other, *args):
|
||||
others = [other] + list(args)
|
||||
for other in others:
|
||||
for key, value in other.items():
|
||||
src = d.get(key, None)
|
||||
if type_util.is_dict(src) and type_util.is_dict(value):
|
||||
merge(src, value)
|
||||
else:
|
||||
d[key] = value
|
||||
_merge_dict(d, other)
|
||||
return d
|
||||
|
|
|
@ -15,7 +15,7 @@ class KeylistDict(dict):
|
|||
return super(KeylistDict, self).__contains__(key)
|
||||
|
||||
def _contains_by_keys(self, keys):
|
||||
parent, key, _ = keylist_util.get_item(self, keys)
|
||||
parent, _, _ = keylist_util.get_item(self, keys)
|
||||
if type_util.is_dict(parent):
|
||||
return True
|
||||
elif type_util.is_list(parent):
|
||||
|
|
Loading…
Reference in New Issue