0

I'm trying to create a set of JSON configuration files for my program. I generally pass a JSON file to the command line and run my simulation. The issue is that I want to explore a lot parameters and I want to create a config files for each set of parameters. For instance, if my config file looks like this:

{
    "A": x,
    "B": "Green",
    "C": {
        "c_a": "O2",
        "c_b": y
        }
    }
}

I'd like to iterate over a set of values for each key, for instance x = [1, 2, 3], and y = [5, 6, 7]. I'd like to have a solution that user can define all parameters on top and JSONnet produce all combinations in different files.

I'm a bit familiar with the JSONnet and I know that I can have functions and basically pass different values for each key and have a new json file. But this is not very scalable, and my config file is nested which makes everything more complicated, also, this method would not really address the permutation of parameters.

I've this resolved with a shell script generating a table of permutations, and then passing each line to jsonnet as an input, but I think there should be a better way.

Edit: if there is a way to achieve this using JSONnet Python binding, that'd be ok too.

Amir
  • 1,087
  • 1
  • 9
  • 26

1 Answers1

2

Do you mean something like:

import json
import itertools

params_info = {
    "x": [ 1, 2, 3],
    "y": [ "a", "b", "c"],
    "z": [ "A", "B", "C"],
    }

for param_vals in itertools.product(*params_info.values()):
    params = dict(zip(params_info.keys(), param_vals))
    data = {
      "A": params["x"],
      "B": "Green",
      "C": {
        "c_a": "O2",
        "c_b": params["y"],
        "c_c": ["D", "E", "F", params["z"]]
      }
    }
    jsonstr = json.dumps(data) # use json.dump if you want to dump to a file
    print(jsonstr)
    # add code here to do something with json
gelonida
  • 5,327
  • 2
  • 23
  • 41
  • I cannot believe this can be done like this. I was expecting something much more complicated but this seems to be working as I wanted it. Thanks! – Amir Nov 04 '19 at 16:33
  • Basic explanation: `itertools.product` allows you to go through all combinations of a list of iterables (https://docs.python.org/2/library/itertools.html ) `params_info` is the dict describing parameters and which values should be used for which param. the first line in the for loop puts the paramters into a dict named `params` Then in the for loop you create a python object which uses `params` and directly afterwards you dump it into a json string or into a json file. Hope that explains the mechanics a little. – gelonida Nov 04 '19 at 17:02