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 blocksget_hunks(transform, prev_array, next_array, context=-1, big_enough=1)- Generate hunksmatches(prev_array, next_array)- Get matching index pairsmatch_ratio(prev_array, next_array)- Calculate similarity ratio
Data Types
Hunk- Contiguous diff region with rangesMatchingBlock- Matching subsequence (prev_start, next_start, length)- Range types:
Same,Prev,Next,Replace
Project Structure
bram_diff/ # 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:
- Identify unique elements (appearing exactly once in both sequences)
- Build longest increasing subsequence (LIS) of unique matches using patience sorting
- Recursively find matches in gaps between LIS elements
- Remove spurious small matches ("semantic cleanup")
- Merge adjacent blocks separated by identical elements
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file patience_diff_algo-1.0.3.tar.gz.
File metadata
- Download URL: patience_diff_algo-1.0.3.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61b87085684f7dde2c0bb0d831bb38ab230519b30c5d2561d60f24414a1b67fd
|
|
| MD5 |
c4349e028c928753450c925dfd1a32b8
|
|
| BLAKE2b-256 |
256a21fa9ea59d4d23e9b920a4c0e6a540cd46d00374a68b220e089e519ce52c
|
Provenance
The following attestation bundles were made for patience_diff_algo-1.0.3.tar.gz:
Publisher:
python-publish.yml on joery0x3b800001/patience_diff
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
patience_diff_algo-1.0.3.tar.gz -
Subject digest:
61b87085684f7dde2c0bb0d831bb38ab230519b30c5d2561d60f24414a1b67fd - Sigstore transparency entry: 1226117192
- Sigstore integration time:
-
Permalink:
joery0x3b800001/patience_diff@4d5ab522bb07d9eeaada7366e5b0e363b0aab819 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/joery0x3b800001
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4d5ab522bb07d9eeaada7366e5b0e363b0aab819 -
Trigger Event:
release
-
Statement type:
File details
Details for the file patience_diff_algo-1.0.3-py3-none-any.whl.
File metadata
- Download URL: patience_diff_algo-1.0.3-py3-none-any.whl
- Upload date:
- Size: 25.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50aa63a37e136a41d11768ef82f550b3a8b3661af3001d19d7b0e767b5a91d4c
|
|
| MD5 |
b9c2f8299824c001fc7dc572b04cb0dd
|
|
| BLAKE2b-256 |
6fda10eff27de6b4c4167e942052c0c4bffb9abd7f7613cf1eeddaa86cb4a0b2
|
Provenance
The following attestation bundles were made for patience_diff_algo-1.0.3-py3-none-any.whl:
Publisher:
python-publish.yml on joery0x3b800001/patience_diff
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
patience_diff_algo-1.0.3-py3-none-any.whl -
Subject digest:
50aa63a37e136a41d11768ef82f550b3a8b3661af3001d19d7b0e767b5a91d4c - Sigstore transparency entry: 1226117256
- Sigstore integration time:
-
Permalink:
joery0x3b800001/patience_diff@4d5ab522bb07d9eeaada7366e5b0e363b0aab819 -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/joery0x3b800001
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4d5ab522bb07d9eeaada7366e5b0e363b0aab819 -
Trigger Event:
release
-
Statement type: