C implementation of parts of difflib
Low maintenance activity. Last release 23 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install cdifflibcdifflib
Python difflib sequence matcher reimplemented in C.
Actually only contains reimplemented parts. Creates a CSequenceMatcher type
which inherets most functions from difflib.SequenceMatcher.
cdifflib is about 4x the speed of the pure python difflib when diffing
large streams.
Limitations
The C part of the code can only work on list rather than generic iterables,
so anything that isn't a list will be converted to list in the
CSequenceMatcher constructor. This may cause undesirable behavior if you're
not expecting it.
Works with Python 2.7 and 3.6 (Should work on all 3.3+)
Usage
Can be used just like the difflib.SequenceMatcher as long as you pass lists. These examples are right out of the difflib docs:
>>> from cdifflib import CSequenceMatcher
>>> s = CSequenceMatcher(None, ' abcd', 'abcd abcd')
>>> s.find_longest_match(0, 5, 0, 9)
Match(a=1, b=0, size=4)
>>> s = CSequenceMatcher(lambda x: x == " ",
... "private Thread currentThread;",
... "private volatile Thread currentThread;")
>>> print round(s.ratio(), 3)
0.866
It's completely compatible, so you can replace the difflib version on startup and then other libraries will use CSequenceMatcher too, eg:
from cdifflib import CSequenceMatcher
import difflib
difflib.SequenceMatcher = CSequenceMatcher
import library_that_uses_difflib
# Now the library will transparantely be using the C SequenceMatcher - other
# things remain the same
library_that_uses_difflib.do_some_diffing()
Making
Set up a venv:
python -m venv .venv
source .venv/bin/activate
To install:
python -m build
To test:
python -m pytest tests/cdifflib_tests.py
License etc
This code lives at https://github.com/mduggan. See LICENSE for the license.