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#

Dataclass

Type hinting to defined a dataclass as a typing Protocol.

DictConfig

param.Parameter that defines a DictConfig object.

OmegaConfInterface

Common functionality to work with configurations.

BaseConfig

Manages getters and setters to access the target's configuration values.

Config

Config handles the .conf attribute of a Configurable class.

Configurable

A Configurable class is an extension of param.Parameterized that allows to handle parameters with missing values and omegaconf interpolation strings.

Functions#

param_to_dataclass_dict(obj)

Create a dictionary that can be used to initialize a dataclass containing the parameters of the target param.Parameterized class.

param_to_dataclass(obj)

Create a dataclass equivalent to the target param.Parameterized target.

param_to_omegaconf(obj)

Transform a param.Parameterized class into an OmegaConf structured configuration.

is_interpolation(s)

Return True if the provided string is an OmegaConf interpolation string.

to_param_type(obj, config, key)

Transform the provided attribute of the target param.Parameterized object into the appropriate type so it can be stored in a configuration file.

to_config(config, **kwargs)

Transform the provided object into an omegaconf.DictConfig.

resolve_as_dict(obj, config, **kwargs)

Transform the provided object into a dictionary resolving all its interpolations.

safe_select(cfg, key[, default])

Access safely the target value of the provided cfg DictConfig.

as_resolved_dict(cfg)

Return a dictionary containing the resolved values for the provided DictConfig.

Attributes#

DClassField

ConfigValue

ConfigurationDict

PythonType

ParamType

DataClassDict

PARAM_TO_TYPE

CONF_ATTRS

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
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

is_interpolation(key)[source]#

Check if the key target’s attribute is an interpolation string.

Parameters

key (str) –

Return type

bool

select(key, default=None)[source]#

Select the key target’s attribute.

Return MISSING if key corresponds to a missing value, or an interpolation that resolves to a missing value.

Return type

Any

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) –

__getitem__(item)[source]#

Access the target config value.

Parameters

item (str) –

Return type

Any

__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

static _resolve_node(kwargs, config=None, cfg_node=None)[source]#

Return a DictConfig containing the resolved configuration values defined in kwargs.

Parameters
  • kwargs (dict) –

  • config (Optional[omegaconf.DictConfig]) –

  • cfg_node (Optional[str]) –

Return type

omegaconf.DictConfig

_setup_config(config=None, cfg_node=None, **kwargs)[source]#

Initialize and validate the structured config of target.

Parameters
  • config (Optional[Union[ConfigurationDict, omegaconf.DictConfig]]) –

  • cfg_node (Optional[str]) –

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

DictConfig

sync()[source]#

Ensure the parameter values of the target class have the right 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

Config

__setattr__(key, value)[source]#

Update the config values when setting a parameter.

__getattr__(item)[source]#

Add support for MISSING values when accessing the parameter values.