Skip to content

Commit 6ce5540

Browse files
authored
Replace all %-substitution and .format() calls with f-strings (#506)
Eliminate all old-style string replacement (`%` substitutions) and explicit `.format()` calls, in favor of equivalent f-strings.
1 parent 2c67877 commit 6ce5540

File tree

18 files changed

+57
-65
lines changed

18 files changed

+57
-65
lines changed

doc/intro.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ Handlers are given a `progress` object containing a number of useful fields.
333333
For example::
334334

335335
def eval_handler(image, progress):
336-
print('run time so far (secs) = {}'.format(progress.run))
337-
print('estimated time of arrival (secs) = {}'.format(progress.eta))
338-
print('total number of pels to process = {}'.format(progress.tpels))
339-
print('number of pels processed so far = {}'.format(progress.npels))
340-
print('percent complete = {}'.format(progress.percent))
336+
print(f' run = {progress.run} (seconds of run time)')
337+
print(f' eta = {progress.eta} (estimated seconds left)')
338+
print(f' tpels = {progress.tpels} (total number of pels)')
339+
print(f' npels = {progress.npels} (number of pels computed so far)')
340+
print(f' percent = {progress.percent} (percent complete)')
341341

342342
Use :meth:`.Image.set_kill` on the image to stop computation early.
343343

examples/pil-numpy-pyvips.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99

1010
if len(sys.argv) != 3:
11-
print('usage: {0} input-filename output-filename'.format(sys.argv[0]))
11+
print(f'usage: {sys.argv[0]} input-filename output-filename')
1212
sys.exit(-1)
1313

1414

examples/progress.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def progress_print(name, progress):
7-
print(f'signal {name}:'.format(name))
7+
print(f'signal {name}:')
88
print(f' run = {progress.run} (seconds of run time)')
99
print(f' eta = {progress.eta} (estimated seconds left)')
1010
print(f' tpels = {progress.tpels} (total number of pels)')

examples/soak-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
pyvips.cache_set_max(0)
88

99
for i in range(1000):
10-
print("loop {0} ...".format(i))
10+
print(f"loop {i} ...")
1111
im = pyvips.Image.new_from_file(sys.argv[1])
1212
im = im.embed(100, 100, 3000, 3000, extend="mirror")
1313
im.write_to_file("x.v")

examples/try5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def should_equal(test, a, b):
1313
if abs(a - b) > 0.01:
14-
print('%s: seen %g and %g' % (test, a, b))
14+
print(f'{test}: seen {a:g} and {b:g}')
1515
sys.exit(1)
1616

1717

pyvips/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@
3232
lib_minor = vips_lib.vips_version(1)
3333
wrap_major = vips_lib.VIPS_MAJOR_VERSION
3434
wrap_minor = vips_lib.VIPS_MINOR_VERSION
35-
logger.debug('Module generated for libvips %s.%s' %
36-
(wrap_major, wrap_minor))
37-
logger.debug('Linked to libvips %s.%s' % (lib_major, lib_minor))
35+
logger.debug(f'Module generated for libvips {wrap_major}.{wrap_minor}')
36+
logger.debug(f'Linked to libvips {lib_major}.{lib_minor}')
3837

3938
if wrap_major != lib_major or wrap_minor != lib_minor:
4039
raise Exception('bad wrapper version')
4140

4241
API_mode = True
4342

4443
except Exception as e:
45-
logger.debug('Binary module load failed: %s' % e)
44+
logger.debug(f'Binary module load failed: {e}')
4645
logger.debug('Falling back to ABI mode')
4746

4847
from cffi import FFI
@@ -141,14 +140,14 @@ class GLogLevelFlags(object):
141140
@ffi.def_extern()
142141
def _log_handler_callback(domain, level, message, user_data):
143142
logger.log(GLogLevelFlags.LEVEL_TO_LOGGER[level],
144-
'{0}: {1}'.format(_to_string(domain), _to_string(message)))
143+
f'{_to_string(domain)}: {_to_string(message)}')
145144

146145
# keep a ref to the cb to stop it being GCd
147146
_log_handler_cb = glib_lib._log_handler_callback
148147
else:
149148
def _log_handler_callback(domain, level, message, user_data):
150149
logger.log(GLogLevelFlags.LEVEL_TO_LOGGER[level],
151-
'{0}: {1}'.format(_to_string(domain), _to_string(message)))
150+
f'{_to_string(domain)}: {_to_string(message)}')
152151

153152
# keep a ref to the cb to stop it being GCd
154153
_log_handler_cb = ffi.callback('GLogFunc', _log_handler_callback)

pyvips/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self, message, detail=None):
6868
logger.debug('Error %s %s', self.message, self.detail)
6969

7070
def __str__(self):
71-
return '{0}\n {1}'.format(self.message, self.detail)
71+
return f'{self.message}\n {self.detail}'
7272

7373

7474
__all__ = [

pyvips/gobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def new_pointer_from_gtype(gtype):
159159

160160
pointer = gobject_lib.g_object_new(gtype, ffi.NULL)
161161
if pointer == ffi.NULL:
162-
raise Error("can't create {0}".format(type_name(gtype)))
162+
raise Error(f"can't create {type_name(gtype)}")
163163

164164
return pointer
165165

@@ -176,7 +176,7 @@ def signal_connect(self, name, callback):
176176
"""
177177

178178
if name not in _marshalers:
179-
raise Error('unsupported signal "{0}"'.format(name))
179+
raise Error(f'unsupported signal "{name}"')
180180

181181
go = ffi.cast('GObject *', self.pointer)
182182
handle = ffi.new_handle(callback)

pyvips/gvalue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def gtype_to_python(gtype):
8484
name = type_name(gtype)
8585
if name.startswith('Vips'):
8686
name = name[4:]
87-
return "Union[str, %s]" % name
87+
return f"Union[str, {name}]"
8888
if gtype in GValue._gtype_to_python:
8989
return GValue._gtype_to_python[gtype]
9090
if fundamental in GValue._gtype_to_python:

pyvips/pyvips_build.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
''' if major == 8 and minor < 6 else ''
3333

3434
ffibuilder.set_source("_libvips",
35-
r"""
35+
f"""
3636
#include <vips/vips.h>
37-
""" + compat,
37+
{compat}
38+
""",
3839
**pkgconfig.parse('vips'))
3940

4041
features = {

0 commit comments

Comments
 (0)