mloq.config.configuration
Contents
mloq.config.configuration
#
This module defines the Configurable class and associated logic.
The Configurable class extends the param.Parameterizable class to keep track of the class parameters using an omegaconf.DictConfig.
Module Contents#
Classes#
Type hinting to defined a dataclass as a typing Protocol. |
|
param.Parameter that defines a DictConfig object. |
|
Common functionality to work with configurations. |
|
Manages getters and setters to access the target's configuration values. |
|
Config handles the .conf attribute of a Configurable class. |
|
A Configurable class is an extension of param.Parameterized that allows to handle parameters with missing values and omegaconf interpolation strings. |
Functions#
Create a dictionary that can be used to initialize a dataclass containing the parameters of the target param.Parameterized class. |
|
|
Create a dataclass equivalent to the target param.Parameterized target. |
|
Transform a param.Parameterized class into an OmegaConf structured configuration. |
Return True if the provided string is an OmegaConf interpolation string. |
|
|
Transform the provided attribute of the target param.Parameterized object into the appropriate type so it can be stored in a configuration file. |
|
Transform the provided object into an omegaconf.DictConfig. |
|
Transform the provided object into a dictionary resolving all its interpolations. |
|
Access safely the target value of the provided cfg DictConfig. |
|
Return a dictionary containing the resolved values for the provided DictConfig. |
Attributes#
- mloq.config.configuration.DClassField#
- class mloq.config.configuration.Dataclass[source]#
Bases:
typing_extensions.Protocol
Type hinting to defined a dataclass as a typing Protocol.
- __dataclass_fields__ :Dict#
- mloq.config.configuration.ConfigValue#
- mloq.config.configuration.ConfigurationDict#
- mloq.config.configuration.PythonType#
- mloq.config.configuration.ParamType#
- mloq.config.configuration.DataClassDict#
- class mloq.config.configuration.DictConfig(default=None, doc=None, instantiate=True, per_instance=True, **kwargs)[source]#
Bases:
mloq.config.param_patch.param.ClassSelector
param.Parameter that defines a DictConfig object.
- Parameters
default (Optional[omegaconf.DictConfig]) –
doc (Optional[str]) –
instantiate (bool) –
per_instance (bool) –
- mloq.config.configuration.PARAM_TO_TYPE#
- mloq.config.configuration.param_to_dataclass_dict(obj)[source]#
Create a dictionary that can be used to initialize a dataclass containing the parameters of the target param.Parameterized class.
- Parameters
obj (Union[mloq.config.param_patch.param.Parameterized, Any]) – Class or instance of a param.Parameterized class.
- Returns
dict containing the fields required to define a dataclass with the obj parameters.
- Return type
Dict[str, Tuple[type, DClassField]]
- mloq.config.configuration.param_to_dataclass(obj)[source]#
Create a dataclass equivalent to the target param.Parameterized target.
- Parameters
obj (Union[mloq.config.param_patch.param.Parameterized, Any]) –
- Return type
type
- mloq.config.configuration.param_to_omegaconf(obj)[source]#
Transform a param.Parameterized class into an OmegaConf structured configuration.
- Parameters
obj (Union[mloq.config.param_patch.param.Parameterized, Any]) –
- Return type
omegaconf.DictConfig
- mloq.config.configuration.is_interpolation(s)[source]#
Return True if the provided string is an OmegaConf interpolation string.
- Parameters
s (str) –
- Return type
bool
- mloq.config.configuration.to_param_type(obj, config, key)[source]#
Transform the provided attribute of the target param.Parameterized object into the appropriate type so it can be stored in a configuration file.
- Parameters
obj (mloq.config.param_patch.param.Parameterized) –
config (DictConfig) –
key (str) –
- Return type
Any
- mloq.config.configuration.to_config(config, **kwargs)[source]#
Transform the provided object into an omegaconf.DictConfig.
- Parameters
config (Union[omegaconf.DictConfig, ConfigurationDict, Dataclass, mloq.config.param_patch.param.Parameterized, None]) –
- Return type
omegaconf.DictConfig
- mloq.config.configuration.resolve_as_dict(obj, config, **kwargs)[source]#
Transform the provided object into a dictionary resolving all its interpolations.
- Parameters
config (Union[omegaconf.DictConfig, ConfigurationDict, Dataclass, mloq.config.param_patch.param.Parameterized]) –
- Return type
ConfigurationDict
- mloq.config.configuration.safe_select(cfg, key, default=None)[source]#
Access safely the target value of the provided cfg DictConfig.
Return MISSING if the value cannot be resolved or it’s missing.
- Parameters
cfg (DictConfig) –
key (str) –
default (Any) –
- Return type
Any
- mloq.config.configuration.as_resolved_dict(cfg)[source]#
Return a dictionary containing the resolved values for the provided DictConfig.
- Parameters
cfg (DictConfig) –
- Return type
ConfigurationDict
- class mloq.config.configuration.OmegaConfInterface(target, allow_missing=False)[source]#
Common functionality to work with configurations.
- Parameters
target (Configurable) –
allow_missing (bool) –
- property config#
Return a DictConfig containing the target configuration.
- Return type
omegaconf.DictConfig
- property interpolations#
Return a dictionary containing the interpolations of the target configuration.
- Return type
ConfigurationDict
- property missing#
Return a list containing the names of the configuration that are MISSING.
- Return type
List[Union[str, int, enum.Enum, float, bool]]
- _resolve_inplace(key=None)[source]#
Resolve and update the target attribute if it’s an interpolation string.
- Parameters
key (Optional[str]) –
- Return type
None
- resolve(key=None, inplace=False)[source]#
Resolve the target attribute if it is an interpolation string.
- Parameters
key (Optional[str]) – Name of the target’s attribute to be resolved.
inplace (bool) – If True, update the configuration value replacing the interpolation string with the resolved value.
- Returns
Resolved value of the target attribute.
- Return type
Union[omegaconf.Container, ConfigValue, None]
- is_missing(key)[source]#
Return True if the key target’s attribute is Missing, otherwise return False.
- Parameters
key (str) –
- Return type
bool
- class mloq.config.configuration.BaseConfig(target, config=None, cfg_node=None, allow_missing=False, **kwargs)[source]#
Bases:
OmegaConfInterface
Manages getters and setters to access the target’s configuration values.
- Parameters
target (Configurable) –
config (Optional[Union[ConfigurationDict, omegaconf.DictConfig]]) –
cfg_node (Optional[str]) –
allow_missing (bool) –
- __setitem__(key, value)[source]#
Set the target config value.
- Parameters
key (str) –
- Return type
Any
- to_container(resolve=False, **kwargs)[source]#
Return a container containing the target’s configuration.
- Parameters
resolve (bool) –
- Return type
omegaconf.Container
- class mloq.config.configuration.Config(target, config=None, cfg_node=None, allow_missing=False, **kwargs)[source]#
Bases:
BaseConfig
Config handles the .conf attribute of a Configurable class.
It is analogous to .param for param.Parameterized classes. This class implements all the logic to access and update the config attribute of a Configurable class, which returns a DictConfig instance that is automatically update when the parameters of the class change.
- Parameters
target (Configurable) –
config (Optional[Union[ConfigurationDict, omegaconf.DictConfig]]) –
cfg_node (Optional[str]) –
allow_missing (bool) –
- property params#
Return the param.Parameter dictionary of the target configurable.
- Return type
Dict[str, mloq.config.param_patch.param.Parameter]
- resolve(key=None, inplace=False)[source]#
Resolve the key attribute of the target Configurable.
- Parameters
key (Optional[str]) –
inplace (bool) –
- Return type
Union[omegaconf.Container, ConfigValue, None]
- to_param_type(key)[source]#
Transform the value of the key target’s parameter to a DictConfig compatible type.
- Return type
Any
- dataclass_dict(ignore=None)[source]#
Return a dictionary to create a dataclass with the target’s parameters.
- Parameters
ignore (Optional[Union[list, set, tuple, str]]) –
- Return type
DataClassDict
- to_dataclass()[source]#
Return a dataclass describing the parameter values of the target Configurable.
- Return type
type
- to_dictconfig()[source]#
Return a structured DictConfig containing the parameters of the target Configurable.
- Return type
- _setup_config(config=None, cfg_node=None, **kwargs)[source]#
Initialize and validate the structured config of target.
- Parameters
config (Optional[Union[ConfigurationDict, DictConfig]]) –
cfg_node (Optional[str]) –
- mloq.config.configuration.CONF_ATTRS#
- class mloq.config.configuration.Configurable(config=None, throw_on_missing=True, cfg_node=None, **kwargs)[source]#
Bases:
mloq.config.param_patch.param.Parameterized
A Configurable class is an extension of param.Parameterized that allows to handle parameters with missing values and omegaconf interpolation strings.
It add a config attribute containing an omegaconf.DictConfig that contains the values of the class param.Parameters.
It also provides a conf attribute that allows to access omegaconf functionality for managing configurations in a similar fashion as the param attribute allows to access param.Parameter functionality.
- Parameters
config (Optional[Union[ConfigurationDict, DictConfig]]) –
throw_on_missing (bool) –
cfg_node (Optional[str]) –
- config#
- property conf#
Access the Config instance that tracks and manages the values in the class config.
- Return type