这是一个Python扩展模块,它封装了Google的diff_match_patch C++实现,用于非常快速的字符串比较。版本1.0.2修复了Macs上的构建问题。
项目描述
这是一个Python扩展模块,它封装了google-diff-match-patch的C++实现,用于执行非常快速的字符串比较。
google-diff-match-patch是Google的一个库,用于计算文本文件之间的差异(<http://code.google.com/p/google-diff-match-patch>)。有多种语言的实现。尽管有Python端口,但在非常大的文档上速度较慢,我需要速度。我想使用C++实现,但我是一个Python人,所以我更喜欢从Python使用它。
Google的库依赖于Qt 4,所以有些人用标准C++库类重写了它,使其更便携。这可以在 <https://github.com/leutloff/diff-match-patch-cpp-stl> 找到。
本项目是C++ STL端口的Python扩展模块,因此Python代码可以轻松调用本地库。它在Python 2和Python 3中都有效。
示例
首先
pip3 install diff_match_patch_python
然后编写(这是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")
该模块还提供了一个名为 diff_bytes 的方法,用于对字节数组执行diff操作。
在Python 2中,方法名为 diff_unicode(Unicode字符串)和 diff_str(str字符串)。
参数 timelimit 表示确保结果快速返回的最大运行时间(秒)。根据 Google 文档,超出时间限制后,diff 将停止工作,并返回一个有效的 diff,但可能不是最佳结果。参数 checklines 也是一个 Google 的特性,可能会加快基于行文本的 diff,如代码。
diff 方法还接受一个 counts_only 参数,默认值为 True。将其设置为 False,返回值将是一个包含操作和相应字符串的元组数组,而不是操作和这些字符串的长度。
从源代码构建
要从这些源代码构建,您需要
Python 开发头文件(Debian 软件包 python-dev)
diff-match-patch 库,您可以使用 git submodule update --init 进行克隆。
然后使用以下命令构建二进制模块
python setup.py install
或者,您可能会在构建目录中的 git 仓库中找到预先构建的二进制文件。这些文件与您的系统兼容性有关,因此效果可能有所不同。
对于软件包维护者
构建所有内容
git submodule update && rm -rf build && python setup.py build && python3 setup.py build
测试而不安装
PYTHONPATH=build/lib.linux-x86_64-2.7/ python test.py PYTHONPATH=build/lib.linux-x86_64-3.4/ python3 test.py
项目详情
哈希值 for fast_diff_match-1.0.2.macosx-10.15-x86_64.tar.gz
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 17c92c0448b4d81446ed94e870b6285eec1461c4968bb6a6e31501e385cd2c9f |
|
MD5 | 33c0ca83a3f2a0eab9d050d48069ae1d |
|
BLAKE2b-256 | eb44764b2a51d40fb7d49bff49ade1be33519be20bac3e14723a9fef17f94e99 |
哈希值 for fast_diff_match-1.0.2-cp37-cp37m-macosx_10_15_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1e9b1f0c617b676cf5ad74d0b159588081c8ee911f6d48e8354392ea856b4d32 |
|
MD5 | dbf42edf5c79c3b69b0787c2651fc9c9 |
|
BLAKE2b-256 | c5b429b20ecf7dfcb824e3188c30c1162ab37f938c27ac97ecb20868ff04cc02 |