跳转到主要内容

这是一个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

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源代码分发

fast_diff_match-1.0.2.macosx-10.15-x86_64.tar.gz (49.1 kB 查看哈希值)

上传时间 源代码

构建分发

fast_diff_match-1.0.2-cp37-cp37m-macosx_10_15_x86_64.whl (51.5 kB 查看哈希值)

上传时间 CPython 3.7m macOS 10.15+ x86-64

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面