jenkins-bot submitted this change.

View Change

Approvals: Matěj Suchánek: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] use tools.itertools.itergroup to iterate pairs of elements

Also add a strict parameter to itergroup function; if True a ValueError
is raised if length of iterable is not divisible by size.

Change-Id: I815f5011dde9aa77a974b189ba60eaf277a1cd8c
---
M pywikibot/tools/itertools.py
M scripts/claimit.py
M scripts/replace.py
M scripts/template.py
4 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/pywikibot/tools/itertools.py b/pywikibot/tools/itertools.py
index 397453f..7d8547d 100644
--- a/pywikibot/tools/itertools.py
+++ b/pywikibot/tools/itertools.py
@@ -1,4 +1,8 @@
-"""Iterator functions."""
+"""Iterator functions.
+
+.. note:: ``pairwise()`` function introduced in Python 3.10 is backported
+ in :mod:`backports`
+"""
#
# (C) Pywikibot team, 2008-2022
#
@@ -9,21 +13,25 @@

from contextlib import suppress
from itertools import chain, zip_longest
+from typing import Any

+from pywikibot.backports import Generator
from pywikibot.logging import debug
from pywikibot.tools import issue_deprecation_warning


__all__ = (
- 'itergroup',
- 'islice_with_ellipsis',
- 'intersect_generators',
- 'roundrobin_generators',
'filter_unique',
+ 'intersect_generators',
+ 'islice_with_ellipsis',
+ 'itergroup',
+ 'roundrobin_generators',
)


-def itergroup(iterable, size: int):
+def itergroup(iterable,
+ size: int,
+ strict: bool = False) -> Generator[Any, None, None]:
"""Make an iterator that returns lists of (up to) size items from iterable.

Example:
@@ -39,6 +47,11 @@
Traceback (most recent call last):
...
StopIteration
+
+ :param size: How many items of the iterable to get in one chunk
+ :param strict: If True, raise a ValueError if length of iterable is
+ not divisible by `size`.
+ :raises ValueError: iterable is not divisible by size
"""
group = []
for item in iterable:
@@ -47,6 +60,8 @@
yield group
group = []
if group:
+ if strict:
+ raise ValueError('iterable is not divisible by size.')
yield group


@@ -192,7 +207,7 @@
return


-def roundrobin_generators(*iterables):
+def roundrobin_generators(*iterables) -> Generator[Any, None, None]:
"""Yield simultaneous from each iterable.

Sample:
diff --git a/scripts/claimit.py b/scripts/claimit.py
index 3e1ac09..5bddc26 100755
--- a/scripts/claimit.py
+++ b/scripts/claimit.py
@@ -52,6 +52,7 @@
#
import pywikibot
from pywikibot import WikidataBot, pagegenerators
+from pywikibot.tools.itertools import itergroup


# This is required for the text that is shown when you run this script
@@ -126,15 +127,15 @@

claims = []
repo = pywikibot.Site().data_repository()
- for i in range(0, len(commandline_claims), 2):
- claim = pywikibot.Claim(repo, commandline_claims[i])
+ for property_id, target_str in itergroup(commandline_claims, 2):
+ claim = pywikibot.Claim(repo, property_id)
if claim.type == 'wikibase-item':
- target = pywikibot.ItemPage(repo, commandline_claims[i + 1])
+ target = pywikibot.ItemPage(repo, target_str)
elif claim.type == 'string':
- target = commandline_claims[i + 1]
+ target = target_str
elif claim.type == 'globe-coordinate':
coord_args = [
- float(c) for c in commandline_claims[i + 1].split(',')]
+ float(c) for c in target_str.split(',')]
if len(coord_args) >= 3:
precision = coord_args[2]
else:
diff --git a/scripts/replace.py b/scripts/replace.py
index ac97270..cdc730c 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -158,6 +158,7 @@
from pywikibot.bot import ExistingPageBot, SingleSiteBot
from pywikibot.exceptions import InvalidPageError, NoPageError
from pywikibot.tools import chars
+from pywikibot.tools.itertools import itergroup


# This is required for the text that is shown when you run this script
@@ -979,9 +980,8 @@
# The summary stored here won't be actually used but is only an example
site = pywikibot.Site()
single_summary = None
- for i in range(0, len(commandline_replacements), 2):
- replacement = Replacement(commandline_replacements[i],
- commandline_replacements[i + 1])
+ for old, new in itergroup(commandline_replacements, 2):
+ replacement = Replacement(old, new)
if not single_summary:
single_summary = i18n.twtranslate(
site, 'replace-replacing',
diff --git a/scripts/template.py b/scripts/template.py
index 7e83f71..9fc88f0 100755
--- a/scripts/template.py
+++ b/scripts/template.py
@@ -115,7 +115,12 @@
from pywikibot import i18n, pagegenerators, textlib
from pywikibot.bot import SingleSiteBot
from pywikibot.pagegenerators import XMLDumpPageGenerator
-from pywikibot.tools.itertools import filter_unique, roundrobin_generators
+from pywikibot.tools.itertools import (
+ filter_unique,
+ itergroup,
+ roundrobin_generators,
+)
+
from scripts.replace import ReplaceRobot as ReplaceBot


@@ -215,7 +220,6 @@
:param args: command line arguments
"""
template_names = []
- templates = {}
options = {}
# If xmlfilename is None, references will be loaded from the live wiki.
xmlfilename = None
@@ -266,13 +270,11 @@
return

if bool(options.get('subst', False)) ^ options.get('remove', False):
- for template_name in template_names:
- templates[template_name] = None
+ templates = dict.fromkeys(template_names)
else:
try:
- for i in range(0, len(template_names), 2):
- templates[template_names[i]] = template_names[i + 1]
- except IndexError:
+ templates = dict(itergroup(template_names, 2, strict=True))
+ except ValueError:
pywikibot.output('Unless using solely -subst or -remove, '
'you must give an even number of template names.')
return

To view, visit change 823686. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I815f5011dde9aa77a974b189ba60eaf277a1cd8c
Gerrit-Change-Number: 823686
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged