jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/820746 )
Change subject: [IMPR] backport pairwise() from Python 3.10 ......................................................................
[IMPR] backport pairwise() from Python 3.10
Also use pairwise to iterate pairs of elements
Change-Id: I3fde479978960a7b033719ac5c5a26185c5cdd43 --- M pywikibot/backports.py M scripts/claimit.py M scripts/replace.py M scripts/template.py 4 files changed, 34 insertions(+), 18 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/backports.py b/pywikibot/backports.py index d5a0ba2..9773ac6 100644 --- a/pywikibot/backports.py +++ b/pywikibot/backports.py @@ -123,3 +123,19 @@ if string.endswith(suffix): return string[:-len(suffix)] return string + + +# bpo-38200 +if PYTHON_VERSION >= (3, 10): + from itertools import pairwise +else: + from itertools import tee + + def pairwise(iterable): + """Return successive overlapping pairs taken from the input iterable. + + .. versionadded:: 7.6 + """ + a, b = tee(iterable) + next(b, None) + return zip(a, b) diff --git a/scripts/claimit.py b/scripts/claimit.py index 3e1ac09..4fff795 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -52,6 +52,7 @@ # import pywikibot from pywikibot import WikidataBot, pagegenerators +from pywikibot.backports import pairwise
# 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 source_str, target_str in pairwise(commandline_claims): + claim = pywikibot.Claim(repo, source_str) 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..703dd5c 100755 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -154,7 +154,7 @@
import pywikibot from pywikibot import editor, fixes, i18n, pagegenerators, textlib -from pywikibot.backports import Dict, Generator, List, Pattern, Tuple +from pywikibot.backports import pairwise, Dict, Generator, List, Pattern, Tuple from pywikibot.bot import ExistingPageBot, SingleSiteBot from pywikibot.exceptions import InvalidPageError, NoPageError from pywikibot.tools import chars @@ -979,9 +979,9 @@ # 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 pairwise(commandline_replacements): + 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..58421bd 100755 --- a/scripts/template.py +++ b/scripts/template.py @@ -113,6 +113,7 @@
import pywikibot from pywikibot import i18n, pagegenerators, textlib +from pywikibot.backports import pairwise from pywikibot.bot import SingleSiteBot from pywikibot.pagegenerators import XMLDumpPageGenerator from pywikibot.tools.itertools import filter_unique, roundrobin_generators @@ -215,7 +216,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,17 +266,16 @@ return
if bool(options.get('subst', False)) ^ options.get('remove', False): - for template_name in template_names: - templates[template_name] = None + templates = {name: None for name in template_names} else: - try: - for i in range(0, len(template_names), 2): - templates[template_names[i]] = template_names[i + 1] - except IndexError: - pywikibot.output('Unless using solely -subst or -remove, ' - 'you must give an even number of template names.') + if len(template_names) % 2: + pywikibot.warning('Unless using solely -subst or -remove, you' + 'must give an even number of template names.') return
+ templates = {key: value + for key, value in pairwise(template_names)} + old_templates = [pywikibot.Page(site, template_name, ns=10) for template_name in templates]