| Total Complexity | 4 |
| Total Lines | 22 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |