Skip to content

Instantly share code, notes, and snippets.

@DanielSzoska
Created February 9, 2013 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanielSzoska/4744663 to your computer and use it in GitHub Desktop.
Save DanielSzoska/4744663 to your computer and use it in GitHub Desktop.
>>> a = '1234aaaa56789bbbbbb0123456789ccccccc1111111dddddddddd'
>>> b = 'aa12456bbbb0123cc8cccc111111dddddd567889'
>>> import difflib
>>> s = difflib.SequenceMatcher(None, a, b)
>>> s.get_opcodes()
[('insert', 0, 0, 0, 2), ('equal', 0, 2, 2, 4), ('delete', 2, 3, 4, 4), ('equal', 3, 4, 4, 5), ('delete', 4, 8, 5, 5), ('equal', 8, 10, 5, 7), ('delete', 10, 15, 7, 7), ('equal', 15, 23, 7, 15), ('replace', 23, 27, 15, 17), ('equal', 27, 28, 17, 18), ('delete', 28, 29, 18, 18), ('equal', 29, 33, 18, 22), ('delete', 33, 37, 22, 22), ('equal', 37, 49, 22, 34), ('replace', 49, 53, 34, 40)]
>>> i = s.get_grouped_opcodes()
>>> next(i)
[('insert', 0, 0, 0, 2), ('equal', 0, 2, 2, 4), ('delete', 2, 3, 4, 4), ('equal', 3, 4, 4, 5), ('delete', 4, 8, 5, 5), ('equal', 8, 10, 5, 7), ('delete', 10, 15, 7, 7), ('equal', 15, 18, 7, 10)]
>>> next(i)
[('equal', 20, 23, 12, 15), ('replace', 23, 27, 15, 17), ('equal', 27, 28, 17, 18), ('delete', 28, 29, 18, 18), ('equal', 29, 33, 18, 22), ('delete', 33, 37, 22, 22), ('equal', 37, 40, 22, 25)]
>>> next(i)
[('equal', 46, 49, 31, 34), ('replace', 49, 53, 34, 40)]
>>> next(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment