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 (f"Operator { operator } not implemented for { 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" :
@@ -164,7 +167,10 @@ def from_opt(ctx, param, value):
164167 # E.g. `'advanced'`
165168 opt = value
166169 if opt not in configuration ._accepted ['opt' ]:
167- raise click .BadParameter ("Invalid choice `{}` (choose from {})" .format (opt , str (configuration ._accepted ['opt' ])))
170+ raise click .BadParameter (
171+ f'Invalid choice `{ opt } '
172+ f'(choose from { str (configuration ._accepted ["opt" ])} )'
173+ )
168174 return value
169175
170176 def config_blockshape (ctx , param , value ):
@@ -180,7 +186,7 @@ def config_blockshape(ctx, param, value):
180186 # 1. integers, not strings
181187 # 2. sanity check the (hierarchical) blocking shape
182188 normalized_value = []
183- for i , block_shape in enumerate ( value ) :
189+ for block_shape in value :
184190 # If hierarchical blocking is activated, say with N levels, here in
185191 # `bs` we expect to see 3*N entries
186192 bs = [int (x ) for x in block_shape .split ()]
@@ -204,7 +210,10 @@ def config_autotuning(ctx, param, value):
204210 elif value != 'off' :
205211 # Sneak-peek at the `block-shape` -- if provided, keep auto-tuning off
206212 if ctx .params ['block_shape' ]:
207- warning ("Skipping autotuning (using explicit block-shape `{}`)" .format (str (ctx .params ['block_shape' ])))
213+ warning (
214+ 'Skipping autotuning'
215+ f'(using explicit block-shape `{ str (ctx .params ["block_shape" ])} `)'
216+ )
208217 level = False
209218 else :
210219 # Make sure to always run in preemptive mode
@@ -272,8 +281,8 @@ def run(problem, **kwargs):
272281 # Note: the following piece of code is horribly *hacky*, but it works for now
273282 for i , block_shape in enumerate (block_shapes ):
274283 for n , level in enumerate (block_shape ):
275- for d , s in zip (['x' , 'y' , 'z' ], level ):
276- 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
277286
278287 solver = setup (space_order = space_order , time_order = time_order , ** kwargs )
279288 if warmup :
@@ -345,7 +354,7 @@ def run_jit_backdoor(problem, **kwargs):
345354
346355 if not os .path .exists (cfile ):
347356 # First time we run this problem, let's generate and jit-compile code
348- op .cfunction
357+ _ = op .cfunction
349358 info (f"You may now edit the generated code in `{ cfile } `. "
350359 "Then save the file, and re-run this benchmark." )
351360 return
@@ -403,9 +412,10 @@ def test(problem, **kwargs):
403412 set_log_level ('DEBUG' , comm = MPI .COMM_WORLD )
404413
405414 if MPI .COMM_WORLD .size > 1 and not configuration ['mpi' ]:
406- warning ("It seems that you're running over MPI with %d processes, but "
407- "DEVITO_MPI is unset. Setting `DEVITO_MPI=basic`..."
408- % 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+ )
409419 configuration ['mpi' ] = 'basic'
410420 except (TypeError , ModuleNotFoundError ):
411421 # MPI not available
@@ -417,8 +427,6 @@ def test(problem, **kwargs):
417427
418428 benchmark (standalone_mode = False )
419429
420- try :
430+ # In case MPI not available
431+ with suppress (TypeError ):
421432 MPI .Finalize ()
422- except TypeError :
423- # MPI not available
424- pass
0 commit comments