GithubHelp home page GithubHelp logo

isabella232 / diff_match_patch-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from npcole/diff_match_patch-python

0.0 0.0 0.0 6.78 MB

Python extension module for google-diff-match-patch's C++ implementation

License: Creative Commons Zero v1.0 Universal

C++ 67.62% Python 32.38%

diff_match_patch-python's Introduction

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

diff_match_patch-python's People

Contributors

freakboy3742 avatar joshdata avatar timonwong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.