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

benedict.core.flatten   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A flatten() 0 16 4
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