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 merged = oc.OmegaConf.merge(*configs) 53 if not isinstance(merged, (oc.DictConfig, oc.ListConfig)): 54 raise TypeError("Only mapping or list configurations are supported.") 55 return merged 56 57 58# %% CONVERTERS 59 60 61def to_object(config: Config, resolve: bool = True) -> object: 62 """Convert a config object to a python object. 63 64 Args: 65 config (Config): representation of the config. 66 resolve (bool): resolve variables. Defaults to True. 67 68 Returns: 69 object: conversion of the config to a python object. 70 """ 71 return oc.OmegaConf.to_container(config, resolve=resolve, throw_on_missing=True)
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 merged = oc.OmegaConf.merge(*configs) 54 if not isinstance(merged, (oc.DictConfig, oc.ListConfig)): 55 raise TypeError("Only mapping or list configurations are supported.") 56 return merged
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:
62def to_object(config: Config, resolve: bool = True) -> object: 63 """Convert a config object to a python object. 64 65 Args: 66 config (Config): representation of the config. 67 resolve (bool): resolve variables. Defaults to True. 68 69 Returns: 70 object: conversion of the config to a python object. 71 """ 72 return oc.OmegaConf.to_container(config, resolve=resolve, throw_on_missing=True)
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.