jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1324204?usp=email )

Change subject: diff: Reuse similarity ratio
......................................................................

diff: Reuse similarity ratio

Store each final SequenceMatcher ratio while applying the cutoff.

Reuse it when recording the match instead of calculating it again.

Change-Id: I7402b427bd6177ef5f8016cb6d52013ce691c13b
---
M pywikibot/diff.py
M tests/diff_tests.py
2 files changed, 31 insertions(+), 4 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/diff.py b/pywikibot/diff.py
index 014c10f..e26d526 100644
--- a/pywikibot/diff.py
+++ b/pywikibot/diff.py
@@ -684,8 +684,8 @@
         s.set_seq1(x.lower() if ignorecase else x)
         if s.real_quick_ratio() >= cutoff and \
            s.quick_ratio() >= cutoff and \
-           s.ratio() >= cutoff:
-            result.append((s.ratio(), x))
+           (ratio := s.ratio()) >= cutoff:
+            result.append((ratio, x))

     # Move the best scorers to head of list
     return nlargest(n, result)
diff --git a/tests/diff_tests.py b/tests/diff_tests.py
index 6822717..f480990 100755
--- a/tests/diff_tests.py
+++ b/tests/diff_tests.py
@@ -8,13 +8,40 @@

 import unittest
 from contextlib import suppress
-from unittest.mock import patch
+from unittest.mock import Mock, patch

-from pywikibot.diff import PatchManager, cherry_pick, html_comparator
+from pywikibot.diff import (
+    PatchManager,
+    cherry_pick,
+    get_close_matches_ratio,
+    html_comparator,
+)
 from tests import join_html_data_path
 from tests.aspects import TestCase, require_modules


+class TestCloseMatchesRatio(TestCase):
+
+    """Test get_close_matches_ratio function."""
+
+    net = False
+
+    def test_ratio_called_once_per_match(self) -> None:
+        """Test that the final similarity ratio is reused."""
+        matcher = Mock()
+        matcher.real_quick_ratio.return_value = 1
+        matcher.quick_ratio.return_value = 1
+        matcher.ratio.return_value = 0.75
+        possibilities = ['one', 'two', 'three']
+
+        with patch('pywikibot.diff.SequenceMatcher', return_value=matcher):
+            result = get_close_matches_ratio(
+                'word', possibilities, cutoff=0.5)
+
+        self.assertLength(result, len(possibilities))
+        self.assertEqual(matcher.ratio.call_count, len(possibilities))
+
+
 @require_modules('bs4')
 class TestDryHTMLComparator(TestCase):


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1324204?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I7402b427bd6177ef5f8016cb6d52013ce691c13b
Gerrit-Change-Number: 1324204
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to