python-benedict/benedict/core/unflatten.py

21 lines
615 B
Python
Raw Permalink Normal View History

from benedict.core import clone
from benedict.dicts.keylist import keylist_util
from benedict.utils import type_util
2020-02-06 14:18:01 +00:00
def _unflatten_item(key, value, separator):
keys = key.split(separator)
if type_util.is_dict(value):
2021-10-12 12:27:35 +00:00
return (keys, unflatten(value, separator=separator))
return (keys, value)
2020-02-06 14:18:01 +00:00
2022-02-13 10:35:43 +00:00
def unflatten(d, separator="_"):
new_dict = clone(d, empty=True)
keys = list(d.keys())
for key in keys:
value = d.get(key, None)
2020-02-06 14:18:01 +00:00
new_keys, new_value = _unflatten_item(key, value, separator)
keylist_util.set_item(new_dict, new_keys, new_value)
return new_dict