Skip to main content

diff_match_patch-python

A Python extension module that wraps google-diff-match-patch’s C++ implementation for performing very fast string comparisons.

google-diff-match-patch is a Google library for computing differences between text files (<http://code.google.com/p/google-diff-match-patch>). Thare are implementations in various languages. Although there is a Python port, it’s slow on very large documents, and I have a need for speed. I wanted to use the C++ implementation, but I’m a Python guy so I’d prefer to use it from Python.

Google’s library depends on Qt 4, so some other folks rewrote it using the standard C++ library classes instead, making it more portable. That’s at <https://github.com/leutloff/diff-match-patch-cpp-stl>.

This project is a Python extension module for the C++ STL port so Python code can call into the native library easily. It works in both Python 2 and Python 3.

Example

First:

pip3 install diff_match_patch_python

Then write (this is Python 3):

from diff_match_patch import diff

changes = diff("Hello world.", "Goodbye moon.",
        timelimit=0, checklines=False)

for op, length in changes:
        if op == "-": print ("next", length, "characters are deleted")
        if op == "=": print ("next", length, "characters are in common")
        if op == "+": print ("next", length, "characters are inserted")

The module also provides a method called diff_bytes for performing a diff on a bytes array.

In Python 2, the methods are named diff_unicode (unicode strings) and diff_str (str strings).

The timelimit argument is the maximum running time in seconds if you want to ensure the result comes quickly. According to the Google docs, the diff will stop working after the time is exceeded and will return a valid diff, but it might not be the best one. checklines is also a Google thing and might speed up diffs that are over lined-based text like code.

The diff methods also take a counts_only argument which is True by default. Set it to False to have the returned value be an array of tuples of operations and corresponding strings rather than operations and the lengths of those strings.

Building from source

To build from these sources, you will need:

  • Python development headers (Debian package python-dev)

  • The diff-match-patch library, which you can clone using git submodule update --init.

Then build the binary module using:

python setup.py install

Or you may find pre-built binaries stored in the git repository in the build directory. Your mileage may vary with these depending on whether your system is compatible.

For package maintainers

To build everything:

git submodule update && rm -rf build && python setup.py build && python3 setup.py build

And to test without installing:

PYTHONPATH=build/lib.linux-x86_64-2.7/ python test.py
PYTHONPATH=build/lib.linux-x86_64-3.4/ python3 test.py

Release files for fast-diff-match 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fast-diff-match 1.0.2
File Size Uploaded
fast_diff_match-1.0.2.macosx-10.15-x86_64.tar.gz 49.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fast-diff-match 1.0.2
File Interpreter ABI Platform
fast_diff_match-1.0.2-cp37-cp37m-macosx_10_15_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.15+ x86-64 Details

Total release size: 100.7 kB

Release files / fast_diff_match-1.0.2.macosx-10.15-x86_64.tar.gz

Download URL fast_diff_match-1.0.2.macosx-10.15-x86_64.tar.gz
Size 49.1 kB
Tags Source
SHA-256 checksum
How to use checksums
17c92c0448b4d81446ed94e870b6285eec1461c4968bb6a6e31501e385cd2c9f
BLAKE2b-256 checksum
How to use checksums
eb44764b2a51d40fb7d49bff49ade1be33519be20bac3e14723a9fef17f94e99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.6

Release files / fast_diff_match-1.0.2-cp37-cp37m-macosx_10_15_x86_64.whl

Download URL fast_diff_match-1.0.2-cp37-cp37m-macosx_10_15_x86_64.whl
Size 51.5 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
1e9b1f0c617b676cf5ad74d0b159588081c8ee911f6d48e8354392ea856b4d32
BLAKE2b-256 checksum
How to use checksums
c5b429b20ecf7dfcb824e3188c30c1162ab37f938c27ac97ecb20868ff04cc02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.6

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page