-
Notifications
You must be signed in to change notification settings - Fork 1
/
DictDiffer.py
28 lines (28 loc) · 1.03 KB
/
DictDiffer.py
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
"""
Credit for DictDiffer to hughdbrown:
https://github.com/hughdbrown/dictdiffer
"""
class DictDiffer(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, current_dict, past_dict):
self.current_dict, self.past_dict = current_dict, past_dict
self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
self.intersect = self.set_current.intersection(self.set_past)
def added(self):
""" doc """
return self.set_current - self.intersect
def removed(self):
""" doc """
return self.set_past - self.intersect
def changed(self):
""" doc """
return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
""" doc """
return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])