-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
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'])
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels