Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
a = {'a': 1, 'b': 1, 'c': 0}
b = {'a': 1, 'b': 2, 'd': 0}
self.d = DictDiffer(b, a)
def diff_dicts(d1, d2, d1_label='d1', d2_label='d2', print_unchanged=False):
"""
Diff two dictionaries - prints changed, added and removed keys and the changed values. DOES NOT DO NESTED DICTS!
:param d1: First dict - we compare this with d2
:param d2: Second dict - we compare against this one
:param d1_label: Will be used instead of "d1" in debugging output to make it more helpful.
:param d2_label: Will be used instead of "d2" in debugging output to make it more helpful.
:param print_unchanged: - should we print set of unchanged keys (can be long and useless). Default: False.
:return: nothing, prints results to STDOUT
"""
differ = dictdiffer.DictDiffer(d1, d2)
print 'Added :: keys present in {d1} which are not in {d2}'.format(d1=d1_label, d2=d2_label)
print differ.added()
print
print 'Removed :: keys present in {d2} which are not in {d1}'.format(d1=d1_label, d2=d2_label)
print differ.removed()
print
print 'Changed :: keys which are the same in {d1} and {d2} but whose values are different'.format(d1=d1_label, d2=d2_label)
print differ.changed()
print
if differ.changed():
print 'Changed values :: the values of keys which have changed. Format is as follows:'
print ' Key name:'
print ' value in {d1}'.format(d1=d1_label)
print ' value in {d2}'.format(d2=d2_label)
print
def recurse(context, c, o):
dd = dictdiffer.DictDiffer(c, o)
changed = dd.changed()
added = dd.added()
for a in added:
context[a] = c[a]
for change in changed:
sub = c[change]
if isinstance(c[change], dict):
context[change] = {}
recurse(context[change], c[change], o[change])
else:
context[change] = sub