Skip to main content

Dictdiffer

Dictdiffer is a helper module that helps you to diff and patch dictionaries.

Installation

Requires Python 3.11 or newer.

Install using pip

pip install git+https://github.com/inspirehep/dictdiffer.git

Usage

Let's start with an example on how to find the diff between two dictionaries using :func:.diff method:

from inspire_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) == [
	('change', ['settings', 'assignees', 2], (201, 202)),
	('add', 'stargazers', [(2, '/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) == [
	('change', ['settings', 'assignees', 2], (202, 201)),
	('remove', 'stargazers', [(2, '/users/40')]),
	('change', 'title', ('hellooo', 'hello'))]

Let's revert the last changes:

result = diff(first, second)
reverted = revert(result, patched)
assert reverted == first

A tolerance can be used to consider closed values as equal. The tolerance parameter only applies for int and float. Let's try with a tolerance of 10% with the values 10 and 10.5:

first = {'a': 10.0}
second = {'a': 10.5}
result = diff(first, second, tolerance=0.1)

assert list(result) == []

Now with a tolerance of 1%:

result = diff(first, second, tolerance=0.01)

assert list(result) == ('change', 'a', (10.0, 10.5))

Testing

Running the test suite is as simple as:

poetry install --with dev --extras numpy
poetry run pytest

Release files for inspire-dictdiffer 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for inspire-dictdiffer 0.0.7
File Size Uploaded
inspire_dictdiffer-0.0.7.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for inspire-dictdiffer 0.0.7
File Interpreter ABI Platform
inspire_dictdiffer-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 29.8 kB

Release files / inspire_dictdiffer-0.0.7.tar.gz

Download URL inspire_dictdiffer-0.0.7.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6a65967ca6438317242b3094c67dfdb5f8db25fa256a1292aaba5ba9036e7c92
BLAKE2b-256 checksum
How to use checksums
9cf793c2470458004e15c8df4892c325e3497f0d94a07946837bf23aa338e3fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / inspire_dictdiffer-0.0.7-py3-none-any.whl

Download URL inspire_dictdiffer-0.0.7-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2704da4b1682f9e138fdec268ac3708ae0a76c403cb432abe8f9e586780bc8d5
BLAKE2b-256 checksum
How to use checksums
38f68898ddd6de8d9400cdac9abbbbf5c2a5428a3ba0d9ca8f7eafdfc242c7d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page