Created
January 12, 2011 19:46
-
-
Save mattwilliamson/776748 to your computer and use it in GitHub Desktop.
Merge two dicts together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import collections | |
def quacks_like_dict(object): | |
"""Check if object is dict-like""" | |
return isinstance(object, collections.Mapping) | |
def merge(a, b): | |
"""Merge two deep dicts non-destructively | |
Uses a stack to avoid maximum recursion depth exceptions | |
>>> a = {'a': 1, 'b': {1: 1, 2: 2}, 'd': 6} | |
>>> b = {'c': 3, 'b': {2: 7}, 'd': {'z': [1, 2, 3]}} | |
>>> c = merge(a, b) | |
>>> from pprint import pprint; pprint(c) | |
{'a': 1, 'b': {1: 1, 2: 7}, 'c': 3, 'd': {'z': [1, 2, 3]}} | |
""" | |
assert quacks_like_dict(a), quacks_like_dict(b) | |
dst = a.copy() | |
stack = [(dst, b)] | |
while stack: | |
current_dst, current_src = stack.pop() | |
for key in current_src: | |
if key not in current_dst: | |
current_dst[key] = current_src[key] | |
else: | |
if quacks_like_dict(current_src[key]) and quacks_like_dict(current_dst[key]) : | |
stack.append((current_dst[key], current_src[key])) | |
else: | |
current_dst[key] = current_src[key] | |
return dst | |
if __name__ == '__main__': | |
# Run test in timer | |
import timeit | |
print timeit.Timer('doctest.testmod()', 'import doctest').timeit(number=1), 'seconds' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment