Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 106ecff

Browse files
committed
MAINT: Replace %-formatting with f-strings in remaining modules (UP031)
1 parent 4be1598 commit 106ecff

19 files changed

Lines changed: 66 additions & 70 deletions

File tree

benchmarks/benchmarks/bench_linalg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ class MatmulStrided(Benchmark):
252252

253253
def __init__(self):
254254
self.args_map = {
255-
'matmul_m%03d_p%03d_n%03d_bs%02d' % arg: arg for arg in self.args
255+
f'matmul_m{arg[0]:03}_p{arg[1]:03}_n{arg[2]:03}_bs{arg[3]:02}': arg
256+
for arg in self.args
256257
}
257258

258259
self.params = [list(self.args_map.keys())]

doc/source/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def linkcode_resolve(domain, info):
583583
fn = relpath(fn, start=dirname(numpy.__file__))
584584

585585
if lineno:
586-
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
586+
linespec = f"#L{lineno}-L{lineno + len(source) - 1}"
587587
else:
588588
linespec = ""
589589

@@ -593,8 +593,8 @@ def linkcode_resolve(domain, info):
593593
if 'dev' in numpy.__version__:
594594
return f"https://github.com/numpy/numpy/blob/main/numpy/{fn}{linespec}"
595595
else:
596-
return "https://github.com/numpy/numpy/blob/v%s/numpy/%s%s" % (
597-
numpy.__version__, fn, linespec)
596+
return (f"https://github.com/numpy/numpy/blob/v{numpy.__version__}/"
597+
f"numpy/{fn}{linespec}")
598598

599599

600600
from pygments.lexer import inherit

numpy/ctypeslib/_ctypeslib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def from_param(cls, obj):
193193
raise TypeError(f"array must have data type {cls._dtype_}")
194194
if cls._ndim_ is not None \
195195
and obj.ndim != cls._ndim_:
196-
raise TypeError("array must have %d dimension(s)" % cls._ndim_)
196+
raise TypeError(f"array must have {cls._ndim_} dimension(s)")
197197
if cls._shape_ is not None \
198198
and obj.shape != cls._shape_:
199199
raise TypeError(f"array must have shape {str(cls._shape_)}")
@@ -333,7 +333,7 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
333333
else:
334334
name = dtype.str
335335
if ndim is not None:
336-
name += "_%dd" % ndim
336+
name += f"_{ndim}d"
337337
if shape is not None:
338338
name += "_" + "x".join(str(x) for x in shape)
339339
if flags is not None:

numpy/lib/tests/test_io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,15 +1019,15 @@ def test_dtype_with_object(self):
10191019
def test_uint64_type(self):
10201020
tgt = (9223372043271415339, 9223372043271415853)
10211021
c = TextIO()
1022-
c.write("%s %s" % tgt)
1022+
c.write(f'{tgt[0]} {tgt[1]}')
10231023
c.seek(0)
10241024
res = np.loadtxt(c, dtype=np.uint64)
10251025
assert_equal(res, tgt)
10261026

10271027
def test_int64_type(self):
10281028
tgt = (-9223372036854775807, 9223372036854775807)
10291029
c = TextIO()
1030-
c.write("%s %s" % tgt)
1030+
c.write(f'{tgt[0]} {tgt[1]}')
10311031
c.seek(0)
10321032
res = np.loadtxt(c, dtype=np.int64)
10331033
assert_equal(res, tgt)
@@ -1069,7 +1069,7 @@ def test_default_float_converter_exception(self):
10691069
def test_from_complex(self):
10701070
tgt = (complex(1, 1), complex(1, -1))
10711071
c = TextIO()
1072-
c.write("%s %s" % tgt)
1072+
c.write(f'{tgt[0]} {tgt[1]}')
10731073
c.seek(0)
10741074
res = np.loadtxt(c, dtype=complex)
10751075
assert_equal(res, tgt)

