comparison Lib/difflib.py @ 29721:09e42f79f830 2.2

Fix SF bug #763023, difflib.py: ratio() zero division not caught
author Neal Norwitz <nnorwitz@gmail.com>
date Tue, 01 Jul 2003 15:12:33 +0000
parents fc6a19f69ac7
children
comparison
equal deleted inserted replaced
29709:1f3547d4939f 29721:09e42f79f830
21 For producing human-readable deltas from sequences of lines of text. 21 For producing human-readable deltas from sequences of lines of text.
22 """ 22 """
23 23
24 __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', 24 __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
25 'Differ', 'IS_CHARACTER_JUNK', 'IS_LINE_JUNK'] 25 'Differ', 'IS_CHARACTER_JUNK', 'IS_LINE_JUNK']
26
27 def _calculate_ratio(matches, length):
28 if length:
29 return 2.0 * matches / length
30 return 1.0
26 31
27 class SequenceMatcher: 32 class SequenceMatcher:
28 33
29 """ 34 """
30 SequenceMatcher is a flexible class for comparing pairs of sequences of 35 SequenceMatcher is a flexible class for comparing pairs of sequences of
523 1.0 528 1.0
524 """ 529 """
525 530
526 matches = reduce(lambda sum, triple: sum + triple[-1], 531 matches = reduce(lambda sum, triple: sum + triple[-1],
527 self.get_matching_blocks(), 0) 532 self.get_matching_blocks(), 0)
528 return 2.0 * matches / (len(self.a) + len(self.b)) 533 return _calculate_ratio(matches, len(self.a) + len(self.b))
529 534
530 def quick_ratio(self): 535 def quick_ratio(self):
531 """Return an upper bound on ratio() relatively quickly. 536 """Return an upper bound on ratio() relatively quickly.
532 537
533 This isn't defined beyond that it is an upper bound on .ratio(), and 538 This isn't defined beyond that it is an upper bound on .ratio(), and
552 else: 557 else:
553 numb = fullbcount.get(elt, 0) 558 numb = fullbcount.get(elt, 0)
554 avail[elt] = numb - 1 559 avail[elt] = numb - 1
555 if numb > 0: 560 if numb > 0:
556 matches = matches + 1 561 matches = matches + 1
557 return 2.0 * matches / (len(self.a) + len(self.b)) 562 return _calculate_ratio(matches, len(self.a) + len(self.b))
558 563
559 def real_quick_ratio(self): 564 def real_quick_ratio(self):
560 """Return an upper bound on ratio() very quickly. 565 """Return an upper bound on ratio() very quickly.
561 566
562 This isn't defined beyond that it is an upper bound on .ratio(), and 567 This isn't defined beyond that it is an upper bound on .ratio(), and
564 """ 569 """
565 570
566 la, lb = len(self.a), len(self.b) 571 la, lb = len(self.a), len(self.b)
567 # can't have more matches than the number of elements in the 572 # can't have more matches than the number of elements in the
568 # shorter sequence 573 # shorter sequence
569 return 2.0 * min(la, lb) / (la + lb) 574 return _calculate_ratio(min(la, lb), la + lb)
570 575
571 def get_close_matches(word, possibilities, n=3, cutoff=0.6): 576 def get_close_matches(word, possibilities, n=3, cutoff=0.6):
572 """Use SequenceMatcher to return list of the best "good enough" matches. 577 """Use SequenceMatcher to return list of the best "good enough" matches.
573 578
574 word is a sequence for which close matches are desired (typically a 579 word is a sequence for which close matches are desired (typically a