jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/432721 )
Change subject: [IMPR] Add roundrobin_generators to tools ......................................................................
[IMPR] Add roundrobin_generators to tools
- It does the same as CombinedPageGenerator but instead of concatenate the generators it yields the data simultaneously. - This might replace CombinedPageGenerator when running through large generators e.g. both generators are sorted from newest to oldest which is mostly the default and bot operators are wondering why newer pages are comming after older. - This is an alternatice implementation of roundrobin generator of the itertools recipes collection. - test added.
Change-Id: I4fb4ceeafb37a46087214d865a9a37c308e25e61 --- M pywikibot/tools/__init__.py M tests/tools_tests.py 2 files changed, 34 insertions(+), 0 deletions(-)
Approvals: Zhuyifei1999: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py index 6ef05c5..9fb1117 100644 --- a/pywikibot/tools/__init__.py +++ b/pywikibot/tools/__init__.py @@ -31,10 +31,12 @@ PY2 = (PYTHON_VERSION[0] == 2)
if not PY2: + from itertools import zip_longest import queue as Queue StringTypes = basestring = (str,) UnicodeType = unicode = str else: + from itertools import izip_longest as zip_longest import Queue StringTypes = types.StringTypes UnicodeType = types.UnicodeType @@ -839,6 +841,23 @@ return
+def roundrobin_generators(*iterables): + """Yield simultaneous from each iterable. + + Sample: + >>> tuple(roundrobin_generators('ABC', range(5))) + ('A', 0, 'B', 1, 'C', 2, 3, 4) + + @param iterables: any iterable to combine in roundrobin way + @type iterables: iterable + @return: the combined generator of iterables + @rtype: generator + """ + return (item + for item in itertools.chain.from_iterable(zip_longest(*iterables)) + if item is not None) + + def filter_unique(iterable, container=None, key=None, add=None): """ Yield unique items from an iterable, omitting duplicates. diff --git a/tests/tools_tests.py b/tests/tools_tests.py index e0429c0..3c2a7dd 100644 --- a/tests/tools_tests.py +++ b/tests/tools_tests.py @@ -841,6 +841,21 @@ self.assertEqual(Foo.bar, Foo._bar)
+class TestMergeGenerator(TestCase): + + """Test merging generators.""" + + net = False + + def test_roundrobin_generators(self): + """Test merge_generators generator.""" + gen = range(5) + result = list(tools.roundrobin_generators(gen, 'ABC')) + self.assertEqual(result, [0, 'A', 1, 'B', 2, 'C', 3, 4]) + result = ''.join(tools.roundrobin_generators('HlWrd', 'e', 'lool')) + self.assertEqual(result, 'HelloWorld') + + if __name__ == '__main__': # pragma: no cover try: unittest.main()
pywikibot-commits@lists.wikimedia.org