XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/186334

Change subject: [FEAT] Add min_len to check a generators minimum length
......................................................................

[FEAT] Add min_len to check a generators minimum length

In interwiki_graph, it's not important how many elements are in there,
just that there are at least two, so it would be a waste to query more
than two. This is used in the category script to determine if it has at
least one element.

It either returns the generator for all elements if there are enough in
it, or False otherwise.

Change-Id: I7112838745660829b6b7edaa42acaccfb86a0667
---
M pywikibot/interwiki_graph.py
M pywikibot/tools.py
M scripts/category.py
3 files changed, 46 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/34/186334/1

diff --git a/pywikibot/interwiki_graph.py b/pywikibot/interwiki_graph.py
index 00fe657..604c7b2 100644
--- a/pywikibot/interwiki_graph.py
+++ b/pywikibot/interwiki_graph.py
@@ -84,10 +84,9 @@
             node.set_color('green')
             node.set_style('filled,bold')
         # if we found more than one valid page for this language:
-        # TODO: Only iterate through at most 2 elements
-        if len([p for p in self.subject.foundIn.keys()
-                if p.site == page.site and p.exists() and
-                not p.isRedirectPage()]) > 1:          # noqa
+        if pywikibot.tools.min_len((p for p in self.subject.foundIn.keys()
+                                    if p.site == page.site and p.exists() and
+                                    not p.isRedirectPage()), 2) is not False:
             # mark conflict by octagonal node
             node.set_shape('octagon')
         self.graph.add_node(node)
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index 3d45090..481ce56 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -38,6 +38,41 @@
     yield
 
 
+def min_len(generator, length=1):
+    """
+    Check whether a generator or iterator has a specific minimum length.
+
+    To get the length it has to poll the first 'length' elements. It buffers
+    the elements and those will be yielded by the new generator first. After
+    the buffered elements (which are as many as described by 'length') are
+    yielded it will query the original generator/iterator.
+
+    @param generator: the generator
+    @type generator: generator or iterable
+    @param length: the minimum length (default 1)
+    @type length: positive int (> 0)
+    @return: A new generator, if the old contained at least 'length' elements
+        and False otherwise.
+    @rtype: generator, bool
+    """
+    def gen():
+        for element in buffered:
+            yield element
+        for element in iterator:
+            yield element
+
+    if length <= 0:
+        raise TypeError('The length may be only positive')
+    iterator = iter(generator)
+    buffered = []
+    for element in iterator:
+        buffered.append(element)
+        if length <= len(buffered):
+            return gen()
+    else:
+        return False
+
+
 class UnicodeMixin(object):
 
     """Mixin class to add __str__ method in Python 2 or 3."""
diff --git a/scripts/category.py b/scripts/category.py
index 2b68625..65a7557 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -116,7 +116,7 @@
 from pywikibot import config, pagegenerators
 from pywikibot import i18n, textlib
 from pywikibot.tools import (
-    deprecated_args, deprecated, ModuleDeprecationWrapper
+    deprecated_args, deprecated, ModuleDeprecationWrapper, min_len
 )
 
 if sys.version_info[0] > 2:
@@ -927,17 +927,15 @@
     def run(self):
         cat = pywikibot.Category(self.site, self.catTitle)
 
-        empty = True
-        preloadingGen = pagegenerators.PreloadingGenerator(cat.articles())
-        for article in preloadingGen:
-            empty = False
-            pywikibot.output('')
-            pywikibot.output(u'=' * 67)
-            self.move_to_category(article, cat, cat)
-
-        if empty:
+        gen = min_len(pagegenerators.PreloadingGenerator(cat.articles()))
+        if gen is False:
             pywikibot.output(u'There are no articles or files in category %s'
                              % self.catTitle)
+        else:
+            for article in gen[0]:
+                pywikibot.output('')
+                pywikibot.output(u'=' * 67)
+                self.move_to_category(article, cat, cat)
 
 
 class CategoryTreeRobot:

-- 
To view, visit https://gerrit.wikimedia.org/r/186334
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7112838745660829b6b7edaa42acaccfb86a0667
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to