11import os
2+ from contextlib import suppress
23
34import click
45import numpy as np
@@ -73,8 +74,10 @@ def run_op(solver, operator, **options):
7374 # Get the operator if exist
7475 try :
7576 op = getattr (solver , operator )
76- except AttributeError :
77- raise AttributeError ("Operator %s not implemented for %s" % (operator , solver ))
77+ except AttributeError as e :
78+ raise AttributeError (
79+ f"Operator { operator } not implemented for { solver } "
80+ ) from e
7881
7982 # This is a bit ugly but not sure how to make clean input creation for different op
8083 if operator == "forward" :
@@ -95,7 +98,7 @@ def run_op(solver, operator, **options):
9598 args = args [:- 1 ]
9699 return op (* args , ** options )
97100 else :
98- raise ValueError ("Unrecognized operator %s" % operator )
101+ raise ValueError (f "Unrecognized operator { operator } " )
99102
100103
101104@click .group ()
@@ -157,15 +160,17 @@ def from_opt(ctx, param, value):
157160 # E.g., `('advanced', {'par-tile': True})`
158161 value = eval (value )
159162 if not isinstance (value , tuple ) and len (value ) >= 1 :
160- raise click .BadParameter ("Invalid choice `%s ` (`opt` must be "
161- "either str or tuple)" % str ( value ) )
163+ raise click .BadParameter (f "Invalid choice `{ str ( value ) } ` (`opt` must be "
164+ "either str or tuple)" )
162165 opt = value [0 ]
163166 except NameError :
164167 # E.g. `'advanced'`
165168 opt = value
166169 if opt not in configuration ._accepted ['opt' ]:
167- raise click .BadParameter ("Invalid choice `%s` (choose from %s)"
168- % (opt , str (configuration ._accepted ['opt' ])))
170+ raise click .BadParameter (
171+ f'Invalid choice `{ opt } '
172+ f'(choose from { str (configuration ._accepted ["opt" ])} )'
173+ )
169174 return value
170175
171176 def config_blockshape (ctx , param , value ):
@@ -181,18 +186,18 @@ def config_blockshape(ctx, param, value):
181186 # 1. integers, not strings
182187 # 2. sanity check the (hierarchical) blocking shape
183188 normalized_value = []
184- for i , block_shape in enumerate ( value ) :
189+ for block_shape in value :
185190 # If hierarchical blocking is activated, say with N levels, here in
186191 # `bs` we expect to see 3*N entries
187192 bs = [int (x ) for x in block_shape .split ()]
188193 levels = [bs [x :x + 3 ] for x in range (0 , len (bs ), 3 )]
189194 if any (len (level ) != 3 for level in levels ):
190195 raise ValueError ("Expected 3 entries per block shape level, but got "
191- "one level with less than 3 entries (`%s `)" % levels )
196+ f "one level with less than 3 entries (`{ levels } `)" )
192197 normalized_value .append (levels )
193198 if not all_equal (len (i ) for i in normalized_value ):
194199 raise ValueError ("Found different block shapes with incompatible "
195- "number of levels (`%s `)" % normalized_value )
200+ f "number of levels (`{ normalized_value } `)" )
196201 configuration ['opt-options' ]['blocklevels' ] = len (normalized_value [0 ])
197202 else :
198203 normalized_value = []
@@ -205,8 +210,10 @@ def config_autotuning(ctx, param, value):
205210 elif value != 'off' :
206211 # Sneak-peek at the `block-shape` -- if provided, keep auto-tuning off
207212 if ctx .params ['block_shape' ]:
208- warning ("Skipping autotuning (using explicit block-shape `%s`)"
209- % str (ctx .params ['block_shape' ]))
213+ warning (
214+ 'Skipping autotuning'
215+ f'(using explicit block-shape `{ str (ctx .params ["block_shape" ])} `)'
216+ )
210217 level = False
211218 else :
212219 # Make sure to always run in preemptive mode
@@ -274,8 +281,8 @@ def run(problem, **kwargs):
274281 # Note: the following piece of code is horribly *hacky*, but it works for now
275282 for i , block_shape in enumerate (block_shapes ):
276283 for n , level in enumerate (block_shape ):
277- for d , s in zip (['x' , 'y' , 'z' ], level ):
278- options ['%s%d_blk%d_size' % ( d , i , n ) ] = s
284+ for d , s in zip (['x' , 'y' , 'z' ], level , strict = True ):
285+ options [f' { d } { i } _blk { n } _size' ] = s
279286
280287 solver = setup (space_order = space_order , time_order = time_order , ** kwargs )
281288 if warmup :
@@ -305,11 +312,11 @@ def run(problem, **kwargs):
305312
306313 dumpfile = kwargs .pop ('dump_norms' )
307314 if dumpfile :
308- norms = ["'%s': %f" % ( i .name , norm (i )) for i in retval [:- 1 ]
315+ norms = [f"' { i .name } ': { norm (i ):f } " for i in retval [:- 1 ]
309316 if isinstance (i , DiscreteFunction )]
310317 if rank == 0 :
311318 with open (dumpfile , 'w' ) as f :
312- f .write ("{%s}" % ', ' .join (norms ))
319+ f .write ("{{{}}}" . format ( ', ' .join (norms ) ))
313320
314321 return retval
315322
@@ -343,13 +350,13 @@ def run_jit_backdoor(problem, **kwargs):
343350 op = solver .op_fwd ()
344351
345352 # Get the filename in the JIT cache
346- cfile = "%s.c" % str (op ._compiler .get_jit_dir ().joinpath (op ._soname ))
353+ cfile = f" { str (op ._compiler .get_jit_dir ().joinpath (op ._soname ))} .c"
347354
348355 if not os .path .exists (cfile ):
349356 # First time we run this problem, let's generate and jit-compile code
350- op .cfunction
351- info ("You may now edit the generated code in `%s `. "
352- "Then save the file, and re-run this benchmark." % cfile )
357+ _ = op .cfunction
358+ info (f "You may now edit the generated code in `{ cfile } `. "
359+ "Then save the file, and re-run this benchmark." )
353360 return
354361
355362 info ("Running wave propagation Operator..." )
@@ -364,7 +371,7 @@ def _run_jit_backdoor():
364371 if dumpnorms :
365372 for i in retval [:- 1 ]:
366373 if isinstance (i , DiscreteFunction ):
367- info ("'%s': %f" % ( i .name , norm (i )) )
374+ info (f"' { i .name } ': { norm (i ):f } " )
368375
369376 return retval
370377
@@ -405,9 +412,10 @@ def test(problem, **kwargs):
405412 set_log_level ('DEBUG' , comm = MPI .COMM_WORLD )
406413
407414 if MPI .COMM_WORLD .size > 1 and not configuration ['mpi' ]:
408- warning ("It seems that you're running over MPI with %d processes, but "
409- "DEVITO_MPI is unset. Setting `DEVITO_MPI=basic`..."
410- % MPI .COMM_WORLD .size )
415+ warning (
416+ f'It seems that you are running over MPI with { MPI .COMM_WORLD .size } '
417+ 'processes, but DEVITO_MPI is unset. Setting `DEVITO_MPI=basic`...'
418+ )
411419 configuration ['mpi' ] = 'basic'
412420 except (TypeError , ModuleNotFoundError ):
413421 # MPI not available
@@ -419,8 +427,6 @@ def test(problem, **kwargs):
419427
420428 benchmark (standalone_mode = False )
421429
422- try :
430+ # In case MPI not available
431+ with suppress (TypeError ):
423432 MPI .Finalize ()
424- except TypeError :
425- # MPI not available
426- pass
0 commit comments