Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,46 @@ dictionaries using :func:`.diff` method:
('add', 'stargazers', [(2, '/users/40')]),
('change', 'title', ('hello', 'hellooo'))]

The :func:`.diff` method can ignore fields by passing any container to the
``ignore`` argument. This allows custom path matching, for example ignoring a
field in every object of nested arrays regardless of the array index:

.. code-block:: python

class IgnorePath:
def __init__(self, *path):
self.path = path

def __contains__(self, path):
if not isinstance(path, tuple):
path = tuple(path.split("."))
return len(path) == len(self.path) and all(
expected is None or expected == actual
for expected, actual in zip(self.path, path)
)

first = {
"races": [{
"reportingUnits": [{
"apiAccessTime": "2018-06-04T12:00:00Z",
"name": "North",
}],
}],
}
second = {
"races": [{
"reportingUnits": [{
"apiAccessTime": "2018-06-04T12:05:00Z",
"name": "North",
}],
}],
}

ignore = IgnorePath(
"races", None, "reportingUnits", None, "apiAccessTime")

assert list(diff(first, second, ignore=ignore)) == []


Now we can apply the diff result with :func:`.patch` method:

Expand Down