numpy/linalg/_linalg.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,22 @@ def _to_native_byte_order(*arrays):
239239
def _assert_2d(*arrays):
240240
for a in arrays:
241241
if a.ndim != 2:
242-
raise LinAlgError('%d-dimensional array given. Array must be '
243-
'two-dimensional' % a.ndim)
242+
raise LinAlgError(f'{a.ndim}-dimensional array given. Array must be '
243+
'two-dimensional')
244244

245245
def _assert_stacked_2d(*arrays):
246246
for a in arrays:
247247
if a.ndim < 2:
248-
raise LinAlgError('%d-dimensional array given. Array must be '
249-
'at least two-dimensional' % a.ndim)
248+
raise LinAlgError(f'{a.ndim}-dimensional array given. Array must be '
249+
'at least two-dimensional')
250250

251251
def _assert_stacked_square(*arrays):
252252
for a in arrays:
253253
try:
254254
m, n = a.shape[-2:]
255255
except ValueError:
256-
raise LinAlgError('%d-dimensional array given. Array must be '
257-
'at least two-dimensional' % a.ndim)
256+
raise LinAlgError(f'{a.ndim}-dimensional array given. Array must be '
257+
'at least two-dimensional')
258258
if m != n:
259259
raise LinAlgError('Last 2 dimensions of the array must be square')
260260

numpy/linalg/lapack_lite/fortran.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def fortranSourceLines(fo):
104104
break
105105
yield numberingiter.lineno, ''.join(lines)
106106
else:
107-
raise ValueError("jammed: continuation line not expected: %s:%d" %
108-
(fo.name, numberingiter.lineno))
107+
raise ValueError("jammed: continuation line not expected: "
108+
f"{fo.name}:{numberingiter.lineno}")
109109

110110
def getDependencies(filename):
111111
"""For a Fortran source file, return a list of routines declared as EXTERNAL

numpy/linalg/tests/test_linalg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ def _stride_comb_iter(x):
329329
xi[...] = x
330330
xi = xi.view(x.__class__)
331331
assert_(np.all(xi == x))
332-
yield xi, "stride_" + "_".join(["%+d" % j for j in repeats])
332+
parts = [f"{j:+}" for j in repeats]
333+
yield xi, "stride_" + "_".join(parts)
333334

334335
# generate also zero strides if possible
335336
if x.ndim >= 1 and x.shape[-1] == 1:

numpy/ma/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ def masked_where(condition, a, copy=True):
19891989
(cshape, ashape) = (cond.shape, a.shape)
19901990
if cshape and cshape != ashape:
19911991
raise IndexError("Inconsistent shape between the condition and the input"
1992-
" (got %s and %s)" % (cshape, ashape))
1992+
f" (got {cshape} and {ashape})")
19931993
if hasattr(a, '_mask'):
19941994
cond = mask_or(cond, a._mask)
19951995
cls = type(a)

numpy/random/_examples/cffi/extending.py

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

3333
interface = rng.bit_generator.cffi
3434
n = 100
35-
vals_cffi = ffi.new('double[%d]' % n)
35+
vals_cffi = ffi.new(f'double[{n}]')
3636
lib.random_standard_normal_fill(interface.bit_generator, n, vals_cffi)
3737

3838
# reset the state

numpy/random/tests/test_generator_mt19937.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def test_full_range(self, endpoint):
462462
except Exception as e:
463463
raise AssertionError("No error should have been raised, "
464464
"but one was with the following "
465-
"message:\n\n%s" % str(e))
465+
f"message:\n\n{e}")
466466

467467
def test_full_range_array(self, endpoint):
468468
# Test for ticket #1690
@@ -477,7 +477,7 @@ def test_full_range_array(self, endpoint):
477477
except Exception as e:
478478
raise AssertionError("No error should have been raised, "
479479
"but one was with the following "
480-
"message:\n\n%s" % str(e))
480+
f"message:\n\n{e}")
481481

482482
def test_in_bounds_fuzz(self, endpoint):
483483
# Don't use fixed seed

0 commit comments

Comments
 (0)