Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1341984?usp=email )
Change subject: Use lazy logging and f-strings for bot and script messages
......................................................................
Use lazy logging and f-strings for bot and script messages
Defer interpolation in three logging calls and use f-strings for
seven ordinary messages. Preserve message text, pluralization,
whitespace and timing precision.
Change-Id: Id558315871696549a6a4e2abfff19273f23191a8
---
M pywikibot/bot.py
M pywikibot/scripts/shell.py
M pywikibot/scripts/version.py
M pywikibot/scripts/wrapper.py
4 files changed, 25 insertions(+), 28 deletions(-)
Approvals:
Xqt: Verified; Looks good to me, approved
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 879d253..b963796 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -497,8 +497,8 @@
# new framework release/revision? (handle_args needs to be called first)
try:
- _log('VERSION: {}'.format(version.getversion(
- online=config.log_pywiki_repo_version).strip()))
+ _log('VERSION: %s', version.getversion(
+ online=config.log_pywiki_repo_version).strip())
except VersionParseError:
_exception()
@@ -961,20 +961,19 @@
messages.append(
'Unable to execute script because no generator was defined.')
if missing_parameters:
- messages.append('Missing parameter{s} "{params}".'
- .format(s='s' if len(missing_parameters) > 1 else '',
- params='", "'.join(missing_parameters)))
+ suffix = 's' if len(missing_parameters) > 1 else ''
+ params = '", "'.join(missing_parameters)
+ messages.append(f'Missing parameter{suffix} "{params}".')
if missing_action:
messages.append('No action defined.')
if unknown_parameters:
- messages.append('Unknown parameter{s} "{params}".'
- .format(s='s' if len(unknown_parameters) > 1 else '',
- params='", "'.join(unknown_parameters)))
+ suffix = 's' if len(unknown_parameters) > 1 else ''
+ params = '", "'.join(unknown_parameters)
+ messages.append(f'Unknown parameter{suffix} "{params}".')
if missing_dependencies:
- messages.append('Missing dependenc{s} "{deps}".'
- .format(
- s='ies' if len(missing_dependencies) > 1 else 'y',
- deps='", "'.join(missing_dependencies)))
+ suffix = 'ies' if len(missing_dependencies) > 1 else 'y'
+ deps = '", "'.join(missing_dependencies)
+ messages.append(f'Missing dependenc{suffix} "{deps}".')
if additional_text:
messages.append(additional_text.strip())
if messages:
@@ -997,10 +996,9 @@
command_log = Path(config.datafilepath('logs', 'commands.log'))
mode = 'a' if command_log.exists() else 'w'
with command_log.open(mode, encoding='utf-8') as command_log_file:
- command_log_file.write('{} r{} Python {} '
- .format(iso_date,
- version.getversiondict()['rev'],
- sys.version.split()[0]))
+ command_log_file.write(
+ f"{iso_date} r{version.getversiondict()['rev']} "
+ f'Python {sys.version.split()[0]} ')
command_log_file.write(' '.join(args) + os.linesep)
@@ -1422,8 +1420,8 @@
pywikibot.info('Execution time: ' + used)
if self.counter['read']:
- pywikibot.info('Read operation time: {:.1f} seconds'
- .format(read_seconds / self.counter['read']))
+ pywikibot.info('Read operation time: %.1f seconds',
+ read_seconds / self.counter['read'])
for op, count in self.counter.items():
if not count or op == 'read':
diff --git a/pywikibot/scripts/shell.py b/pywikibot/scripts/shell.py
index 0c3f5d8..8e613a8 100755
--- a/pywikibot/scripts/shell.py
+++ b/pywikibot/scripts/shell.py
@@ -44,8 +44,7 @@
warn_type = 'Unknown'
if args:
- print('{} arguments: {}\n' # noqa: T201
- .format(warn_type, ', '.join(args)))
+ print(f'{warn_type} arguments: {", ".join(args)}\n') # noqa: T201
# Set up interactive features such as command history.
# This is defined in the site module of the Python Standard Library,
diff --git a/pywikibot/scripts/version.py b/pywikibot/scripts/version.py
index f65f532..03c6d54 100755
--- a/pywikibot/scripts/version.py
+++ b/pywikibot/scripts/version.py
@@ -105,8 +105,8 @@
'PYWIKIBOT_NO_USER_CONFIG'])
for environ_name in sorted(settings):
pywikibot.info(
- '{}: {}'.format(environ_name,
- os.environ.get(environ_name, 'Not set') or "''"))
+ '%s: %s', environ_name,
+ os.environ.get(environ_name, 'Not set') or "''")
pywikibot.info('Config base dir: ' + pywikibot.config.base_dir)
diff --git a/pywikibot/scripts/wrapper.py b/pywikibot/scripts/wrapper.py
index b150364..4ea366d 100755
--- a/pywikibot/scripts/wrapper.py
+++ b/pywikibot/scripts/wrapper.py
@@ -220,9 +220,9 @@
else:
format_string = '\nA package necessary for {} is {}.'
print(format_string.format(script or 'pywikibot', variant))
- print('Please {} required module{} with:\n\n'
- .format('install' if variant == 'missing' else 'update',
- 's' if len(requirements) > 1 else ''))
+ action = 'install' if variant == 'missing' else 'update'
+ suffix = 's' if len(requirements) > 1 else ''
+ print(f'Please {action} required module{suffix} with:\n\n')
for requirement in requirements:
print(f" pip install \"{str(requirement).partition(';')[0]}\"\n")
@@ -489,9 +489,9 @@
if global_args: # don't use sys.argv
unknown_args = pwb.handle_args(global_args)
if unknown_args:
- print('ERROR: unknown pwb.py argument{}: {}\n'
- .format('' if len(unknown_args) == 1 else 's',
- ', '.join(unknown_args)))
+ suffix = '' if len(unknown_args) == 1 else 's'
+ print(f'ERROR: unknown pwb.py argument{suffix}: '
+ f'{", ".join(unknown_args)}\n')
return False
if not filename:
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1341984?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: Id558315871696549a6a4e2abfff19273f23191a8
Gerrit-Change-Number: 1341984
Gerrit-PatchSet: 2
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]