Skip to content

Commit 0d03e64

Browse files
committed
minor difflib perf
1 parent 0f01530 commit 0d03e64

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Lib/difflib.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -638,15 +638,15 @@ def quick_ratio(self):
638638
# avail[x] is the number of times x appears in 'b' less the
639639
# number of times we've seen it in 'a' so far ... kinda
640640
avail = {}
641-
availhas, matches = avail.__contains__, 0
641+
matches = 0
642642
for elt in self.a:
643-
if availhas(elt):
643+
if elt in avail:
644644
numb = avail[elt]
645645
else:
646646
numb = fullbcount.get(elt, 0)
647647
avail[elt] = numb - 1
648648
if numb > 0:
649-
matches = matches + 1
649+
matches += 1
650650
return _calculate_ratio(matches, len(self.a) + len(self.b))
651651

652652
def real_quick_ratio(self):
@@ -702,10 +702,12 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
702702
s.set_seq2(word)
703703
for x in possibilities:
704704
s.set_seq1(x)
705-
if s.real_quick_ratio() >= cutoff and \
706-
s.quick_ratio() >= cutoff and \
707-
s.ratio() >= cutoff:
708-
result.append((s.ratio(), x))
705+
if s.real_quick_ratio() < cutoff or s.quick_ratio() < cutoff:
706+
continue
707+
708+
ratio = s.ratio()
709+
if ratio >= cutoff:
710+
result.append((ratio, x))
709711

710712
# Move the best scorers to head of list
711713
result = _nlargest(n, result)

0 commit comments

Comments
 (0)