When trying to find optimal parameters for some program, it would be handy if the code could be automatically executed for all possible values of a parameter in a certain range, without having to manually add for loops everywhere. Let's explain:
Let prms be a dict of parameters. If each value of this dict is not a list, then the following code should be normally executed, like this:
prms = dict()
prms['param1'] = 3
prms['param2'] = 4
prms['param3'] = -17
do_something()
But if any of the parameter is a list, then the program should be re-executed for each value of the list. Example:
prms = dict()
prms['param1'] = [3, 7]
prms['param2'] = 4
prms['param3'] = [-17, 2]
should give:
temp_param1 = prms['param1'] # store the lists in temp variables
temp_param3 = prms['param3']
for prms['param1'] in temp_param1: # the following code will be executed
for prms['param3'] in temp_param3: # for all possible values of the parameters...
do_something() # execute the same code than before,
# it's transparent
prms['param1'] = temp_param1 # restore the lists
prms['param3'] = temp_param3
Is there a programming pattern / nice way to do this?
Aucun commentaire:
Enregistrer un commentaire