deltatools

Helper functions for computing deltas.

Installation

  1. Get tarball from http://return0.dyndns.org/deltatools/deltatools-0.0.1.tar.gz
  2. tar zxvf deltatools-0.0.1.tar.gz
  3. cd deltatools-0.0.1
  4. python setup.py install

You need Python 2.5 or newer.

Synopsis

>>> from deltatools import findlcs
>>> s1 = 'abcdefghijk'
>>> s2 = 'bcdegfhjjk'
>>> lcs = findlcs(s1, s2)
>>> print list(lcs)
[(1, 0), (2, 1), (3, 2), (4, 3), (5, 5), (7, 6), (9, 7), (10, 9)]
>>> from deltatools import unified_diff
>>> s1 = map(lambda c: c + '\n', list(s1))
>>> s2 = map(lambda c: c + '\n', list(s2))
>>> print ''.join(list(unified_diff(s1, s2)))
--- 	 
+++ 	 
@@ -1,10 +1,9 @@
-a
 b
 c
 d
 e
+g
 f
-g
 h
-i
 j
+j

Functions

findlcs(seq1, seq2)

Compare s1 and s2 (something iterable), return a longest common subsequence (as a generater). This function is based on "S.W.maner, G.Myers, W.Miller, ``An O(NP) Sequence Comparison Algorithm'', August 1989"

for example:

>>> seq1 = [1, 2, 3, 4, 5]
>>> seq2 = [9, 1, 2, 4]
>>> delta = findlcs(seq1, seq2)
>>> list(delta)
[(0, 1), (1, 2), (3, 3)]
normal_diff(seq1, seq2, lineterm='\n')

Compare two sequences of lines and generate normal diff.

unified_diff(seq1, seq2, fromfile='', tofile='', fromdate='', todate='', n=3, lineterm='\n')

Compare two sequences of lines and generate unified diff.

License

MIT License

Notes