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

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:

  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.3.tar.gz (20.4 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.3-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

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

Hashes for patience_diff_algo-1.0.3.tar.gz
Algorithm Hash digest
SHA256 61b87085684f7dde2c0bb0d831bb38ab230519b30c5d2561d60f24414a1b67fd
MD5 c4349e028c928753450c925dfd1a32b8
BLAKE2b-256 256a21fa9ea59d4d23e9b920a4c0e6a540cd46d00374a68b220e089e519ce52c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for patience_diff_algo-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 50aa63a37e136a41d11768ef82f550b3a8b3661af3001d19d7b0e767b5a91d4c
MD5 b9c2f8299824c001fc7dc572b04cb0dd
BLAKE2b-256 6fda10eff27de6b4c4167e942052c0c4bffb9abd7f7613cf1eeddaa86cb4a0b2

See more details on using hashes here.

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

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