-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplp_1_p2.py
More file actions
executable file
·44 lines (36 loc) · 1.14 KB
/
plp_1_p2.py
File metadata and controls
executable file
·44 lines (36 loc) · 1.14 KB
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
#!/usr/bin/env python
a = {'x': [1,2,3], 'y': 1, 'z': set([1,2,3]), 'w': 'qweqwe', 't': {'a': [1, 2]}, 'm': [1]}
b = {'x': [4,5,6], 'y': 4, 'z': set([4,2,3]), 'w': 'asdf', 't': {'a': [3, 2]}, 'm': "wer"}
def merge(obj_a, obj_b):
""" Merges two objects with any depth
>>> merge({}, {})
{}
>>> merge({'a': 1}, {})
{'a': 1}
>>> merge({}, {'b': 1})
{'b': 1}
>>> merge({'x': [1,2,3], 'y': 1, 'z': set([1,2,3]), 'w': 'qweqwe', 't': {'a': [1, 2]}, 'm': [1]}, {'x': [4,5,6], 'y': 4, 'z': set([4,2,3]), 'w': 'asdf', 't': {'a': [3, 2]}, 'm': "wer"})
{'m': ([1], 'wer'), 't': {'a': [1, 2, 3, 2]}, 'w': 'qweqweasdf', 'y': 5, 'x': [1, 2, 3, 4, 5, 6], 'z': set([1, 2, 3, 4])}
"""
res = {}
for k, v in obj_a.iteritems():
if k not in obj_b.keys():
res[k] = v
else:
try:
res[k] = v + obj_b[k]
except TypeError:
try:
res[k] = v.union(obj_b[k])
except AttributeError:
try:
v.iteritems()
except AttributeError:
res[k] = (v, obj_b[k])
else:
res[k] = merge(v, obj_b[k])
for k, v in obj_b.iteritems():
if k not in obj_a.keys():
res[k] = v
return res
print merge(a, b)