-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_utils.py
49 lines (47 loc) · 1.36 KB
/
dict_utils.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
def dict_merge(main, default, c_path=[]):
"""
Pure dict deep-merge function. First dict has precedence.
"""
if isinstance(main, dict):
if default and not isinstance(default, dict):
print(str(c_path))
raise ValueError('Skew-level dictionaries cannot be merged at %s' % ('.'.join(c_path)))
return {
k: dict_merge(
main[k],
default=({} if default is None else default).get(k),
c_path=c_path + [(k if isinstance(k, str) else '[%i]' % k)]
) if k in main else default[k]
for k in ({} if default is None else default.keys()) | main.keys()
}
else:
return main
def dict_prune_nulls(tree):
"""
Prune None values out of dictionaries.
Empty maps as well go away.
"""
if isinstance(tree, dict):
pruned = {
k: v
for k, v in {
k2: dict_prune_nulls(v2)
for k2, v2 in tree.items()
}.items()
if v is not None
}
if pruned:
return pruned
else:
return None
elif isinstance(tree, list):
return [
v
for v in (
dict_prune_nulls(v2)
for v2 in tree
)
if v is not None
]
else:
return tree