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.1.tar.gz (19.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.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: patience_diff_algo-1.0.1.tar.gz
  • Upload date:
  • Size: 19.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.1.tar.gz
Algorithm Hash digest
SHA256 ff9d5d4399838eea5c988f825d223929467b6119c2318f00964a00cc3b88867e
MD5 8dd9f58d47667a451b6bbfa4480eba00
BLAKE2b-256 35a8601a41ff3f5575e140f0f998d219a158e5d0c4be076e3cf417f278faa123

See more details on using hashes here.

Provenance

The following attestation bundles were made for patience_diff_algo-1.0.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for patience_diff_algo-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 910ef58b5720022d3f658fb83f920eb38b2202946bcd80157301d2da4df5c483
MD5 467286c3abbe727b8ba1a150bfa80f95
BLAKE2b-256 4e055f132f82de4445bcd5dad0ce06929e99814f39d001368d7d83fa2801d6a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for patience_diff_algo-1.0.1-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