Skip to content

Instantly share code, notes, and snippets.

@jedie
Created May 9, 2012 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedie/2647137 to your computer and use it in GitHub Desktop.
Save jedie/2647137 to your computer and use it in GitHub Desktop.
"""
http://www.python-forum.de/viewtopic.php?f=1&t=29222
"""
import difflib
from itertools import izip_longest, tee
def compare(first, second):
"""
from Hyperion
"""
def pairwise(iterable):
"""
Aus der Doku zu `itertools`
s -> (s0,s1), (s1,s2), (s2, s3), ...
"""
a, b = tee(iterable)
next(b, None)
return izip_longest(a, b)
pf, ps = map(pairwise, (first, second))
f, s = map(next, (pf, ps))
while True:
if f[0] != s[0]:
if f[1] == s[0]:
yield ("-", f[0])
f = next(pf)
continue
elif f[0] == s[1]:
yield("+", s[0])
s = next(ps)
continue
yield ("-", f[0])
yield ("+", s[0])
else:
yield (" ", f[0])
f = next(pf)
s = next(ps)
def diff(sequence_a, sequence_b):
"""
BlackJack
"""
matcher = difflib.SequenceMatcher(a=sequence_a, b=sequence_b)
for opcode, i, j, m, n in matcher.get_opcodes():
if opcode == 'equal':
yield (' ', sequence_a[i:j])
elif opcode == 'delete':
yield ('-', sequence_a[i:j])
elif opcode == 'insert':
yield ('+', sequence_b[m:n])
elif opcode == 'replace':
yield ('-', sequence_a[i:j])
yield ('+', sequence_b[m:n])
else:
assert False, 'unknown opcode %r' % opcode
def set_compare(first, second):
"""
jens
"""
result = []
for item in set(first).union(set(second)):
tag = None
if item not in first:
tag = "+"
elif item not in second:
tag = "-"
else:
tag = " "
result.append((tag, item))
return result
def main():
old = ['1', '2', '3', 'veer', '7', '8', '9']
new = ['1', '3', 'vier', '5', '6', '9']
print "difflib.ndiff():"
for line in difflib.ndiff(old, new):
if line and not line.startswith("?"):
print line
print "-"*79
print "difflib.SequenceMatcher():"
for operation, values in diff(old, new):
for value in values:
print operation, value
print "-"*79
print "compare used itertools:"
for operation, value in compare(old, new):
print operation, value
print "-"*79
print "compare used set():"
for operation, value in set_compare(old, new):
print operation, value
if __name__ == '__main__':
main()
#Output:
"""
difflib.ndiff():
1
- 2
3
- veer
+ vier
- 7
- 8
+ 5
+ 6
9
-------------------------------------------------------------------------------
difflib.SequenceMatcher():
1
- 2
3
- veer
- 7
- 8
+ vier
+ 5
+ 6
9
-------------------------------------------------------------------------------
compare used itertools:
1
- 2
3
- veer
+ vier
- 7
+ 5
- 8
+ 6
9
-------------------------------------------------------------------------------
compare used set():
- veer
1
3
- 2
+ 5
- 7
+ 6
9
- 8
+ vier
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment