Assume that I have data in a json-file that looks like this:
"params" : {
"main" : {
"type_A":[
[
{"name" : "param1", "value" : 0.000005, "limits" : [0, 0.1], "vary" : 1, "deviation" : [0.1, 0.2], "use_limits": 1},
{"name" : "param2", "value" : -0.00012, "limits" : [-0.1, 0.1], "vary" : 1, "deviation" : [0.1, 0.2], "use_limits": 1},
{"name" : "param3", "value" : 0, "limits" : [0, 1], "vary" : 0, "deviation" : [0.1, 0.1], "use_limits": 0}
]
]
},
"background" : {
"type_B" : [
[
{"name" : "param1", "value" : 0.0985, "limits" : [0, 0.1], "vary" : 0, "deviation" : [0.1, 0.1], "use_limits": 0},
{"name" : "param2", "value" : 0.015, "limits" : [0, 1], "vary" : 0, "deviation" : [0.1, 0.2], "use_limits": 0}
],
[
{"name" : "param1", "value" : 0.0985, "limits" : [0, 0.1], "vary" : 0, "deviation" : [0.1, 0.1], "use_limits": 0},
{"name" : "param2", "value" : 0.015, "limits" : [0, 1], "vary" : 0, "deviation" : [0.1, 0.2], "use_limits": 0}
]
]
},
"others" : {
"type_C" :[
[
{ "name" : "0", "value": 0, "limits" : [-1e20, 1e20], "vary" : 1, "deviation" : [0.1, 0.1], "use_limits" : 0 },
{ "name" : "1", "value": 0, "limits" : [-1e20, 1e20], "vary" : 1, "deviation" : [0.1, 0.1], "use_limits" : 0 },
{ "name" : "2", "value": 0, "limits" : [-1e20, 1e20], "vary" : 1, "deviation" : [0.1, 0.1], "use_limits" : 0 }
]
]
}
}
After loading in the data in Python, I can access it over data["params"]
, which returns the above data as a dictionary with the 3 keys main, background
and others
.
I need to loop over this data regularly. These loops can look as straightforward as
for kind in data["params"]:
for type_ in data["params"][kind]:
for idx, line in enumerate(data["params"][kind][type_]):
for parameter in line:
f(line[parameter], idx)
but they can sometimes also be a bit more involved, e.g.
for kind in data["params"]:
if kind != "others":
for type_ in data["params"][kind]:
some_variable[kind] = {type_: []}
for k, line in enumerate(data["params"][kind][type_]):
for parameter in line:
some_variable[kind][type_][k][parameter] = f(line[parameters])
These loops can get pretty messy and hard to read (i.e. understand the logic...), which obviously makes them also hard to maintain. Is there a way to clean this up? Maybe with itertools
or trying a more modular approach using iterators directly?