I have a JSON file with
{
"t": 5550.45,
"r": 12.4
}
Now I would like to have an element with Dv= 1.0e-04*np.exp(-300000.0/r/t)
So my question is can I write Dv as a key value pair like other parameters above?
I have a JSON file with
{
"t": 5550.45,
"r": 12.4
}
Now I would like to have an element with Dv= 1.0e-04*np.exp(-300000.0/r/t)
So my question is can I write Dv as a key value pair like other parameters above?
If you accept that the value of the key is given as a string then you can leverage eval()
. Note that this opens you up to an attack given by evil data but technically this is possible. See: Why is using 'eval' a bad practice?
import numpy
data = {
"t": 5550.45,
"r": 12.4,
"dv": "1.0e-04 * numpy.exp(-300000.0/data['r']/data['t'])"
}
data["dv"] = eval(data["dv"])
print(data)
That will give you:
{'t': 5550.45, 'r': 12.4, 'dv': 1.2793161767043818e-06}
Alternatively, If you control the "json" you could potentially swap that out for a python dictionary. They look very similar. That would allow you to do something like:
import numpy
data = {
"t": 5550.45,
"r": 12.4,
"dv": lambda d : 1.0e-04 * numpy.exp(-300000.0/d['r']/d['t'])
}
data["dv"] = data["dv"](data)
print(data)