-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
56 lines (49 loc) · 2.21 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import operator
from collections import defaultdict
from functools import reduce
from tolerant_isinstance import isinstance_tolerant
from flatten_any_dict_iterable_or_whatsoever import fla_tu
def convert_to_normal_dict(di):
if isinstance_tolerant(di, defaultdict):
di = {k: convert_to_normal_dict(v) for k, v in di.items()}
return di
nested_dict = lambda: defaultdict(nested_dict)
def dict_merger(*args):
newdict = nested_dict()
for it in args:
for p in fla_tu(it):
tcr = type(reduce(operator.getitem, p[1][:-1], it))
if tcr is tuple or tcr is set:
tcr = list
if tcr == list:
try:
if not reduce(operator.getitem, p[1][:-2], newdict)[p[1][-2]]:
reduce(operator.getitem, p[1][:-2], newdict)[p[1][-2]] = tcr()
reduce(operator.getitem, p[1][:-2], newdict)[p[1][-2]].append(
p[0]
)
except Exception:
try:
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]] = p[0]
except Exception:
reduce(operator.getitem, p[1][:-2], newdict)[p[1][-2]] = [
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]],
p[0],
]
else:
try:
if not reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]]:
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]] = p[0]
else:
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]] = [
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]]
]
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]].append(
p[0]
)
except Exception:
reduce(operator.getitem, p[1][:-2], newdict)[p[1][-2]] = [
reduce(operator.getitem, p[1][:-1], newdict)[p[1][-1]],
p[0],
]
return convert_to_normal_dict(newdict)