Best Practice to run python script with multiple variations of config script

Viewed 36

I am regularly running into the following situation: I have a script config.py that holds various parameters. I then run another script.py that imports the parameters from the config.py and performs some operations. At some point I wish to run script.py with multiple variations of config.py. My current approach is to transform script.py into a function and apply it to each desired parameter combination. For that I need to create another script map_script.py which adds boilerplate code.

Minimal Example for Illustration:

config.py

PARAMETER = 1

script.py

import config as conf
print(conf.PARAMETER**2)

map_script.py

desired_configurations = [1,2,3]
def map_function(PARAMETER):
    print(PARAMETER**2)

for PARAMETER in desired_configurations:
    map_function(PARAMETER)

I would be interested in the most elegant way to handle this given that such a config.py and script.py already exist. Furthermore I would be interested if it is considered best practice to set it up this way or if one should e.g. aim to formulate the script.py as a function in the first place.

Edit: I want to vary a certain subset of the parameters in the config file. So creating a separate config file for each combination seems also to cumbersome

1 Answers

The best practice would be to have a file config.json file containing all your parameter choices. Here is an example:

{"desired_configurations": [1,2,3]}

and then a single script.pt like this:

import json
desired_configurations = json.load(open('config.json', 'r'))['desired_configurations']

def map_function(PARAMETER):
    print(PARAMETER**2)


for PARAMETER in desired_configurations:
    map_function(PARAMETER)
Related