repype.config

class repype.config.Config(other: dict | Self | None = None)

Bases: object

Represents a set of hyperparameters.

Hyperparameters can be worked with as follows:

>>> import repype.config
>>> config = repype.config.Config()
>>> config['stage1/param1'] = 1000
>>> config['stage2/param2'] = 5
>>> print(config.entries)
{'stage1': {'param1': 1000}, 'stage2': {'param2': 5}}
Parameters:

other – A dictionary to be wrapped (no copying occurs), or another Config object (a deep copy is created). Defaults to None, for which a blank configuration is created.

__contains__(key: str) bool

Checks whether a hyperparameter is set.

Parameters:

key – The hyperparameter to be queried.

Returns:

True if the hyperparameter key is set and False otherwise.

__getitem__(key: str) Any

Returns the value of a hyperparameter.

Parameters:

key – The hyperparameter to be queried.

Returns:

The value of the hyperparameter key, or default if key is not set.

Raises:

KeyError – Raised if the hyperparameter key is not set.

__setitem__(key: str, value: Any) Self

Sets the value of a hyperparameter.

Parameters:
  • key – The hyperparameter to be set.

  • value – The new value of the hyperparameter.

Returns:

Itself.

__str__()

Readable representation of this configuration.

copy()

Returns a deep copy.

entries: dict

Nested dictionaries of hyperparameters.

get(key: str, default: Any) Any

Returns the value of a hyperparameter.

Parameters:
  • key – The hyperparameter to be queried.

  • default – Returned if the hyperparameter key is not set.

Returns:

The value of the hyperparameter key, or default if key is not set.

merge(other: Self) Self

Updates this configuration using the hyperparameters from another configuration.

The hyperparameters of this configuration are set to the values from the other configuration. If a hyperparameter was previously not set in this configuration, it is set to the value from the other configuration.

Parameters:

other – The configuration which is to be merged into this configuration.

Returns:

Itself.

pop(key: str, default: Any) Any

Removes a hyperparameter from this configuration.

Parameters:
  • key – The hyperparameter to be removed.

  • default – Returned if the hyperparameter key is not set.

Returns:

The value of the hyperparameter key, or the default if key is not set.

set_default(key: str, default: Any, override_none: bool = False)

Sets a hyperparameter if it is not set yet.

Parameters:
  • key – The hyperparameter to be set.

  • default – Returned if the hyperparameter key is not set.

  • override_noneTrue if a hyperparameter set to None should be treated as not set.

Returns:

The new or unmodified value of the hyperparameter key.

property sha

The SHA1 hash code associated with the hyperparameters set in this configuration.

update(key: str, func: Callable[[Any], Any]) Any

Updates a hyperparameter by mapping it to a new value.

Parameters:
  • key – The hyperparameter to be updated.

  • func – Function which maps the previous value to the new value.

Returns:

The new value.

property yaml: str

YAML representation of this configuration.

>>> import repype.config
>>> config = repype.config.Config()
>>> config['stage1/param1'] = 1000
>>> config['stage2/param2'] = 5
>>> config['stage1/sub/param1'] = 'xyz'
>>> print(config.yaml)
stage1:
  param1: 1000
  sub:
    param1: 'xyz'
stage2:
  param2: 5