bikes.io.configs

Parse, merge, and convert config objects.

 1"""Parse, merge, and convert config objects."""
 2
 3# %% IMPORTS
 4
 5import typing as T
 6
 7import omegaconf as oc
 8
 9# %% TYPES
10
11Config = oc.ListConfig | oc.DictConfig
12
13# %% PARSERS
14
15
16def parse_file(path: str) -> Config:
17    """Parse a config file from a path.
18
19    Args:
20        path (str): path to local config.
21
22    Returns:
23        Config: representation of the config file.
24    """
25    return oc.OmegaConf.load(path)
26
27
28def parse_string(string: str) -> Config:
29    """Parse the given config string.
30
31    Args:
32        string (str): content of config string.
33
34    Returns:
35        Config: representation of the config string.
36    """
37    return oc.OmegaConf.create(string)
38
39
40# %% MERGERS
41
42
43def merge_configs(configs: T.Sequence[Config]) -> Config:
44    """Merge a list of config into a single config.
45
46    Args:
47        configs (T.Sequence[Config]): list of configs.
48
49    Returns:
50        Config: representation of the merged config objects.
51    """
52    return oc.OmegaConf.merge(*configs)
53
54
55# %% CONVERTERS
56
57
58def to_object(config: Config, resolve: bool = True) -> object:
59    """Convert a config object to a python object.
60
61    Args:
62        config (Config): representation of the config.
63        resolve (bool): resolve variables. Defaults to True.
64
65    Returns:
66        object: conversion of the config to a python object.
67    """
68    return oc.OmegaConf.to_container(config, resolve=resolve)
Config = omegaconf.listconfig.ListConfig | omegaconf.dictconfig.DictConfig
def parse_file( path: str) -> omegaconf.listconfig.ListConfig | omegaconf.dictconfig.DictConfig:
17def parse_file(path: str) -> Config:
18    """Parse a config file from a path.
19
20    Args:
21        path (str): path to local config.
22
23    Returns:
24        Config: representation of the config file.
25    """
26    return oc.OmegaConf.load(path)

Parse a config file from a path.

Arguments:
  • path (str): path to local config.
Returns:

Config: representation of the config file.

def parse_string( string: str) -> omegaconf.listconfig.ListConfig | omegaconf.dictconfig.DictConfig:
29def parse_string(string: str) -> Config:
30    """Parse the given config string.
31
32    Args:
33        string (str): content of config string.
34
35    Returns:
36        Config: representation of the config string.
37    """
38    return oc.OmegaConf.create(string)

Parse the given config string.

Arguments:
  • string (str): content of config string.
Returns:

Config: representation of the config string.

def merge_configs( configs: Sequence[omegaconf.listconfig.ListConfig | omegaconf.dictconfig.DictConfig]) -> omegaconf.listconfig.ListConfig | omegaconf.dictconfig.DictConfig:
44def merge_configs(configs: T.Sequence[Config]) -> Config:
45    """Merge a list of config into a single config.
46
47    Args:
48        configs (T.Sequence[Config]): list of configs.
49
50    Returns:
51        Config: representation of the merged config objects.
52    """
53    return oc.OmegaConf.merge(*configs)

Merge a list of config into a single config.

Arguments:
  • configs (T.Sequence[Config]): list of configs.
Returns:

Config: representation of the merged config objects.

def to_object( config: omegaconf.listconfig.ListConfig | omegaconf.dictconfig.DictConfig, resolve: bool = True) -> object:
59def to_object(config: Config, resolve: bool = True) -> object:
60    """Convert a config object to a python object.
61
62    Args:
63        config (Config): representation of the config.
64        resolve (bool): resolve variables. Defaults to True.
65
66    Returns:
67        object: conversion of the config to a python object.
68    """
69    return oc.OmegaConf.to_container(config, resolve=resolve)

Convert a config object to a python object.

Arguments:
  • config (Config): representation of the config.
  • resolve (bool): resolve variables. Defaults to True.
Returns:

object: conversion of the config to a python object.