CarefulConfiguration

class upsilonconf.CarefulConfiguration(**kwargs: Any)[source]

Configuration with overwrite protection.

A Configuration should provide a convenient way to store (hyper-)parameters for your code and/or experiments. Any configuration value that represents a mapping is also automagically converted to a Configuration to build arbitrary config hierarchies.

Additional parameter values can be added to the mapping as needed. However, to avoid unwanted changes to configuration values, it is not possible to directly set the value for existing parameters. If you deliberately wish to set the value for existing parameters, you should use the overwrite method instead.

Methods

overwrite(key, value)

Explicitly overwrite an existing parameter value in the configuration.

overwrite_all(m, **kwargs)

Explicitly overwrite multiple existing parameter values in the configuration.

Examples

>>> conf = CarefulConfiguration(foo=0, bar="bar", baz={'a': 1, 'b': 2})
>>> print(conf)
{foo: 0, bar: bar, baz: {a: 1, b: 2}}
>>> conf['bar']
'bar'
>>> conf['baz']['a']
1

Values can also be accessed as attributes, tuples or a string with dots

>>> conf.bar
'bar'
>>> conf.baz.a
1
>>> conf['baz', 'a']
1
>>> conf['baz.a']
1

Values for new parameters can be added directly. To avoid inadvertadly overwriting values, the overwrite method must be used.

>>> conf.new = "works"
>>> conf['new'] = "won't work"
Traceback (most recent call last):
    ...
ValueError: key 'new' already defined, use 'overwrite' methods instead
>>> conf.overwrite('new', "will work")
'works'
>>> conf.new
'will work'
overwrite(key: str, value: Any) Any[source]

Overwrite a possibly existing parameter value in the configuration.

Parameters:
keystr

The parameter name to overwrite the value for.

value

The new value for the parameter.

Returns:
old_value

The value that has been overwritten or None if no value was present.

See also

overwrite_all

overwrite multiple values in one go.

overwrite_all(other: Mapping[str, Any] | Iterable[Tuple[str, Any]] = (), **kwargs) Mapping[str, Any][source]

Overwrite multiple possibly existing parameter value in this configuration.

This method makes it possible to overwrite multiple values in one go. It should produce the same results as calling update when none of the keys are already contained in this configuration. Unlike update, however, this method will not raise an error if one or more of the keys already exist.

Parameters:
otherMapping or iterable of tuples

Dictionary-like object with values to overwrite.

**kwargs

Additional key-value pairs for overwrites.

Returns:
old_valuesMapping

Mapping from keys to the values that have been overwritten. If the key did not exist, the corresponding value is None.

See also

overwrite

overwrite single values.

update

same functionality, but raises errors for duplicate keys.