coma.config.base¶
Base of config utilities implementation.
- class InstanceKeys[source]¶
Collection of standard instance keys used for
Configin corecoma. User-defined keys beyond these can exist.
- InstanceKey¶
Type alias for instance keys.
- Identifier¶
Type alias for a valid Python identifier.
- ConfigID¶
Type alias for config identifiers. Must be a valid Python identifier.
- ParamID¶
Type alias for parameter identifiers. Must be a valid Python identifier.
- Parameters¶
Type alias for a mapping between parameter identifiers and values.
- class Config(back_end: ~typing.Any, instances: dict[str, ~typing.Any] = <factory>)[source]¶
Wrapper for config data. Manages all viable instances of a config. Typically, a new instance variant is created whenever major changes to the config data are made.
- back_end¶
The backing construct of the config. Must be an
omegaconf-supported type, which means either alistobject, adictobject, or adataclasstype or object.- Type:
- instances¶
The collection of instance variants of
back_endmapped by variant keys.- Type:
dict[
InstanceKey, typing.Any]
- get(key: str) Any[source]¶
Returns the instance corresponding to
key.- Parameters:
key (
InstanceKey) – The instance key.- Returns:
The corresponding instance.
- Return type:
- Raises:
KeyError – If no corresponding instance exists.
- set(key: str, instance: Any, force_latest: bool = False) None[source]¶
Sets or updates the instance corresponding to
key.- Parameters:
key (
InstanceKey) – The instance variant key for which to set or update the instance data.instance (Any) – The instance data to set for
key.force_latest (bool) – Whether to force
keyto be interpreted as the latest (at least until the next call toset()).
See also
- delete(key: str, raise_on_missing: bool = False) None[source]¶
Deletes the instance corresponding to
key.- Parameters:
key (
InstanceKey) – The instance variant key for which to delete the instance data.raise_on_missing (bool) – Whether to raise an error when no instance variant corresponding to
keyexists or no nothing silently.
- Raises:
KeyError – If no instance corresponding to
keyexists andraise_on_missingisTrue.
- get_latest() Any[source]¶
Returns the latest instance.
Note
This is the latest by insertion order. Newer overwrites of existing instances don’t count as latest unless
force_latestisTrueon calls toset().- Raises:
ValueError – If there are no instances at all and so no latest instance.
See also
- get_latest_key() str[source]¶
Returns the key of the latest instance.
Note
This is the latest by insertion order. Newer overwrites of existing instances don’t count as latest unless
force_latestisTrueon calls toset().- Returns:
The key corresponding to the latest instance to be set.
- Return type:
- Raises:
ValueError – If there are no instances at all and so no latest instance.
See also
- get_or_latest(key: str | None = None) Any[source]¶
Returns the instance corresponding to
keyunlesskeyisNonein which case the latest instance is returned instead.- Returns:
The corresponding instance.
- Return type:
- Raises:
KeyError – If
keyis notNone, but no corresponding instance exists.ValueError – If
keyisNone, but there are no instances at all and so no latest instance.
See also
- make_latest(key: str) None[source]¶
Forces the given
keyto be interpreted as the latest (at least until a new key gets added).- Raises:
KeyError – If no corresponding instance exists.
See also
- is_primitive(key: str) bool[source]¶
Returns whether the instance data corresponding to
keyis a primitive Python object (list,dict, ordataclass) as opposed to anomegaconfcontainer object.- Raises:
KeyError – If no corresponding instance exists.
- as_primitive(key: str, *, resolve: bool = True, throw_on_missing: bool = True, enum_to_str: bool = False, structured_config_mode: SCMode = SCMode.INSTANTIATE) Any[source]¶
Returns the instance data corresponding to
keyas a primitive Python object (list,dict, ordataclass) instead of anomegaconfcontainer object.If the instance is already primitive, returns it directly.
Does not update the underlying instance. To do so, use
set().- Parameters:
key (
InstanceKey) – The instance variant key for which to convert the instance data into a primitive.resolve (bool) – Passed to OmegaConf.to_container().
throw_on_missing (bool) – Passed to OmegaConf.to_container().
enum_to_str (bool) – Passed to OmegaConf.to_container().
structured_config_mode (
omegaconf.base.SCMode) – Passed to OmegaConf.to_container().
- Returns:
The instance data for
keyas a Python primitive.- Return type:
- Raises:
KeyError – If no corresponding instance exists.
Others – As may be raised by OmegaConf.to_container().
- from_primitive(key: str, *, parent: BaseContainer | None = None, flags: dict[str, bool] | None = None) Any[source]¶
Returns the instance data corresponding to
keyas anomegaconfcontainer object instead of a primitive Python object (list,dict, ordataclass).If the instance is already an
omegaconfcontainer, returns it directly.Does not update the underlying instance. To do so, use
set().- Parameters:
key (
InstanceKey) – The instance variant key for which to convert the instance data into a primitive.parent (
omegaconf.basecontainer.BaseContainer, optional) – Passed to OmegaConf.create().flags (dict[str, bool], optional) – Passed to OmegaConf.create().
- Returns:
The instance data for
keyas anomegaconfcontainer.- Return type:
- Raises:
KeyError – If no corresponding instance exists.
Others – As may be raised by OmegaConf.create().