Skip to content

Config

Handle config files in Nexus-e.

This module's purpose is to provide an easy access to the variables stored in a config file. The ConfigFile interface declare methods to load and write a dictionary from a given file. The TomlFile class implements this interface for .toml files.

To avoid having to use this dictionary directly, the module provides a Config class that can conveniently parse and be exported to a dictionary. The config variables can then be accessed via the Config class attributes.

The load(), write() and write_default_config_file() functions of this module can be used to handle a Config object with a pre-defined ConfigFile object.

Example

Write a default config file and update a config variable value:

import config
config.write_default_config_file()
settings = config.load()
settings.logging.filename = "alternative.log"
config.write(settings)

CONFIG_FILE_NAME module-attribute

CONFIG_FILE_NAME = 'config.toml'

The default config file name.

Config dataclass

Config(
    logging: Logging = Logging(),
    results: Results = Results(),
    data_context: DataContext = DataContext(),
    modules: Modules = Modules(),
)

Define the config variables' structure.

To add a new config variable you can add it as an attribute of an existing or a new dataclass that is itself an attribute of this Config class.

Example

Add a new config variable and a new dataclass:

@dataclass
class NewConfigSection:
    new_config_variable: str = "default_variable_value"

@dataclass
class Config:
    new_config_section: NewConfigSection = field(default_factory=NewConfigSection)

The default value of the config variable is given by the default value of its corresponding dataclass attribute.

data_context class-attribute instance-attribute

data_context: DataContext = field(default_factory=DataContext)

logging class-attribute instance-attribute

logging: Logging = field(default_factory=Logging)

modules class-attribute instance-attribute

modules: Modules = field(default_factory=Modules)

results class-attribute instance-attribute

results: Results = field(default_factory=Results)

parse

parse(**config: dict)

Populate the Config class with an input dictionary.

Source code in src/nexus_e/config.py
def parse(self, **config: dict):
    """Populate the Config class with an input dictionary."""
    for key in config.keys():
        if isinstance(config[key], dict):
            config_class_name = "".join(
                word.capitalize() for word in key.split("_")
            )
            setattr(self, key, eval(config_class_name)(**config[key]))

Logging dataclass

Logging(
    filename: str = "nexus-e.log",
    filemode: str = "w",
    format: str = "%(asctime)s %(levelname)s %(message)s",
    date_format: str = "%Y-%m-%d %H:%M:%S",
    level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO",
)

Config section about the logger's parameters.

date_format class-attribute instance-attribute

date_format: str = '%Y-%m-%d %H:%M:%S'

See how to build a date format string here.

filemode class-attribute instance-attribute

filemode: str = 'w'

filename class-attribute instance-attribute

filename: str = 'nexus-e.log'

format class-attribute instance-attribute

format: str = '%(asctime)s %(levelname)s %(message)s'

See how to build a log record format string here.

level class-attribute instance-attribute

level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] = 'INFO'

The logging level can be given as a string. More here.

Results dataclass

Results(
    base_folder: str = "Results",
    create_new_simulation_results_folder: bool = True,
)

Represents configuration settings for simulation results management.

base_folder class-attribute instance-attribute

base_folder: str = 'Results'

The base directory where simulation results are stored.

create_new_simulation_results_folder class-attribute instance-attribute

create_new_simulation_results_folder: bool = True

Determines whether a new folder should be created for each simulation's results.

DataContext dataclass

DataContext(
    type: Literal["mysql", "sqlite"] = "mysql",
    name: str = "",
    host: str = "",
    port: str = "",
    user: str = "",
    password: str = "",
)

host class-attribute instance-attribute

host: str = ''

name class-attribute instance-attribute

name: str = ''

password class-attribute instance-attribute

password: str = ''

port class-attribute instance-attribute

port: str = ''

type class-attribute instance-attribute

type: Literal['mysql', 'sqlite'] = 'mysql'

user class-attribute instance-attribute

user: str = ''

Modules dataclass

Modules(
    commons: dict[str, Any] = (
        lambda: {"resolution_in_days": 8, "single_electric_node": True}
    )(),
    playlist_name: str = "end_to_end_test",
)

Represents configuration settings for all modules.

commons class-attribute instance-attribute

commons: dict[str, Any] = field(
    default_factory=lambda: {
        "resolution_in_days": 8,
        "single_electric_node": True,
    }
)

The extra parameters passed to every Module.

playlist_name class-attribute instance-attribute

playlist_name: str = 'end_to_end_test'

The file defining the modules to load and their execution order.

Module dataclass

Module(name: str = '', parameters: dict = dict())

Represents a module configuration with its name and its associated parameters.

name class-attribute instance-attribute

name: str = ''

The name of the module to import.

parameters class-attribute instance-attribute

parameters: dict = field(default_factory=dict)

A dictionary containing the module's specific parameters.

ConfigFile

ConfigFile(file_path: str)

Bases: ABC

Interface for loading from and writing in a config file.

Source code in src/nexus_e/config.py
@abstractmethod
def __init__(self, file_path: str):
    """Store the file path in an object's attribute."""
    pass

load abstractmethod

load() -> dict

Load the full content of the config file as a dictionary.

Source code in src/nexus_e/config.py
@abstractmethod
def load(self) -> dict:
    """Load the full content of the config file as a dictionary."""
    pass

write abstractmethod

write(config: dict)

Fill the config file with the content of a dictionary.

Source code in src/nexus_e/config.py
@abstractmethod
def write(self, config: dict):
    """Fill the config file with the content of a dictionary."""
    pass

TomlFile

TomlFile(file_path: str)

Bases: ConfigFile

Implement the ConfigFile interface for .toml files.

Source code in src/nexus_e/config.py
def __init__(self, file_path: str):
    self.__config_file_path = file_path

load

load() -> dict
Source code in src/nexus_e/config.py
def load(self) -> dict:
    with open(self.__config_file_path, "rb") as fid:
        return tomli.load(fid)

write

write(config: dict)
Source code in src/nexus_e/config.py
def write(self, config: dict):
    with open(self.__config_file_path, "wb") as fid:
        tomli_w.dump(config, fid, multiline_strings=True)