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
parent¶ – Applies only if this is part of a
DictConfig
allowed_values¶ – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The
default
value is not checked.
T
can be one of: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. A help which is written to the config file must be provided as a str in the class attributehelp
or by callingSet.set_help_for_type()
. If that class has a str attributetype_name
this is used instead of the class name inside of 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
ValueError – if
default
is an empty list because the first element is used to infer the data type to which a value given in a config file is convertedTypeError – if this setting is a number or a list of numbers and
unit
is not given
- LIST_SEP = ','
- allowed_values: collections.abc.Sequence[confattr.config.T] | None
The values which are allowed for this setting. Trying to set this setting to a different value in the config file is considered an error. If you set this setting in the program the value is not checked.
- default_config_id = 'general'
- help: str | dict[T, str] | None
A description of this setting or a description for each allowed value.
- instances: dict[str, confattr.config.Config[Any]] = {}
A mapping of all
Config
instances. The key in the mapping is thekey
attribute. The value is theConfig
instance. NewConfig
instances add themselves automatically in their constructor.
- parse_value_part(t: type[T1], value: str) T1
- Raises
ValueError – if value is invalid
- value: T
The value of this setting.
- 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
parent¶ – Applies only if this is part of a
DictConfig
allowed_values¶ – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The
default
value is not checked.
T
can be one of: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. A help which is written to the config file must be provided as a str in the class attributehelp
or by callingSet.set_help_for_type()
. If that class has a str attributetype_name
this is used instead of the class name inside of 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
ValueError – if
default
is an empty list because the first element is used to infer the data type to which a value given in a config file is convertedTypeError – if this setting is a number or a list of numbers and
unit
is not given
- has_changed() bool
- Returns
True
ifvalue
has been changed since the last call tosave_value()
- restore_value() None
Restore
value
to the value before the last call ofsave_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
- Raises
ValueError – if a key is not unique
- 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
parent¶ – Applies only if this is part of a
DictConfig
allowed_values¶ – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The
default
value is not checked.
T
can be one of: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. A help which is written to the config file must be provided as a str in the class attributehelp
or by callingSet.set_help_for_type()
. If that class has a str attributetype_name
this is used instead of the class name inside of 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
ValueError – if
default
is an empty list because the first element is used to infer the data type to which a value given in a config file is convertedTypeError – if this setting is a number or a list of numbers and
unit
is not given
- allowed_values: collections.abc.Sequence[confattr.config.T] | None
The values which are allowed for this setting. Trying to set this setting to a different value in the config file is considered an error. If you set this setting in the program the value is not checked.
- 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
- Raises
ValueError – if a key is not unique