Skip to content

Instantly share code, notes, and snippets.

@killertilapia
Last active October 27, 2016 05:52
Show Gist options
  • Save killertilapia/ab01ecbd2fe76802f3ff396844f07865 to your computer and use it in GitHub Desktop.
Save killertilapia/ab01ecbd2fe76802f3ff396844f07865 to your computer and use it in GitHub Desktop.
A simple class to find the difference of two single level python dictionaries
'''
A simple class to find the difference of two single level python dictionaries AND where deepdiff is just overkill
https://github.com/seperman/deepdiff
'''
class SimpleDictDiffer(object):
"""
Calculate the difference between two dictionaries as:
(1) items added
(2) items removed
(3) keys same in both but changed values
(4) keys same in both and unchanged values
"""
def __init__(self, first_dict, second_dict):
self.current_dict, self.past_dict = first_dict, second_dict
self.set_current, self.set_past = set(first_dict.keys()), set(second_dict.keys())
self.intersect = self.set_current.intersection(self.set_past)
def added(self):
return self.set_current - self.intersect
def removed(self):
return self.set_past - self.intersect
def changed(self):
return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment