Dictdiffer is a helper module that helps you to diff and patch dictionaries.
Compares two dictionary object and returns a diff result.
Applies the diff result to the old dictionary.
Swaps the diff result to revert changes.
A shortcut function that swaps and patches the diff result to destination dictionary.
$ pip install dictdiffer
from dictdiffer import diff, patch, swap, revert
first = {
"title": "hello",
"fork_count": 20,
"stargazers": ["/users/20", "/users/30"],
"settings": {
"assignees": [100, 101, 201],
}
}
second = {
"title": "hellooo",
"fork_count": 20,
"stargazers": ["/users/20", "/users/30", "/users/40"],
"settings": {
"assignees": [100, 101, 202],
}
}
result = diff(first, second)
assert list(result) == [
('push', 'settings.assignees', [202]),
('pull', 'settings.assignees', [201]),
('push', 'stargazers', ['/users/40']),
('change', 'title', ('hello', 'hellooo'))]
Now we can apply the diff result with patch
method.
result = diff(first, second)
patched = patch(result, first)
assert patched == second
Also we can swap the diff result with swap
method.
result = diff(first, second)
swapped = swap(result)
assert list(swapped) == [
('pull', 'settings.assignees', [202]),
('push', 'settings.assignees', [201]),
('pull', 'stargazers', ['/users/40']),
('change', 'title', ('hellooo', 'hello'))]
Let's revert the last changes.
reverted = revert(result, patched)
assert reverted == first
I decided to make revision history mechanism in dbpatterns.com for documents. Documents are storing in MongoDB, so I was needed a dictionary differ that diffs two dictionary and reverts back changes.
After your changes, you could run the tests to ensure everything is operating correctly.
$ source run_tests.sh
Ran 15 tests in 0.002s
Name Stmts Miss Cover Missing
------------------------------------------
dictdiffer 74 0 100%
------------------------------------------
TOTAL 74 0 100%