1 from difflib import SequenceMatcher
4 def sequence_matcher(old, new):
6 Generates a sequence of operations using :class:`difflib.SequenceMatcher`.
9 old : list( `hashable` )
11 new : list( `hashable` )
15 Minimal operations needed to convert `old` to `new`
17 sm = SequenceMatcher(None, list(old), list(new))
18 return sm.get_opcodes()
21 def apply(ops, old, new):
23 Applies operations (delta) to copy items from `old` to `new`.
26 ops : list((op, a1, a2, b1, b2))
28 old : list( `hashable` )
30 new : list( `hashable` )
33 An iterator over elements matching `new` but copied from `old`
35 for code, a_start, a_end, b_start, b_end in ops:
37 for t in new[b_start:b_end]:
39 elif code == "replace":
40 for t in new[b_start:b_end]:
43 for t in old[a_start:a_end]:
45 elif code == "delete":
49 "encounted an unrecognized operation code: " + repr(code)