Skip to main content

Patience Diff Algorithm - An implementation of Bram Cohen's diff algorithm

Project description

Patience Diff

A complete Python port of Bram Cohen's algorithm for computing efficient differences.

Features

  • Patience Sorting Algorithm: O(n log n)
  • Semantic Cleanup: Removes spurious matches
  • Multiple Output Formats: Blocks, hunks with context
  • Myers Fallback: Simple diff for edge cases
  • Type Hints: Full annotations for IDE support
  • Well Tested: Comprehensive test suite

Quick Start

make install  # Install in dev mode
make test     # Run tests

License

GNU General Public License v2 or later

Original algorithm and OCaml implementation by Bram Cohen
Copyright (C) 2005, 2006 Canonical Ltd
Python port by Shaun Joe

API Reference

Core Functions

  • get_matching_blocks(transform, prev_array, next_array, big_enough=1) - Find matching blocks
  • get_hunks(transform, prev_array, next_array, context=-1, big_enough=1) - Generate hunks
  • matches(prev_array, next_array) - Get matching index pairs
  • match_ratio(prev_array, next_array) - Calculate similarity ratio

Data Types

  • Hunk - Contiguous diff region with ranges
  • MatchingBlock - Matching subsequence (prev_start, next_start, length)
  • Range types: Same, Prev, Next, Replace

Project Structure

src/                      # Python package (11 modules)
├── patience_diff.py      # Core algorithm (700+ lines)
├── plain_diff.py         # Myers fallback
├── hunk.py               # Hunk data structures
├── hunks.py              # Hunk utilities
├── matching_block.py     # Matching block type
├── range.py              # Range types
├── move_id.py            # Move identifiers
├── move_kind.py          # Move classification
├── patience_diff_intf.py # Interface definitions
├── patience_diff_lib.py  # Library exports
└── __init__.py           # Package initialization

test/                     # Test suite
├── test_limit_context.py
├── test_merge_diff.py
└── test_plain_diff_cutoff.py

Makefile                  # Build automation
setup.py                  # Package setup
pyproject.toml            # Build configuration
README.md

Testing

All tests passing:

$ make test
test/test_limit_context.py PASSED
test/test_merge_diff.py PASSED
test/test_plain_diff_cutoff.py PASSED

============================  3 passed  ============================

Algorithm

The Patience Diff algorithm works in several steps:

  1. Identify unique elements (appearing exactly once in both sequences)
  2. Build longest increasing subsequence (LIS) of unique matches using patience sorting
  3. Recursively find matches in gaps between LIS elements
  4. Remove spurious small matches ("semantic cleanup")
  5. Merge adjacent blocks separated by identical elements
  6. Convert matching blocks to hunks with Same/Prev/Next/Replace ranges

References

  • http://bazaar-vcs.org - Original implementation
  • Eugene W. Myers: "An O(ND) Difference Algorithm and Its Variations"

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

patience_diff_algo-1.0.2.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

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

patience_diff_algo-1.0.2-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file patience_diff_algo-1.0.2.tar.gz.

File metadata

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

File hashes

Hashes for patience_diff_algo-1.0.2.tar.gz
Algorithm Hash digest
SHA256 603453c7ddbe238cf877b39289beedcba868ced40fc55a899e4fc7c8d265dec5
MD5 e12552defb7e0c94d0230b0c2c8fe1de
BLAKE2b-256 2d79c7f7d7ddbe92b5f095a6f688482033749434640b0bcf03370c2ec8552816

See more details on using hashes here.

Provenance

The following attestation bundles were made for patience_diff_algo-1.0.2.tar.gz:

Publisher: python-publish.yml on joery0x3b800001/patience_diff

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

File details

Details for the file patience_diff_algo-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for patience_diff_algo-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1ffd17f68748aa9d6ea66c30ff807293d2d5f985acbbc803fe53f13cb606216c
MD5 deb808c7e707bcf68d0047897f8f504a
BLAKE2b-256 313002cf4009c93d04f0747e41447c8e9ae12bec5f918145a419a87491cbafcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for patience_diff_algo-1.0.2-py3-none-any.whl:

Publisher: python-publish.yml on joery0x3b800001/patience_diff

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