Skip to content

Introduce centralized configuration object #1901

@GernotMaier

Description

@GernotMaier

It is cumbersome to propagate the command line configuration throughout the code - adding a single parameter might lead to multiple hard to find changes throughout the code.

Suggest to use use a centralized configuration object with controlled mutability:

# config.py
from types import MappingProxyType

class _Config:
    def __init__(self):
        self._cfg = {}

    def load(self, args: dict):
        self._cfg = MappingProxyType(args)  # immutable view

    @property
    def data(self):
        return self._cfg

config = _Config()

and

# main.py
import argparse
from config import config

args = vars(argparse.ArgumentParser(...).parse_args())
config.load(args)

# anywhere
from config import config
print(config.data["input_file"])
``

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions