Skip to main content

MIT

Project description

PyPI version CI status

Patchdiff 🔍

Based on rfc6902 this library provides a simple API to generate bi-directional diffs between composite python datastructures composed out of lists, sets, tuples and dicts. The diffs are jsonpatch compliant, and can optionally be serialized to json format. Patchdiff can also be used to apply lists of patches to objects, both in-place or on a deepcopy of the input.

Install

pip install patchdiff

Quick-start

from patchdiff import apply, diff, iapply, to_json

input = {"a": [5, 7, 9, {"a", "b", "c"}], "b": 6}
output = {"a": [5, 2, 9, {"b", "c"}], "b": 6, "c": 7}

ops, reverse_ops = diff(input, output)

assert apply(input, ops) == output
assert apply(output, reverse_ops) == input

iapply(input, ops)  # apply in-place
assert input == output

print(to_json(ops, indent=4))
# [
#     {
#         "op": "add",
#         "path": "/c",
#         "value": 7
#     },
#     {
#         "op": "replace",
#         "path": "/a/1",
#         "value": 2
#     },
#     {
#         "op": "remove",
#         "path": "/a/3/a"
#     }
# ]

Proxy-based patch generation

For better performance, produce() can be used which generates patches by tracking mutations on a proxy object (inspired by Immer):

from patchdiff import produce

base = {"count": 0, "items": [1, 2, 3]}

def recipe(draft):
    """Mutate the draft object - changes are tracked automatically."""
    draft["count"] = 5
    draft["items"].append(4)
    draft["new_field"] = "hello"

result, patches, reverse_patches = produce(base, recipe)

# base is unchanged (immutable by default)
assert base == {"count": 0, "items": [1, 2, 3]}

# result contains the changes
assert result == {"count": 5, "items": [1, 2, 3, 4], "new_field": "hello"}

# patches describe what changed
print(patches)
# [
#     {"op": "replace", "path": "/count", "value": 5},
#     {"op": "add", "path": "/items/-", "value": 4},
#     {"op": "add", "path": "/new_field", "value": "hello"}
# ]

When immutability is not needed, it is possible to apply the ops directly, improving performance even further by not having to make a deepcopy of the given state.

from observ import reactive
from patchdiff import produce

state = reactive({"count": 0})

# Mutate in place and get patches for undo/redo
result, patches, reverse = produce(
    state, 
    lambda draft: draft.update({"count": 5}), 
    in_place=True,
)

assert result is state  # Same object
assert state["count"] == 5  # State was mutated, watchers triggered

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patchdiff-0.3.8.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patchdiff-0.3.8-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file patchdiff-0.3.8.tar.gz.

File metadata

  • Download URL: patchdiff-0.3.8.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for patchdiff-0.3.8.tar.gz
Algorithm Hash digest
SHA256 458bb004d916c0ad805b8d9b543efc75eb38f68e3e839f783920c67aa0e2462d
MD5 55107b187994fc93d1f84ce371a95273
BLAKE2b-256 19a085eca800d564d90c27ad69134d33c0803e41d7911610dc632dd7e91dcc4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for patchdiff-0.3.8.tar.gz:

Publisher: ci.yml on fork-tongue/patchdiff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file patchdiff-0.3.8-py3-none-any.whl.

File metadata

  • Download URL: patchdiff-0.3.8-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for patchdiff-0.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2b67c34813dd523c7bff03eb9c0c2e99278898b8f528ae84c5d4f441d773594f
MD5 8cba79f6b8c969afb44eb2a5e939f6b1
BLAKE2b-256 0c3b3378047c3a61f1223dd45dd2f5706645c02cfa085f82a262e1178c85e6f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for patchdiff-0.3.8-py3-none-any.whl:

Publisher: ci.yml on fork-tongue/patchdiff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page