use f-strings (#10899)

* use f-strings

* add changelog frag

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2025-10-11 22:59:28 +13:00 committed by GitHub
commit 3734f471c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 50 additions and 54 deletions

View file

@ -55,10 +55,10 @@ def to_time_unit(human_time, unit='ms', **kwargs):
unit = unit_to_short_form.get(unit.rstrip('s'), unit)
if unit not in unit_factors:
raise AnsibleFilterError("to_time_unit() can not convert to the following unit: %s. "
"Available units (singular or plural): %s. "
"Available short units: %s"
% (unit, ', '.join(unit_to_short_form.keys()), ', '.join(unit_factors.keys())))
raise AnsibleFilterError((
f"to_time_unit() can not convert to the following unit: {unit}. Available units (singular or plural):"
f"{', '.join(unit_to_short_form.keys())}. Available short units: {', '.join(unit_factors.keys())}"
))
if 'year' in kwargs:
unit_factors['y'] = unit_factors['y'][:-1] + [kwargs.pop('year')]
@ -66,14 +66,14 @@ def to_time_unit(human_time, unit='ms', **kwargs):
unit_factors['mo'] = unit_factors['mo'][:-1] + [kwargs.pop('month')]
if kwargs:
raise AnsibleFilterError('to_time_unit() got unknown keyword arguments: %s' % ', '.join(kwargs.keys()))
raise AnsibleFilterError(f"to_time_unit() got unknown keyword arguments: {', '.join(kwargs.keys())}")
result = 0
for h_time_string in human_time.split():
res = re.match(r'(-?\d+)(\w+)', h_time_string)
if not res:
raise AnsibleFilterError(
"to_time_unit() can not interpret following string: %s" % human_time)
f"to_time_unit() can not interpret following string: {human_time}")
h_time_int = int(res.group(1))
h_time_unit = res.group(2)
@ -81,7 +81,7 @@ def to_time_unit(human_time, unit='ms', **kwargs):
h_time_unit = unit_to_short_form.get(h_time_unit.rstrip('s'), h_time_unit)
if h_time_unit not in unit_factors:
raise AnsibleFilterError(
"to_time_unit() can not interpret following string: %s" % human_time)
f"to_time_unit() can not interpret following string: {human_time}")
time_in_milliseconds = h_time_int * multiply(unit_factors[h_time_unit])
result += time_in_milliseconds