| 1 |
========== |
| 2 |
Dictdiffer |
| 3 |
========== |
| 4 |
.. currentmodule:: dictdiffer |
| 5 |
|
| 6 |
.. image:: https://github.com/inveniosoftware/dictdiffer/workflows/CI/badge.svg |
| 7 |
:target: https://github.com/inveniosoftware/dictdiffer/actions |
| 8 |
|
| 9 |
.. image:: https://img.shields.io/coveralls/inveniosoftware/dictdiffer.svg |
| 10 |
:target: https://coveralls.io/r/inveniosoftware/dictdiffer |
| 11 |
|
| 12 |
.. image:: https://img.shields.io/github/tag/inveniosoftware/dictdiffer.svg |
| 13 |
:target: https://github.com/inveniosoftware/dictdiffer/releases |
| 14 |
|
| 15 |
.. image:: https://img.shields.io/pypi/dm/dictdiffer.svg |
| 16 |
:target: https://pypi.python.org/pypi/dictdiffer |
| 17 |
|
| 18 |
.. image:: https://img.shields.io/github/license/inveniosoftware/dictdiffer.svg |
| 19 |
:target: https://github.com/inveniosoftware/dictdiffer/blob/master/LICENSE |
| 20 |
|
| 21 |
Dictdiffer is a helper module that helps you to diff and patch |
| 22 |
dictionaries. |
| 23 |
|
| 24 |
Installation |
| 25 |
============ |
| 26 |
|
| 27 |
Dictdiffer is on PyPI so all you need is: |
| 28 |
|
| 29 |
.. code-block:: console |
| 30 |
|
| 31 |
$ pip install dictdiffer |
| 32 |
|
| 33 |
|
| 34 |
Usage |
| 35 |
===== |
| 36 |
|
| 37 |
Let's start with an example on how to find the diff between two |
| 38 |
dictionaries using :func:`.diff` method: |
| 39 |
|
| 40 |
.. code-block:: python |
| 41 |
|
| 42 |
from dictdiffer import diff, patch, swap, revert |
| 43 |
|
| 44 |
first = { |
| 45 |
"title": "hello", |
| 46 |
"fork_count": 20, |
| 47 |
"stargazers": ["/users/20", "/users/30"], |
| 48 |
"settings": { |
| 49 |
"assignees": [100, 101, 201], |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
second = { |
| 54 |
"title": "hellooo", |
| 55 |
"fork_count": 20, |
| 56 |
"stargazers": ["/users/20", "/users/30", "/users/40"], |
| 57 |
"settings": { |
| 58 |
"assignees": [100, 101, 202], |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
result = diff(first, second) |
| 63 |
|
| 64 |
assert list(result) == [ |
| 65 |
('change', ['settings', 'assignees', 2], (201, 202)), |
| 66 |
('add', 'stargazers', [(2, '/users/40')]), |
| 67 |
('change', 'title', ('hello', 'hellooo'))] |
| 68 |
|
| 69 |
|
| 70 |
Now we can apply the diff result with :func:`.patch` method: |
| 71 |
|
| 72 |
.. code-block:: python |
| 73 |
|
| 74 |
result = diff(first, second) |
| 75 |
patched = patch(result, first) |
| 76 |
|
| 77 |
assert patched == second |
| 78 |
|
| 79 |
|
| 80 |
Also we can swap the diff result with :func:`.swap` method: |
| 81 |
|
| 82 |
.. code-block:: python |
| 83 |
|
| 84 |
result = diff(first, second) |
| 85 |
swapped = swap(result) |
| 86 |
|
| 87 |
assert list(swapped) == [ |
| 88 |
('change', ['settings', 'assignees', 2], (202, 201)), |
| 89 |
('remove', 'stargazers', [(2, '/users/40')]), |
| 90 |
('change', 'title', ('hellooo', 'hello'))] |
| 91 |
|
| 92 |
|
| 93 |
Let's revert the last changes: |
| 94 |
|
| 95 |
.. code-block:: python |
| 96 |
|
| 97 |
result = diff(first, second) |
| 98 |
reverted = revert(result, patched) |
| 99 |
assert reverted == first |
| 100 |
|
| 101 |
A tolerance can be used to consider closed values as equal. |
| 102 |
The tolerance parameter only applies for int and float. |
| 103 |
|
| 104 |
Let's try with a tolerance of 10% with the values 10 and 10.5: |
| 105 |
|
| 106 |
.. code-block:: python |
| 107 |
|
| 108 |
first = {'a': 10.0} |
| 109 |
second = {'a': 10.5} |
| 110 |
|
| 111 |
result = diff(first, second, tolerance=0.1) |
| 112 |
|
| 113 |
assert list(result) == [] |
| 114 |
|
| 115 |
Now with a tolerance of 1%: |
| 116 |
|
| 117 |
.. code-block:: python |
| 118 |
|
| 119 |
result = diff(first, second, tolerance=0.01) |
| 120 |
|
| 121 |
assert list(result) == ('change', 'a', (10.0, 10.5)) |
| 122 |
|
| 123 |
API |
| 124 |
=== |
| 125 |
|
| 126 |
.. automodule:: dictdiffer |
| 127 |
:members: |
| 128 |
|
| 129 |
.. include:: ../CHANGES |
| 130 |
|
| 131 |
.. include:: ../CONTRIBUTING.rst |
| 132 |
|
| 133 |
License |
| 134 |
======= |
| 135 |
|
| 136 |
.. include:: ../LICENSE |
| 137 |
|
| 138 |
.. include:: ../AUTHORS |