]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/lib/persistence/difference.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / lib / persistence / difference.py
1 from difflib import SequenceMatcher
2
3
4 def sequence_matcher(old, new):
5     """
6     Generates a sequence of operations using :class:`difflib.SequenceMatcher`.
7
8     :Parameters:
9         old : list( `hashable` )
10             Old tokens
11         new : list( `hashable` )
12             New tokens
13
14     Returns:
15         Minimal operations needed to convert `old` to `new`
16     """
17     sm = SequenceMatcher(None, list(old), list(new))
18     return sm.get_opcodes()
19
20
21 def apply(ops, old, new):
22     """
23     Applies operations (delta) to copy items from `old` to `new`.
24
25     :Parameters:
26         ops : list((op, a1, a2, b1, b2))
27             Operations to perform
28         old : list( `hashable` )
29             Old tokens
30         new : list( `hashable` )
31             New tokens
32     :Returns:
33         An iterator over elements matching `new` but copied from `old`
34     """
35     for code, a_start, a_end, b_start, b_end in ops:
36         if code == "insert":
37             for t in new[b_start:b_end]:
38                 yield t
39         elif code == "replace":
40             for t in new[b_start:b_end]:
41                 yield t
42         elif code == "equal":
43             for t in old[a_start:a_end]:
44                 yield t
45         elif code == "delete":
46             pass
47         else:
48             assert False, \
49                 "encounted an unrecognized operation code: " + repr(code)

Community Data Science Collective || Want to submit a patch?