benedict.core.unflatten.unflatten()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
from benedict.core import clone
2
from benedict.dicts.keylist import keylist_util
3
from benedict.utils import type_util
4
5
6
def _unflatten_item(key, value, separator):
7
    keys = key.split(separator)
8
    if type_util.is_dict(value):
9
        return (keys, unflatten(value, separator=separator))
10
    return (keys, value)
11
12
13
def unflatten(d, separator="_"):
14
    new_dict = clone(d, empty=True)
15
    keys = list(d.keys())
16
    for key in keys:
17
        value = d.get(key, None)
18
        new_keys, new_value = _unflatten_item(key, value, separator)
19
        keylist_util.set_item(new_dict, new_keys, new_value)
20
    return new_dict
21