Helper functions for computing deltas.
You need Python 2.5 or newer.
>>> 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
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)]
Compare two sequences of lines and generate normal diff.
Compare two sequences of lines and generate unified diff.
MIT License