fast_diff_match_patch

raw JSON →
2.1.0 verified Fri May 01 auth: no python

Packages the C++ implementation of google-diff-match-patch for Python, providing fast byte and string diffs, match, and patch operations. Current version 2.1.0 is the C++ implementation, replacing an earlier pure Python version (1.x). Release cadence is low; version 2.0 introduced the C++ backend.

pip install fast-diff-match-patch
error ImportError: cannot import name 'diff_match_patch' from 'fast_diff_match_patch'
cause Version 2.x removed the class-based API. The library now provides standalone functions (diff, match_main, patch_apply).
fix
Change import to from fast_diff_match_patch import diff and use diff(text1, text2) instead of dmp.diff_main(text1, text2).
error TypeError: 'module' object is not callable
cause Calling `fast_diff_match_patch.diff(...)` without importing the function directly, or mistaking the module for a class.
fix
Use the correct import: from fast_diff_match_patch import diff and call diff(...).
breaking Version 2.0+ replaced the pure-Python implementation with a C++ wrapper. The API changed from a class-based interface (diff_match_patch) to standalone functions (diff, match_main, patch_apply). Old code using diff_match_patch will break.
fix Replace `from fast_diff_match_patch import diff_match_patch; dmp = diff_match_patch(); dmp.diff_main(...)` with `from fast_diff_match_patch import diff; diff(...)`.
deprecated Version 1.x is deprecated and no longer maintained. Upgrade to 2.x for the faster C++ backend.
fix Run `pip install --upgrade fast-diff-match-patch` and update imports as above.
gotcha The output of `diff` is a list of tuples `(operation, text)` where operation is `-1` (delete), `0` (equal), or `1` (insert). This differs from the original google-diff-match-patch library which sometimes uses enum-like constants.
fix Use integer comparison: if op == -1: # delete; elif op == 0: # equal; elif op == 1: # insert.

Demonstrates the three main functions: diff, match_main, patch_apply. Note that the API changed from the class-based interface in version 1.x.

from fast_diff_match_patch import diff, match_main, patch_apply

# Diff two strings
text1 = "The quick brown fox"
text2 = "The quick blue fox"
diffs = diff(text1, text2)
print(diffs)

# Match a pattern in text
match_result = match_main(text1, "quick", 0)
print(match_result)

# Apply patches from a diff text
patches = patch_apply(diff(text1, text2), text1)
print(patches)