4.7.2 pde.tools.config module
Handles configuration variables of the package.
Class representing a single parameter. |
|
Class handling general (nested) configurations. |
|
Tries to load certain python packages and returns their version. |
|
Helper function converting a version string into a list of integers. |
|
Checks whether a package has a sufficient version. |
|
Read package names from a requirements file. |
|
Read version number of ffmpeg program. |
|
Check whether the code is running in a high-performance computing environment. |
|
Obtain information about the compute environment. |
- exception AccessError[source]
Bases:
RuntimeErrorRaised when a configuration change violates the active access mode.
- add_note()
Exception.add_note(note) – add a note to the exception
- args
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class Config(items: MutableMapping[str, Any] | Config | Sequence[Parameter] | None = None, *, mode: ConfigMode | str = 'update')[source]
Bases:
NestedDict[Parameter]Class handling general (nested) configurations.
Configurations are basically (nested) dictionaries with string keys that hold
Parametervalues, which contain a value with some extra information. Moreover, configurations have a mode that controls whether the configuration is writeable or not.- Parameters:
items (dict, optional) – Configuration values that should be added or overwritten to initialize the configuration.
mode (str) –
Defines the mode in which the configuration is used. Possible values are
insert: any new configuration key can be inserted
update: only the values of pre-existing items can be updated
locked: no values can be changed
Note that the items specified by items will always be inserted, independent of the mode.
- changed_mode(**kwargs)[source]
Temporarily switch to mode and restore the previous mode afterwards.
- Parameters:
**kwargs – Keyword arguments forwarded to
ConfigMode._setstate(), such as node, leaf, and delete.- Yields:
ConfigMode – The mode controller with the temporary mode applied.
- copy() Config[source]
Creates a structural copy with copied nested mappings.
Child dictionaries and child NestedDict instances are copied, while non-mapping leaf values are reused by reference.
- Returns:
New instance containing copied nested structure.
- Return type:
- create_node(key: str) Self
Create an empty node at the given location.
Creates all necessary parent nodes recursively. Skips nodes that already exist.
- Parameters:
key – Key or nested key path identifying the node to create.
- Returns:
The leaf node
- items(*, flatten: bool = False) Iterator[tuple[str, TNestedDictValue[TValue]]]
Iterates over key-value pairs, optionally flattening nested paths.
- keys(*, flatten: bool = False) Iterator[str]
Iterates over keys, optionally returning flattened key paths.
- property mode: ConfigMode
Current mutable mode descriptor shared across the whole config tree.
- pprint(*args: Any, flatten: bool = False, **kwargs: Any) None
Pretty-prints the current data as nested plain dictionaries.
- Parameters:
*args (Any) – Positional arguments forwarded to pprint.pprint.
flatten (bool) – If True, returns a flat mapping with separator-joined paths as keys. If False, returns nested dictionaries.
**kwargs (Any) – Keyword arguments forwarded to pprint.pprint.
- replace_recursive(other: MutableMapping[str, Any], delete_extra: bool = False) None[source]
Recursively replaces data of the current instance by another mapping.
- Parameters:
other (MutableMapping[str, Any]) – Mapping whose entries are will end up in this object.
- to_dict(flatten: bool = False, values: bool = False) dict[str, Any][source]
Convert the configuration to a simple dictionary.
- update(other) None
Update this mapping from another mapping recursively.
This method implements
collections.abc.MutableMappingupdate semantics for mapping-like inputs and forwards the actual merge toupdate_recursive().- Parameters:
other – Mapping containing keys and values to merge into this instance.
- Raises:
TypeError – If other is not a mutable mapping.
- update_recursive(other: MutableMapping[str, Any]) None
Recursively merges another mapping into this instance.
- Parameters:
other (MutableMapping[str, Any]) – Mapping whose entries are merged into this object. If both sides contain nested mappings at a key, values are merged recursively.
- values(*, flatten: bool = False) Iterator[TNestedDictValue[TValue]]
Iterates over values, optionally recursing into nested children.
- Parameters:
flatten (bool) – If True, yields values from all descendant NestedDict instances. If False, yields only top-level values.
- Returns:
Iterator over values according to flatten.
- Return type:
Iterator[Any]
- class ConfigMode(node: Modes = Modes.UPDATE, leaf: Modes = Modes.INSERT, delete: bool = False)[source]
Bases:
objectMutable object storing the current configuration mode.
- Parameters:
mode – Initial mode controlling whether items can be inserted, updated, or modified at all.
- classmethod from_str(value: str)[source]
Create a mode descriptor from a textual mode name.
- Parameters:
value – Mode name. Supported values are
"insert","update", and"locked".- Returns:
Newly created mode descriptor.
- Return type:
- Raises:
ValueError – If value is not one of the supported mode names.
- class Modes(*values)[source]
Bases:
EnumAccess modes controlling how configuration entries can be modified.
- INSERT = 'insert'
- READONLY = 'readonly'
- UPDATE = 'update'
- class Parameter(value: str | float | int | bool | None, *, default_value: str | float | int | bool | None | _OMITTED_TYPE = <_OMITTED_TYPE object>, cls: object | _OMITTED_TYPE = <_OMITTED_TYPE object>, description: str = '', hidden: bool = False, extra: dict[str, ~Any] | None=None)[source]
Bases:
objectClass representing a single parameter.
- Parameters:
value – The current value of the parameter.
default_value – The fallback value used when the parameter is reset. If omitted, the current value is used.
cls – Type used to convert assigned values.
description – Human-readable explanation of the parameter.
hidden – Flag indicating whether the parameter should be hidden in summaries.
extra – Optional metadata stored alongside the parameter.
- convert(value: str | float | int | bool | None | _OMITTED_TYPE = <_OMITTED_TYPE object>) str | float | int | bool | None[source]
Converts a value into the correct type for this parameter. If value is not given, the current value is converted.
Note that this does not make a copy of the values, which could lead to unexpected effects where the default value is changed by an instance.
- Parameters:
value – The value to convert
- Returns:
The converted value, which is of type self.cls
- check_package_version(package_name: str, min_version: str)[source]
Checks whether a package has a sufficient version.
- environment() dict[str, Any][source]
Obtain information about the compute environment.
- Returns:
information about the python installation and packages
- Return type:
- get_ffmpeg_version() str | None[source]
Read version number of ffmpeg program.
- Returns:
Detected version string, or None if ffmpeg is unavailable or the version could not be parsed.
- Return type:
str | None
- get_package_versions(packages: list[str], *, na_str='not available') dict[str, str][source]
Tries to load certain python packages and returns their version.
- is_hpc_environment() bool[source]
Check whether the code is running in a high-performance computing environment.
- Returns:
True if running in an HPC environment, False otherwise.
- Return type: