Created
July 30, 2010 15:39
-
-
Save pelletier/500734 to your computer and use it in GitHub Desktop.
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
def dict_delta(old, new): | |
""" | |
Compute the differences between two versions of a dictionary | |
>>> dict1 = {\ | |
'ipsum': 'ABC',\ | |
'dsqdq': 'sssdqs',\ | |
'opapoaaz': 'dpqsnqsd',\ | |
'pPlolf': 'popoazn',\ | |
'pfkv': 'blah',\ | |
} | |
>>> dict2 = {\ | |
'ipsum': 'ABl',\ | |
'dsqdq': 'sssdqs',\ | |
'hereiam': 'test',\ | |
'opapoaaz': 'dpqsnqsd',\ | |
'pPlolf': 'popoazn',\ | |
} | |
>>> dict_delta(dict1, dict2) | |
[('pfkv', '-'), ('ipsum', '*', 'ABl'), ('hereiam', '+', 'test')] | |
""" | |
diff = [] | |
new_items = new.keys() | |
for old_item in old.keys(): | |
if not old_item in new: | |
diff.append((old_item, '-')) | |
else: | |
new_items.remove(old_item) | |
new_value = new[old_item] | |
if not old[old_item] == new_value: | |
diff.append((old_item, '*', new_value)) | |
for new_item in new_items: | |
diff.append((new_item, '+', new[new_item])) | |
return diff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment