confattr.config module

class confattr.config.Config(key: str, default: T, *, help: Optional[Union[str, dict[T, str]]] = None, unit: Optional[str] = None, parent: Optional[DictConfig[Any, T]] = None, allowed_values: Optional[Sequence[T]] = None)

Bases: Generic[T]

Each instance of this class represents a setting which can be changed in a config file.

This class implements the descriptor protocol to return value if an instance of this class is accessed as an instance attribute. If you want to get this object you need to access it as a class attribute.

Parameters:
  • key – The name of this setting in the config file

  • default – The default value of this setting

  • help – A description of this setting

  • unit – The unit of an int or float value

T can be one of:
  • str

  • int

  • float

  • bool

  • a subclass of enum.Enum (the value used in the config file is the name in lower case letters with hyphens instead of underscores)

  • a class where __str__() returns a string representation which can be passed to the constructor to create an equal object. If that class has a str attribute type_name this is used instead of the class name inside of config file. If that class has a str attribute help this is used instead of the doc string when explaining the type at the beginning of the config file.

  • a list of any of the afore mentioned data types. The list may not be empty when it is passed to this constructor so that the item type can be derived but it can be emptied immediately afterwards. (The type of the items is not dynamically enforced—that’s the job of a static type checker—but the type is mentioned in the help.)

Raises:

ValueError – if key is not unique

LIST_SEP = ','
default_config_id = 'general'
format_allowed_values(t: Optional[type[Any]] = None) str
format_allowed_values_or_type(t: Optional[type[Any]] = None) str
format_any_value(value: Any) str
format_type(t: Optional[type[Any]] = None) str
format_value(config_id: Optional[ConfigId]) str
instances: dict[str, confattr.config.Config[Any]] = {}

A mapping of all Config instances. The key in the mapping is the key attribute. The value is the Config instance. New Config instances add themselves automatically in their constructor.

property key: str
parse_and_set_value(config_id: Optional[ConfigId], value: str) None
parse_value(value: str) T
parse_value_part(t: type[T1], value: str) T1
Raises:

ValueError – if value is invalid

wants_to_be_exported() bool
class confattr.config.ConfigTrackingChanges(key: str, default: T, *, help: Optional[Union[str, dict[T, str]]] = None, unit: Optional[str] = None, parent: Optional[DictConfig[Any, T]] = None, allowed_values: Optional[Sequence[T]] = None)

Bases: Config[T]

Parameters:
  • key – The name of this setting in the config file

  • default – The default value of this setting

  • help – A description of this setting

  • unit – The unit of an int or float value

T can be one of:
  • str

  • int

  • float

  • bool

  • a subclass of enum.Enum (the value used in the config file is the name in lower case letters with hyphens instead of underscores)

  • a class where __str__() returns a string representation which can be passed to the constructor to create an equal object. If that class has a str attribute type_name this is used instead of the class name inside of config file. If that class has a str attribute help this is used instead of the doc string when explaining the type at the beginning of the config file.

  • a list of any of the afore mentioned data types. The list may not be empty when it is passed to this constructor so that the item type can be derived but it can be emptied immediately afterwards. (The type of the items is not dynamically enforced—that’s the job of a static type checker—but the type is mentioned in the help.)

Raises:

ValueError – if key is not unique

has_changed() bool
Returns:

True if value has been changed since the last call to save_value()

restore_value() None

Restore value to the value before the last call of save_value().

save_value(new_value: T) None

Save the current value and assign new_value to value.

property value: T
class confattr.config.DictConfig(key_prefix: str, default_values: dict[T_KEY, T], *, ignore_keys: Container[T_KEY] = {}, unit: Optional[str] = None, help: Optional[str] = None, allowed_values: Optional[Sequence[T]] = None)

Bases: Generic[T_KEY, T]

Parameters:
  • key_prefix

  • default

  • ignore_keys

  • unit

  • help

Raises:

ValueError – if a key is not unique

format_key(key: T_KEY) str
iter_keys() Iterator[str]
new_config(key: str, default: T, *, unit: str | None, help: str | dict[T, str] | None) Config[T]
class confattr.config.InstanceSpecificDictMultiConfig(dmc: MultiDictConfig[T_KEY, T], config_id: ConfigId)

Bases: Generic[T_KEY, T]

class confattr.config.MultiConfig(key: str, default: T, *, unit: Optional[str] = None, help: Optional[Union[str, dict[T, str]]] = None, parent: Optional[MultiDictConfig[Any, T]] = None, allowed_values: Optional[Sequence[T]] = None)

Bases: Config[T]

Parameters:
  • key – The name of this setting in the config file

  • default – The default value of this setting

  • help – A description of this setting

  • unit – The unit of an int or float value

T can be one of:
  • str

  • int

  • float

  • bool

  • a subclass of enum.Enum (the value used in the config file is the name in lower case letters with hyphens instead of underscores)

  • a class where __str__() returns a string representation which can be passed to the constructor to create an equal object. If that class has a str attribute type_name this is used instead of the class name inside of config file. If that class has a str attribute help this is used instead of the doc string when explaining the type at the beginning of the config file.

  • a list of any of the afore mentioned data types. The list may not be empty when it is passed to this constructor so that the item type can be derived but it can be emptied immediately afterwards. (The type of the items is not dynamically enforced—that’s the job of a static type checker—but the type is mentioned in the help.)

Raises:

ValueError – if key is not unique

config_ids: list[confattr.config.ConfigId] = []
format_value(config_id: Optional[ConfigId]) str
parse_and_set_value(config_id: Optional[ConfigId], value: str) None
classmethod reset() None
values: dict[confattr.config.ConfigId, T]
class confattr.config.MultiDictConfig(key_prefix: str, default_values: dict[T_KEY, T], *, ignore_keys: Container[T_KEY] = {}, unit: Optional[str] = None, help: Optional[str] = None, allowed_values: Optional[Sequence[T]] = None)

Bases: DictConfig[T_KEY, T]

Parameters:
  • key_prefix

  • default

  • ignore_keys

  • unit

  • help

Raises:

ValueError – if a key is not unique

new_config(key: str, default: T, *, unit: str | None, help: str | dict[T, str] | None) MultiConfig[T]