Passed
Push — master ( db2481...3bd527 )
by Fabio
03:28
created

benedict.core.flatten.flatten()   A

Complexity

Conditions 4

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 16
rs 9.65
c 0
b 0
f 0
cc 4
nop 3
1
# -*- coding: utf-8 -*-
2
3
from benedict.utils import type_util
4
5
6
def flatten(d, separator='_', **kwargs):
7
    new_dict = d.copy()
8
    new_dict.clear()
9
    keys = list(d.keys())
10
    base_key = kwargs.pop('base_key', '')
11
    for key in keys:
12
        new_key = '{}{}{}'.format(
13
            base_key, separator, key) if base_key and separator else key
14
        value = d.get(key, None)
15
        if type_util.is_dict(value):
16
            new_value = flatten(value, separator=separator, base_key=new_key)
17
            new_value.update(new_dict)
18
            new_dict.update(new_value)
19
        else:
20
            new_dict[new_key] = value
21
    return new_dict
22