2020-09-17 12:09:59 +00:00
|
|
|
from benedict.core import clone
|
2020-02-03 17:25:54 +00:00
|
|
|
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="_"):
|
2020-09-17 12:09:59 +00:00
|
|
|
new_dict = clone(d, empty=True)
|
2020-02-03 17:25:54 +00:00
|
|
|
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)
|
2020-02-03 17:25:54 +00:00
|
|
|
keylist_util.set_item(new_dict, new_keys, new_value)
|
|
|
|
return new_dict
|