-
Notifications
You must be signed in to change notification settings - Fork 42
Open
Labels
Description
Currently the config is one python file config.py
making it a Singleton-like module object:
from MCEq import config # same object everywhere
This definitely comes with the advantages:
- You can directly create python objects in the config
- You load the config once and reuse it everywhere
But it also comes with disadvantages:
- The config is an implicit global space, changing it somewhere changes it everywhere
- Hard to override in tests because it changes the config for all instances
- Multiple instances of MCEqRun share the same config. Can’t run two configs side-by-side
- No validation of the correct input types. User can do anything to break it
Proposed solution:
Change to a pydantic
config system.
from pydantic import BaseModel
class MCEqConfig(BaseModel):
kernel_config: str
e_min: float
e_max: float
standard_particles: list[int]
import yaml
from config import MCEqConfig
with open("config.yaml") as f:
data = yaml.safe_load(f)
cfg = MCEqConfig(**data)
mceq_lowE = MCEqRun(config=cfg_lowE)
mceq_single_particle= MCEqRun(config=cfg_single_particle)