Dictdiffer

0.9.0 · active · verified Thu Apr 09

Dictdiffer is a Python library designed to compute differences between dictionaries and apply patches based on those differences. It handles nested structures, lists, and sets. The current version is 0.9.0, with releases occurring on an as-needed basis.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `diff` to find differences between two dictionaries, `patch` to apply those changes, and `revert` to undo them. It also shows a basic example of ignoring specific keys during diffing.

from dictdiffer import diff, patch, revert

# Example dictionaries
old = {'a': 1, 'b': {'c': 2, 'd': [3, 4]}}
new = {'a': 10, 'b': {'c': 2, 'd': [3, 5]}, 'e': 6}

# Compute the diff
delta = list(diff(old, new))
print(f"Diff operations: {delta}")

# Apply the patch to the old dictionary
patched_dict = patch(delta, old)
print(f"Patched dictionary: {patched_dict}")

# Revert the patch from the new dictionary
reverted_dict = revert(delta, new)
print(f"Reverted dictionary: {reverted_dict}")

# Example with ignoring keys
old_ignore = {'name': 'Alice', 'version': '1.0', 'data': [1,2]}
new_ignore = {'name': 'Alice', 'version': '1.1', 'data': [1,2]}
ignored_delta = list(diff(old_ignore, new_ignore, ignore=['version']))
print(f"Diff ignoring 'version': {ignored_delta}")

view raw JSON →