Passed
Push — master ( b3caf6...57589b )
by Fabio
02:22
created

benedict.core.unflatten._unflatten_item()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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