You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice to have a module or a function that reads in the config file and sets all the necessary defaults in the same place, deals with missing entries or wrong values etc..
Something like this, but better adapted to our needs.
import json
class Config:
def __init__(self, config_file):
self.defaults = {
"detector": "XAMS",
"nevents": 10000,
"nphoton_per_event": [100, 100000],
"photon_zgen": 0.5,
"geometry": {
"type": "cylinder",
"radius": 3.2,
"ztop": 1.2,
"zliq": 0.0,
"zbot": -6.7,
"ptfe_zmin": -5.32,
"ptfe_zmax": -0.25
},
"npmt_xy": 2,
"pmt": {
"type": "square",
"size": 2.54,
"ndivs": 10
},
"scatter": True,
"experimental_scatter_model": True
}
with open(config_file, 'r') as f:
self.config = json.load(f)
self._validate_and_set_defaults()
def _validate_and_set_defaults(self):
for key, value in self.defaults.items():
if key not in self.config:
self.config[key] = value
elif isinstance(value, dict):
for subkey, subvalue in value.items():
if subkey not in self.config[key]:
self.config[key][subkey] = subvalue
def get(self, key, default=None):
return self.config.get(key, default)
def __getitem__(self, key):
return self.config[key]
def __setitem__(self, key, value):
self.config[key] = value
def __contains__(self, key):
return key in self.config
# Usage
config = Config('path_to_config_file.json')
print(config.get('detector'))
print(config['geometry']['type'])
The text was updated successfully, but these errors were encountered:
So this would be to make sure that all necessary parameters are defined? Could be useful, but if things are field in automatically you may also miss essentials and set them to some crazy default.
As an alternative we could build in some checks after reading in the config? If parameters are missing it should throw an exception and stop running
Yess I agree. I do not want to have all the keys to have a defualt, and we can raise errors where they are needed etc.. but it's nice to have this done in only one place for the entire package, so you make sure that you don't have different values as default in different parts of the code or stuff like this
It would be nice to have a module or a function that reads in the config file and sets all the necessary defaults in the same place, deals with missing entries or wrong values etc..
Something like this, but better adapted to our needs.
The text was updated successfully, but these errors were encountered: