This repository has been archived by the owner on May 26, 2023. It is now read-only.
forked from jwpeddle/dictdiffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictdiffer.py
223 lines (166 loc) · 5.75 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import copy
import collections
(ADD, REMOVE, PUSH, PULL, CHANGE) = (
'add', 'remove', 'push', 'pull', 'change')
def diff(first, second, node=None):
"""
Compares two dictionary object, and returns a diff result.
>>> result = diff({'a':'b'}, {'a':'c'})
>>> list(result)
[('change', 'a', ('b', 'c'))]
"""
node = node or []
dotted_node = '.'.join(node)
if (isinstance(first, collections.Iterable)
and not isinstance(first, basestring)):
# dictionaries are not hashable, we can't use sets
intersection = [k for k in first if k in second]
addition = [k for k in second if not k in first]
deletion = [k for k in first if not k in second]
def diff_dict():
"""Compares if object is a dictionary. Callees again the parent
function as recursive if dictionary have child objects.
Yields `add` and `remove` flags."""
if addition:
yield ADD, dotted_node, [
# for additions, return a list that consist with
# two-pair tuples.
(key, second[key]) for key in addition]
if deletion:
yield REMOVE, dotted_node, [
# for deletions, return the list of removed keys
# and values.
(key, first[key]) for key in deletion]
for key in intersection:
# if type is not changed, callees again diff function to compare.
# otherwise, the change will be handled as `change` flag.
recurred = diff(
first[key],
second[key],
node=node + [key])
for diffed in recurred:
yield diffed
def diff_list():
"""Compares if objects are list. Yields `push` and `pull` flags."""
if addition:
# the addition will be consist with the list of added values.
yield PUSH, dotted_node, addition
if deletion:
# for deletions, returns the list of removed items
yield PULL, dotted_node, deletion
def diff_otherwise():
"""Compares string and integer types. Yields `change` flag."""
if first != second:
yield CHANGE, dotted_node, (first, second)
differs = {
dict: diff_dict,
list: diff_list
}
differ = differs.get(type(first))
return (differ or diff_otherwise)()
def patch(diff_result, destination):
"""
Patches the diff result to the old dictionary.
>>> patch([('push', 'numbers', [2, 3])], {'numbers': []})
{'numbers': [2, 3]}
>>> patch([('pull', 'numbers', [1])], {'numbers': [1, 2, 3]})
{'numbers': [2, 3]}
"""
destination = copy.deepcopy(destination)
def add(node, changes):
for key, value in changes:
dot_lookup(destination, node)[key] = value
def change(node, changes):
dest = dot_lookup(destination, node, parent=True)
last_node = node.split('.')[-1]
_, value = changes
dest[last_node] = value
def remove(node, changes):
for key, _ in changes:
del dot_lookup(destination, node)[key]
def pull(node, changes):
dest = dot_lookup(destination, node)
for val in changes:
dest.remove(val)
def push(node, changes):
dest = dot_lookup(destination, node)
for val in changes:
dest.append(val)
patchers = {
PUSH: push,
PULL: pull,
REMOVE: remove,
ADD: add,
CHANGE: change
}
for action, node, changes in diff_result:
patchers[action](node, changes)
return destination
def swap(diff_result):
"""
Swaps the diff result with the following mapping
pull -> push
push -> pull
remove -> add
add -> remove
In addition, swaps the changed values for `change` flag.
>>> swapped = swap([('add', 'a.b.c', ('a', 'b'))])
>>> next(swapped)
('remove', 'a.b.c', ('a', 'b'))
>>> swapped = swap([('change', 'a.b.c', ('a', 'b'))])
>>> next(swapped)
('change', 'a.b.c', ('b', 'a'))
"""
def push(node, changes):
return PULL, node, changes
def pull(node, changes):
return PUSH, node, changes
def add(node, changes):
return REMOVE, node, changes
def remove(node, changes):
return ADD, node, changes
def change(node, changes):
first, second = changes
return CHANGE, node, (second, first)
swappers = {
PUSH: push,
PULL: pull,
REMOVE: remove,
ADD: add,
CHANGE: change
}
for action, node, change in diff_result:
yield swappers[action](node, change)
def revert(diff_result, destination):
"""
A helper function that calles swap function to revert
patched dictionary object.
>>> first = {'a': 'b'}
>>> second = {'a': 'c'}
>>> revert(diff(first, second), second)
{'a': 'b'}
"""
return patch(swap(diff_result), destination)
def dot_lookup(source, lookup, parent=False):
"""
A helper function that allows you to reach dictionary
items with dot lookup (e.g. document.properties.settings)
>>> dot_lookup({'a': {'b': 'hello'}}, 'a.b')
'hello'
If parent argument is True, returns the parent node of matched
object.
>>> dot_lookup({'a': {'b': 'hello'}}, 'a.b', parent=True)
{'b': 'hello'}
If node is empy value, returns the whole dictionary object.
>>> dot_lookup({'a': {'b': 'hello'}}, '')
{'a': {'b': 'hello'}}
"""
if not lookup:
return source
value = source
keys = lookup.split('.')
if parent:
keys = keys[:-1]
for key in keys:
value = value[key]
return value