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

Change subject: itertools: Process union groups lazily
......................................................................

itertools: Process union groups lazily

Use next() to retrieve the first item from each group without materializing 
every duplicate.

Add regression coverage for the lazy behavior.

Change-Id: I46af497f816a123e5f198aeef2076b32aa1b547b
---
M pywikibot/tools/itertools.py
M tests/tools_tests.py
2 files changed, 10 insertions(+), 1 deletion(-)

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




diff --git a/pywikibot/tools/itertools.py b/pywikibot/tools/itertools.py
index e9815c4..457face 100644
--- a/pywikibot/tools/itertools.py
+++ b/pywikibot/tools/itertools.py
@@ -93,7 +93,7 @@
     :return: Generator yielding all unique items in sorted order.
     """
     merged = heapq.merge(*iterables, key=key, reverse=reverse)
-    return (list(group)[0] for _, group in itertools.groupby(merged, key=key))
+    return (next(group) for _, group in itertools.groupby(merged, key=key))


 def intersect_generators(*iterables, allow_duplicates: bool = False):
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index 6f92b1a..1b09b27 100755
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -34,6 +34,7 @@
     intersect_generators,
     islice_with_ellipsis,
     roundrobin_generators,
+    union_generators,
 )
 from tests import join_xml_data_path
 from tests.aspects import TestCase
@@ -830,6 +831,14 @@
         result = ''.join(roundrobin_generators('HlWrd', 'e', 'lool'))
         self.assertEqual(result, 'HelloWorld')

+    def test_union_generators_is_lazy(self) -> None:
+        """Test that duplicate groups are processed lazily."""
+        source = iter([1, 1, 1, 2])
+        generator = union_generators(source)
+
+        self.assertEqual(next(generator), 1)
+        self.assertEqual(next(source), 1)
+

 class TestIsIpAddress(TestCase):


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