jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1337395?usp=email )
Change subject: Use removeprefix() and partition() instead of prefix slicing
......................................................................
Use removeprefix() and partition() instead of prefix slicing
Use removeprefix() after exact prefix checks and partition(':') to split
colon-valued script options. This makes prefixes explicit and avoids
maintaining separate character counts for extracting values.
Change-Id: Iac54ab23194f6277b3c34187e88641c4fcd58396
---
M pywikibot/data/api/_optionset.py
M pywikibot/page/_user.py
M pywikibot/textlib.py
M pywikibot/tools/__init__.py
M scripts/fixing_redirects.py
M scripts/maintenance/cache.py
M scripts/template.py
M scripts/templatecount.py
M scripts/weblinkchecker.py
9 files changed, 27 insertions(+), 24 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/data/api/_optionset.py b/pywikibot/data/api/_optionset.py
index 32219a3..25f7300 100644
--- a/pywikibot/data/api/_optionset.py
+++ b/pywikibot/data/api/_optionset.py
@@ -87,7 +87,7 @@
for type_value in site._paraminfo.parameter(module, param)['type']:
if type_value[0] == '!':
- self._valid_disable.add(type_value[1:])
+ self._valid_disable.add(type_value.removeprefix('!'))
else:
self._valid_enable.add(type_value)
if clear_invalid:
diff --git a/pywikibot/page/_user.py b/pywikibot/page/_user.py
index c48003b..24bbaa2 100644
--- a/pywikibot/page/_user.py
+++ b/pywikibot/page/_user.py
@@ -76,7 +76,7 @@
"""
self._isAutoblock = True
if title.startswith('#'):
- title = title[1:]
+ title = title.removeprefix('#')
elif ':#' in title:
title = title.replace(':#', ':')
else:
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 81647be..fc89027 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -965,7 +965,7 @@
new_label = page_title
# remove preleading ":" from the link text
if new_label[0] == ':':
- new_label = new_label[1:]
+ new_label = new_label.removeprefix(':')
new_linktrail = groups['linktrail']
if new_linktrail:
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 48e9e7d..b696713 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -510,7 +510,7 @@
raise ValueError(f'Generator string ({generator!r}) must start '
f'with "{prefix}"')
- return MediaWikiVersion(generator[len(prefix):])
+ return MediaWikiVersion(generator.removeprefix(prefix))
def __str__(self) -> str:
"""Return version number with optional suffix."""
diff --git a/scripts/fixing_redirects.py b/scripts/fixing_redirects.py
index 6b2e3aa..a6ab070 100755
--- a/scripts/fixing_redirects.py
+++ b/scripts/fixing_redirects.py
@@ -127,7 +127,7 @@
# remove preleading ":"
if link_text[0] == ':':
- link_text = link_text[1:]
+ link_text = link_text.removeprefix(':')
if link_text[0].isupper() or link_text[0].isdigit():
new_page_title = target_page.title()
else:
@@ -135,7 +135,7 @@
# remove preleading ":"
if new_page_title[0] == ':':
- new_page_title = new_page_title[1:]
+ new_page_title = new_page_title.removeprefix(':')
if new_page_title == link_text and not section \
or self.opt.overwrite:
diff --git a/scripts/maintenance/cache.py b/scripts/maintenance/cache.py
index 8849f2e..c623642 100755
--- a/scripts/maintenance/cache.py
+++ b/scripts/maintenance/cache.py
@@ -151,7 +151,7 @@
site = self.key[0:end + 1]
if site[0:5] == 'Site(':
- site = 'APISite(' + site[5:]
+ site = 'APISite(' + site.removeprefix('Site(')
username = None
login_status = None
diff --git a/scripts/template.py b/scripts/template.py
index 7d7db88..d2e5a7a 100755
--- a/scripts/template.py
+++ b/scripts/template.py
@@ -229,6 +229,7 @@
site = pywikibot.Site()
gen_factory = pagegenerators.GeneratorFactory()
for arg in local_args:
+ option, sep, value = arg.partition(':')
if arg == '-remove':
options['remove'] = True
elif arg.startswith('-subst'):
@@ -245,17 +246,17 @@
"Please enter the XML dump's filename: ")
else:
xmlfilename = arg[5:]
- elif arg.startswith('-addcat:'):
- options['addcat'] = arg[len('-addcat:'):]
- elif arg.startswith('-summary:'):
- options['summary'] = arg[len('-summary:'):]
- elif arg.startswith('-onlyuser:'):
- user = arg[len('-onlyuser:'):]
- elif arg.startswith('-skipuser:'):
- user = arg[len('-skipuser:'):]
+ elif option == '-addcat' and sep:
+ options['addcat'] = value
+ elif option == '-summary' and sep:
+ options['summary'] = value
+ elif option == '-onlyuser' and sep:
+ user = value
+ elif option == '-skipuser' and sep:
+ user = value
skip = True
- elif arg.startswith('-timestamp:'):
- timestamp = arg[len('-timestamp:'):]
+ elif option == '-timestamp' and sep:
+ timestamp = value
elif not gen_factory.handle_arg(arg):
template_name = pywikibot.Page(site, arg, ns=10)
template_names.append(template_name.title(with_ns=False))
diff --git a/scripts/templatecount.py b/scripts/templatecount.py
index 2119083..34973c1 100755
--- a/scripts/templatecount.py
+++ b/scripts/templatecount.py
@@ -147,13 +147,14 @@
namespaces = []
for arg in pywikibot.handle_args(args):
+ option, sep, value = arg.partition(':')
if arg in ('-count', '-list'):
operation = arg[1:]
- elif arg.startswith('-namespace:'):
+ elif option == '-namespace' and sep:
try:
- namespaces.append(int(arg[len('-namespace:'):]))
+ namespaces.append(int(value))
except ValueError:
- namespaces.append(arg[len('-namespace:'):])
+ namespaces.append(value)
else:
args_list.append(arg)
diff --git a/scripts/weblinkchecker.py b/scripts/weblinkchecker.py
index 11a3967..b8118e6 100755
--- a/scripts/weblinkchecker.py
+++ b/scripts/weblinkchecker.py
@@ -657,16 +657,17 @@
gen_factory = pagegenerators.GeneratorFactory()
for arg in local_args:
+ option, sep, value = arg.partition(':')
if arg == '-talk':
config.report_dead_links_on_talk = True
elif arg == '-notalk':
config.report_dead_links_on_talk = False
elif arg == '-repeat':
gen = RepeatPageGenerator()
- elif arg.startswith('-ignore:'):
- http_ignores.append(int(arg[8:]))
- elif arg.startswith('-day:'):
- config.weblink_dead_days = int(arg[5:])
+ elif option == '-ignore' and sep:
+ http_ignores.append(int(value))
+ elif option == '-day' and sep:
+ config.weblink_dead_days = int(value)
elif arg.startswith('-xmlstart'):
if len(arg) == 9:
xml_start = pywikibot.input(
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1337395?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: Iac54ab23194f6277b3c34187e88641c4fcd58396
Gerrit-Change-Number: 1337395
Gerrit-PatchSet: 4
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]