Skip to content

New Design for config system #73

@StFroese

Description

@StFroese

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions