Total Complexity | 12 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # -*- coding: utf-8 -*- |
||
2 | |||
3 | from benedict.utils import type_util |
||
4 | |||
5 | |||
6 | def _merge_dict(d, other, overwrite=True, concat=False): |
||
7 | for key, value in other.items(): |
||
8 | _merge_item(d, key, value, overwrite, concat) |
||
9 | |||
10 | |||
11 | def _merge_item(d, key, value, overwrite=True, concat=False): |
||
12 | if key in d: |
||
13 | item = d.get(key, None) |
||
14 | if type_util.is_dict(item) and type_util.is_dict(value): |
||
15 | _merge_dict(item, value, overwrite) |
||
16 | elif concat and type_util.is_list(item) and type_util.is_list(value): |
||
17 | item += value |
||
18 | elif overwrite: |
||
19 | d[key] = value |
||
20 | else: |
||
21 | d[key] = value |
||
22 | |||
23 | |||
24 | def merge(d, other, *args, **kwargs): |
||
25 | overwrite = kwargs.get('overwrite', True) |
||
26 | concat = kwargs.get('concat', False) |
||
27 | others = [other] + list(args) |
||
28 | for other in others: |
||
29 | _merge_dict(d, other, overwrite, concat) |
||
30 | return d |
||
31 |