Registry / serialization / diff-match-patch

diff-match-patch

JSON →
library20241021pypypi✓ verified 25d ago

Google's Diff Match and Patch libraries offer robust algorithms for synchronizing plain text, including diffing two texts, finding fuzzy matches for a pattern, and applying patches. Originally developed for Google Docs, this Python package provides a modern, actively maintained wrapper around the core algorithms. It's suitable for comparing texts, showing differences, and applying changes.

pip install diff-match-patch
INSTALL
IMPORT
SIG · DIFF-MATCH-PATCH
D
diff-match-patch
serializationpythonv20241021
Install
1.7s avg
Import
12ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v20241021 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.014s · 18.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.010s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

diff_match_patch
from diff_match_patch import diff_match_patch

This quickstart demonstrates the core functionalities: computing differences between two texts, generating a patch from these differences, and then applying that patch to an original text. The `diff_cleanupSemantic` method is optionally used to improve readability of the diff output.

from diff_match_patch import diff_match_patch # Initialize the diff_match_patch object dmp = diff_match_patch() text1 = "The quick brown fox jumps over the lazy dog." text2 = "A quick black fox jumps over the active cat." # 1. Compute a diff diffs = dmp.diff_main(text1, text2) # Optional: Clean up the diff for semantic readability dmp.diff_cleanupSemantic(diffs) print(f"Computed Diffs: {diffs}") # Expected output example: [(-1, 'The'), (1, 'A'), (0, ' quick '), (-1, 'brown'), (1, 'black'), (0, ' fox jumps over the '), (-1, 'lazy dog'), (1, 'active cat'), (0, '.')] # 2. Generate a patch from the diffs patches = dmp.patch_make(text1, text2, diffs) patch_text = dmp.patch_toText(patches) print(f"\nGenerated Patch: {patch_text}") # 3. Apply the patch to an original text # Let's simulate applying it to text1 to get text2 new_text, results = dmp.patch_apply(patches, text1) print(f"\nApplied Patch (New Text): {new_text}") print(f"Patch Application Results: {results}")
Debug
Known issues
gotchaThe original Google diff-match-patch project was archived in August 2024. This `diff-match-patch` PyPI package now tracks a community-maintained fork. Users should be aware that Google no longer actively maintains the original repository.
fix
Continue using this `diff-match-patch` PyPI package, as it is designed to track the actively maintained fork. Monitor the PyPI project page for updates on the new upstream source.
affects: All versions since August 2024
gotchaWhen using `patch_fromText()` with unidiff strings, issues with line breaks (`%0A`) can occur if the patch string was not originally generated by `dmp.patch_make()`. The library might interpret line breaks differently, leading to incorrect patch application.
fix
Ensure that any patch string fed into `dmp.patch_fromText()` is either generated by `dmp.patch_make()` or carefully formatted to match its expected unidiff string representation, especially concerning line endings.
affects: All versions
gotchaThe line-diffing algorithm, when used for performance optimization (e.g., in `diff_main` with `checklines=True`), can produce incorrect patches for files exceeding approximately 65,536 lines. This is due to a limitation in mapping lines to 16-bit Unicode characters, causing an overflow.
fix
For very large files (tens of thousands of lines or more), consider splitting the input into smaller chunks, or disable line-mode optimizations if precise diffing is critical and performance is secondary. Reportedly, the issue stems from an ES5 limitation, but the Python wrapper might inherit this behavior.
affects: All versions
gotcha`diff_cleanupSemantic()` improves human readability but uses heuristics. It may not provide semantically perfect or 'correct' differences for all text types, especially in complex natural language processing contexts, as it relies on surface patterns rather than deep linguistic analysis.
fix
Understand that `diff_cleanupSemantic()` is for presentation. For applications requiring strict, byte-level, or programmatically consistent diffs, consider using the raw output of `diff_main()` or applying other cleanup methods like `diff_cleanupEfficiency()` if appropriate.
affects: All versions
gotchaDiff computations, especially for large or complex texts, can be time-consuming. The `Diff_Timeout` property (defaulting to 1.0 second) can prematurely terminate the 'exploration phase' of a diff, leading to potentially suboptimal or incomplete results.
fix
Adjust `dmp.Diff_Timeout` to a higher value (e.g., `dmp.Diff_Timeout = 0` for no timeout, or a larger number of seconds) if computation time is acceptable and more accurate, exhaustive diffs are required.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'diff_match_patch'
The Python package `diff-match-patch` is either not installed, or the import statement uses an incorrect module name. While the package name on PyPI is `diff-match-patch`, the actual module to import is `diff_match_patch`.
fix
Ensure the library is installed using `pip install diff-match-patch`, and then import it with `from diff_match_patch import diff_match_patch`.
AttributeError: module 'diff_match_patch' has no attribute 'diff'
The `diff_match_patch` module does not expose a top-level function or attribute named 'diff'. Diffing operations are methods of an instantiated `diff_match_patch` object.
fix
First, import and instantiate the `diff_match_patch` class, then call its methods for diffing. For example: `from diff_match_patch import diff_match_patch; dmp = diff_match_patch(); diffs = dmp.diff_main(text1, text2)`.
ImportError: cannot import name diff from 'diff_match_patch'
This error occurs when attempting to directly import a function named 'diff' from the `diff_match_patch` module. The module does not expose a standalone 'diff' function; instead, diffing functionality is provided via methods of the `diff_match_patch` class.
fix
Import the `diff_match_patch` class itself, instantiate it, and then use its `diff_main` method. For example: `from diff_match_patch import diff_match_patch; dmp = diff_match_patch(); diffs = dmp.diff_main(text1, text2)`.
Upgrade
Version history
20241021latest on PyPI · released Oct 21, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
diff-match-patch — pip install diff-match-patch · libregistry