algocomponents

Subpackages

Submodules

algocomponents.config_reader module

class algocomponents.config_reader.ConfigReader(global_config_dir: str = 'config', local_config_dir: str = 'config', config_files: List[str] = None, config: ConfigParser = None, section: str = None)

Bases: ABC

A class used to read and manage configs.

The config files are python config.ini-files. The priority is as follows, starting with the highest priority (in terms of what overwrites what):

  1. Adding to config via code

  2. Passed config

  3. Local config

  4. Global config

The rule of thumb is “Code over files, specific beats general”.

The class also creates a unique logger which can be used by classes that inherit from this class. The log is by default written to a file in root called log.log and printed to the terminal. The log is configurable by these fields in the config:

log_to_file: True or False, decides where a log should be written to file log_level: At what level to log (DEBUG, INFO, WARNING, ERROR, CRITICAL)

Parameters:
  • global_config_dir – Path from project root to global config.ini-file.

  • local_config_dir – Relative path to local config.ini-file.

  • config_files – Names of config files to read. Config from files later in the list overwrite earlier elements. Defaults to [“config.ini”]

  • config – A passed ConfigParser object, which overwrites any files read.

  • section – Which section of the ConfigParsers should be read from.

add_to_config(key, value)

Add values to config for the current section

Parameters:
  • key – Which key to add or update

  • value – What value to give the key

format_string(string: str, additional_format_variables: Dict[str, str] = None, max_depth: int = 5) str

Recursively .format():s a string given a dict until it does not change.

A max depth is used, as writing a more general approach to this method involves solving self-referencing problems in the format variables. For example, {“a”: “{b}”, “b”: “{a}”} which will cause “{a}” to be formatted into “{b}”, which will format into “{a}”, etc. There are solutions, but the added code complexity was deemed to not be worth it.

Parameters:
  • string – The string to format.

  • additional_format_variables – A dictionary used to .format() the SQL string.

  • max_depth – Max number of times .format() will be done.

Returns:

The provided string, formatted.

Raises:

RecursionError – When .format():ing more than max_depth times and the string is still changing

Examples

{output_table} -> {tmp_db}.output_table -> tmp.output_table

update_logger()

Update the logger with new config settings.

Currently, this is code duplication, because the fetch_logger() method itself handles returning the same logger if it is called with the same settings. However, having this as a separate piece of code is necessary, and it is likely that this will eventually not be code duplication as the use cases “Setting up logging” and “Updating logging” are different.

verify_section_is_in_config(section)

Verifies that the section exists in the config for this class

Raises:

ValueError – If the section is not in the config

Module contents