confattr.config module
- class confattr.config.Config(key: str, default: T_co, *, help: str | dict[+T_co, str] | None = None, unit: str | None = None, parent: DictConfig[Any, T_co] | None = None, allowed_values: Sequence[T_co] | None = None)
Bases:
Generic[T_co]Each instance of this class represents a setting which can be changed in a config file.
This class implements the descriptor protocol to return
valueif 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
DictConfigallowed_values¶ – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The
defaultvalue is not checked.
T_cocan 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 attributehelpor by callingSet.set_help_for_type(). If that class has a str attributetype_namethis is used instead of the class name inside of config file.a
listof 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
defaultis 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
unitis not given
- LIST_SEP = ','
- allowed_values: Sequence[T_co] | 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_co, 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
Configinstances. The key in the mapping is thekeyattribute. The value is theConfiginstance. NewConfiginstances add themselves automatically in their constructor.
- parse_value_part(t: type[T], value: str) T
- Raises:
ValueError – if value is invalid
- value: T_co
The value of this setting.
- class confattr.config.ConfigTrackingChanges(key: str, default: T_co, *, help: str | dict[+T_co, str] | None = None, unit: str | None = None, parent: DictConfig[Any, T_co] | None = None, allowed_values: Sequence[T_co] | None = None)
Bases:
Config[T_co]- 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
DictConfigallowed_values¶ – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The
defaultvalue is not checked.
T_cocan 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 attributehelpor by callingSet.set_help_for_type(). If that class has a str attributetype_namethis is used instead of the class name inside of config file.a
listof 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
defaultis 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
unitis not given
- has_changed() bool
- Returns:
Trueifvaluehas been changed since the last call tosave_value()
- restore_value() None
Restore
valueto the value before the last call ofsave_value().
- property value: T
The value of this setting.
- class confattr.config.DictConfig(key_prefix: str, default_values: dict[T_KEY, T], *, ignore_keys: Container[T_KEY] = {}, unit: str | None = None, help: str | None = None, allowed_values: Sequence[T] | None = None)
Bases:
Generic[T_KEY,T]A container for several settings which belong together. It can be indexed like a normal
dictbut internally the items are stored inConfiginstances.In contrast to a
Configinstance it does not make a difference whether an instance of this class is accessed as a type or instance attribute.- Parameters:
key_prefix¶ – A common prefix which is used by
format_key()to generate thekeyby which the setting is identified in the config filedefault_values¶ – The content of this container. A
Configinstance is created for each of these values (except if the key is contained inignore_keys). Seeformat_key().ignore_keys¶ – All items which have one of these keys are not stored in a
Configinstance, i.e. cannot be set in the config file.unit¶ – The unit of all items
help¶ – A help for all items
allowed_values¶ – The values which the items can have
- Raises:
ValueError – if a key is not unique
- format_key(key: T_KEY) str
Generate a key by which the setting can be identified in the config file based on the dict key by which the value is accessed in the python code.
- Returns:
key_prefix+ dot +key
- class confattr.config.InstanceSpecificDictMultiConfig(dmc: MultiDictConfig[T_KEY, T], config_id: ConfigId)
Bases:
Generic[T_KEY,T]An intermediate instance which is returned when accsessing a
MultiDictConfigas an instance attribute. Can be indexed like a normaldict.
- class confattr.config.MultiConfig(key: str, default: T_co, *, unit: str | None = None, help: str | dict[+T_co, str] | None = None, parent: MultiDictConfig[Any, T_co] | None = None, allowed_values: Sequence[T_co] | None = None, check_config_id: Callable[[MultiConfig[T_co], ConfigId], None] | None = None)
Bases:
Config[T_co]A setting which can have different values for different objects.
This class implements the descriptor protocol to return one of the values in
valuesdepending on aconfig_idattribute of the owning object if an instance of this class is accessed as an instance attribute. If there is no value for theconfig_idinvaluesvalueis returned instead. If the owning instance does not have aconfig_idattribute anAttributeErroris raised.In the config file a group can be opened with
[config-id]. Then all followingsetcommands set the value for the specified config id.- 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
MultiDictConfigallowed_values¶ – The possible values this setting can have. Values read from a config file or an environment variable are checked against this. The
defaultvalue is not checked.check_config_id¶ – Is called every time a value is set in the config file (except if the config id is
default_config_id—that is always allowed). The callback should raise aParseExceptionif the config id is invalid.
- allowed_values: Sequence[T_co] | 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.
- config_ids: list[confattr.config.ConfigId] = []
A list of all config ids for which a value has been set in any instance of this class (regardless of via code or in a config file and regardless of whether the value has been deleted later on). This list is cleared by
reset().
- help: str | dict[+T_co, str] | None
A description of this setting or a description for each allowed value.
- classmethod reset() None
Clear
config_idsand clearvaluesfor all instances inConfig.instances
- value: T_co
Stores the default value which is used if no value for the object is defined in
values.
- values: dict[confattr.config.ConfigId, +T_co]
Stores the values for specific objects.
- class confattr.config.MultiDictConfig(key_prefix: str, default_values: dict[T_KEY, T], *, ignore_keys: Container[T_KEY] = {}, unit: str | None = None, help: str | None = None, allowed_values: Sequence[T] | None = None, check_config_id: Callable[[MultiConfig[T], ConfigId], None] | None = None)
Bases:
DictConfig[T_KEY,T]A container for several settings which can have different values for different objects.
This is essentially a
DictConfigusingMultiConfiginstead of normalConfig. However, in order to return different values depending on theconfig_idof the owning instance, it implements the descriptor protocol to return anInstanceSpecificDictMultiConfigif it is accessed as an instance attribute.- Parameters:
key_prefix¶ – A common prefix which is used by
format_key()to generate thekeyby which the setting is identified in the config filedefault_values¶ – The content of this container. A
Configinstance is created for each of these values (except if the key is contained inignore_keys). Seeformat_key().ignore_keys¶ – All items which have one of these keys are not stored in a
Configinstance, i.e. cannot be set in the config file.unit¶ – The unit of all items
help¶ – A help for all items
allowed_values¶ – The values which the items can have
check_config_id¶ – Is passed through to
MultiConfig
- Raises:
ValueError – if a key is not unique