diff options
| author | Raymond Hettinger <python@rcn.com> | 2008-01-11 03:20:54 (GMT) | 
|---|---|---|
| committer | Raymond Hettinger <python@rcn.com> | 2008-01-11 03:20:54 (GMT) | 
| commit | 0ff4dafee0025f5565cbd0ef151d385fc33d7936 (patch) | |
| tree | 276cacd42335b727f2bdd0b6f60abb4b0ffe5a4e /Lib/difflib.py | |
| parent | e896acc98c2e41dcc8db926964bdc9146ab373fe (diff) | |
| download | cpython-0ff4dafee0025f5565cbd0ef151d385fc33d7936.zip cpython-0ff4dafee0025f5565cbd0ef151d385fc33d7936.tar.gz cpython-0ff4dafee0025f5565cbd0ef151d385fc33d7936.tar.bz2  | |
Improve usability of the SequenceMatcher by returning named tuples describing match ranges.
Diffstat (limited to 'Lib/difflib.py')
| -rw-r--r-- | Lib/difflib.py | 17 | 
1 files changed, 10 insertions, 7 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py index 9be6ca7..f1c4444 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -30,9 +30,12 @@ Class HtmlDiff:  __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',             'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff', -           'unified_diff', 'HtmlDiff'] +           'unified_diff', 'HtmlDiff', 'Match']  import heapq +from collections import namedtuple as _namedtuple + +Match = _namedtuple('Match', 'a b size')  def _calculate_ratio(matches, length):      if length: @@ -363,7 +366,7 @@ class SequenceMatcher:          >>> s = SequenceMatcher(None, " abcd", "abcd abcd")          >>> s.find_longest_match(0, 5, 0, 9) -        (0, 4, 5) +        Match(a=0, b=4, size=5)          If isjunk is defined, first the longest matching block is          determined as above, but with the additional restriction that no @@ -379,13 +382,13 @@ class SequenceMatcher:          >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")          >>> s.find_longest_match(0, 5, 0, 9) -        (1, 0, 4) +        Match(a=1, b=0, size=4)          If no blocks match, return (alo, blo, 0).          >>> s = SequenceMatcher(None, "ab", "c")          >>> s.find_longest_match(0, 2, 0, 1) -        (0, 0, 0) +        Match(a=0, b=0, size=0)          """          # CAUTION:  stripping common prefix or suffix would be incorrect. @@ -452,7 +455,7 @@ class SequenceMatcher:                a[besti+bestsize] == b[bestj+bestsize]:              bestsize = bestsize + 1 -        return besti, bestj, bestsize +        return Match(besti, bestj, bestsize)      def get_matching_blocks(self):          """Return list of triples describing matching subsequences. @@ -470,7 +473,7 @@ class SequenceMatcher:          >>> s = SequenceMatcher(None, "abxcd", "abcd")          >>> s.get_matching_blocks() -        [(0, 0, 2), (3, 2, 2), (5, 4, 0)] +        [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]          """          if self.matching_blocks is not None: @@ -523,7 +526,7 @@ class SequenceMatcher:          non_adjacent.append( (la, lb, 0) )          self.matching_blocks = non_adjacent -        return self.matching_blocks +        return map(Match._make, self.matching_blocks)      def get_opcodes(self):          """Return list of 5-tuples describing how to turn a into b.  | 
