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

Change subject: tools: Avoid duplicate key evaluation in filter_unique
......................................................................

tools: Avoid duplicate key evaluation in filter_unique

Change-Id: I705b38b9ee1644e66e956b76c99ee253be7ed364
---
M pywikibot/tools/itertools.py
1 file changed, 26 insertions(+), 16 deletions(-)

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




diff --git a/pywikibot/tools/itertools.py b/pywikibot/tools/itertools.py
index 3c23276..7ad1a26 100644
--- a/pywikibot/tools/itertools.py
+++ b/pywikibot/tools/itertools.py
@@ -271,22 +271,32 @@
         container = set()

     if not add:
-        if hasattr(container, 'add'):
-            def container_add(x) -> None:
-                container.add(key(x) if key else x)
-
-            add = container_add
+        if key is None:
+            def key_getter(item):
+                return item
         else:
-            def container_setitem(x) -> None:
-                container.__setitem__(key(x) if key else x,
-                                      True)
+            key_getter = key

-            add = container_setitem
+        if hasattr(container, 'add'):
+            def add_to_container(item):
+                container.add(item)
+        else:
+            def add_to_container(item):
+                container.__setitem__(item, True)

-    for item in iterable:
-        try:
-            if (key(item) if key else item) not in container:
-                add(item)
-                yield item
-        except StopIteration:
-            return
+        for item in iterable:
+            try:
+                cmp = key_getter(item)
+                if cmp not in container:
+                    add_to_container(cmp)
+                    yield item
+            except StopIteration:
+                return
+    else:
+        for item in iterable:
+            try:
+                if (key(item) if key else item) not in container:
+                    add(item)
+                    yield item
+            except StopIteration:
+                return

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1337965?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: I705b38b9ee1644e66e956b76c99ee253be7ed364
Gerrit-Change-Number: 1337965
Gerrit-PatchSet: 4
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