Configuration Module
Defines classes and functions to turn PhysioBlocks objects into a generic configuration that can be saved or loaded.
Configuration
Define the base configurations objects
- class Configuration(label: str, configuration_items: dict[str, ~typing.Any] = <factory>)
Define a generic configuration object that can easily be serialized.
Note
The
configuration_itemscan also beConfigurationinstances.- label: str
The item type name
- configuration_items: dict[str, Any]
Configuration values
- items() ItemsView[str, Any]
Get a view on configuration key and value pairs.
- Returns:
a view on configuration key-value
- Return type:
ItemsView[str, Any]
- keys() KeysView[str]
Get a view on configuration keys
- Returns:
a view on configuration keys
- Return type:
KeysView[str]
- values() ValuesView[Any]
Get an iterable on configuration values
- Returns:
a view on configuration values
- Return type:
ValuesView[Any]
- copy() Configuration
Get a copy of the configuration.
- Returns:
a copy of the configuration item.
- Return type:
- exception ConfigurationError
Error raised when a configuration can not be loaded or saved.
Load ans save functions
Declares functions to load and save PhysioBlocks object to generic
Configuration objects.
Before using generic load() and save() functions on a PhysioBlocks object,
the object type must be registered with the
register_type() decorator.
To define a specific behavior when saving or loading an registered object with the
generic load() or save() functions, declare a function decorated with
loads() or
saves().
Note
If you want to create a Configurable Item for a dataclass type, you will not have to register a specific save or load function.
Registering the type the register_type()
decorator will suffice to create a configuration item that needs the same parameters
as the dataclass.
See register module to for decorators documentation to
register_type() as well as
loads() and
saves().
- BaseTypes: TypeAlias = float | int | bool | str
Type alias for basic types usable in a Configuration File
- load(configuration: Any, configuration_key: str | None = None, configuration_object: Any | None = None, configuration_type: type[Any] | None = None, configuration_references: dict[str, Any] | None = None, configuration_sort: bool = False) Any
Generic load an object from the given configuration item.
- The method can load:
Configuration: Use the matching registered load function.dict and list: recursivly load values in the collection
- Parameters:
configuration (Any) – the configuration to load
configuration_key (str) – (optional) key of the configuration in the parent configuration item.
configuration_object (Any) – (optional) The object to configure. If empty, a the object is first instanciated then configured.
configuration_type (Any) – (optional) the type of the object to configure. If empty, it is determined from the configuration object.
configuration_references (dict[str, Any]) – (optional) mapping of configuration item keys with already configured objects to use in the current configured object.
configuration_sort (dict[str, Any]) – (optional) flag to signal that configuration items should be sorted be sorted before they are loaded. Default is False.
- Returns:
the configured object
- Return type:
Any
- load_configuration(configuration: Configuration, configuration_key: str | None = None, configuration_object: Any | None = None, configuration_references: dict[str, Any] | None = None, configuration_sort: bool = False, *args: Any, **kwargs: Any) Any
Specific load function for a
Configuration: configuration item.It recursivly loads any configuration item used in the
configurationparameter.- Parameters:
configuration (Configuration) – the configuration item to load
configuration_key (str) – (optional) key of the configuration in the parent configuration item.
configuration_object (Any) – (optional) The object to configure. If empty, a the object is first instanciated then configured.
configuration_type (Any) – (optional) the type of the object to configure. If empty, it is determined from the configuration object.
configuration_references (dict[str, Any]) – (optional) mapping of configuration item keys with already configured objects to use in the current configured object.
configuration_sort (dict[str, Any]) – (optional) flag to signal that configuration items should be sorted be sorted before they are loaded. Default is False.
- Returns:
the configured object
- Return type:
Any
- load_dict(configuration: Mapping[str, Any], configuration_object: dict[str, Any] | None = None, configuration_references: dict[str, Any] | None = None, configuration_sort: bool = False, *args: Any, **kwargs: Any) dict[str, Any]
Specific load function for a Mapping configuration item.
It recursivly loads any configuration item used in the mapping values.
- Parameters:
configuration (Configuration) – the configuration item to load
configuration_key (str) – (optional) key of the configuration in the parent configuration item.
configuration_object (Any) – (optional) The object to configure. If empty, a the object is first instanciated then configured.
configuration_type (Any) – (optional) the type of the object to configure. If empty, it is determined from the configuration object.
configuration_references (dict[str, Any]) – (optional) mapping of configuration item keys with already configured objects to use in the current configured object.
configuration_sort (dict[str, Any]) – (optional) flag to signal that configuration items should be sorted be sorted before they are loaded. Default is False.
- Returns:
the configured object
- Return type:
Any
- load_list(configuration: Sequence[Any], configuration_object: list[Any] | None = None, configuration_references: dict[str, Any] | None = None, configuration_sort: bool = False, *args: Any, **kwargs: Any) Sequence[Any]
Specific load function for a Sequence configuration item.
It recursivly loads any configuration item used in the sequence values.
- Parameters:
configuration (Configuration) – the configuration item to load
configuration_key (str) – (optional) key of the configuration in the parent configuration item.
configuration_object (Any) – (optional) The object to configure. If empty, a the object is first instanciated then configured.
configuration_type (Any) – (optional) the type of the object to configure. If empty, it is determined from the configuration object.
configuration_references (dict[str, Any]) – (optional) mapping of configuration item keys with already configured objects to use in the current configured object.
configuration_sort (dict[str, Any]) – (optional) flag to signal that configuration items should be sorted be sorted before they are loaded. Default is False.
- Returns:
the configured object
- Return type:
Any
- save(obj: Any, configuration_references: dict[str, Any] | None = None, *args: Any, **kwargs: Any) Any
- save(obj: Mapping, *args: Any, **kwargs: Any) dict[str, Any]
- save(obj: Sequence, *args: Any, **kwargs: Any) list[Any]
- save(obj: Any, configuration_references: dict[str, Any] | None = None, *args: Any, **kwargs: Any) Any
- save(obj: Any, configuration_references: dict[str, Any] | None = None, *args: Any, **kwargs: Any) Any
- save(obj: Any, configuration_references: dict[str, Any] | None = None, *args: Any, **kwargs: Any) Any
- save(obj: bool, *args: Any, **kwargs: Any) str
- save(obj: ndarray[tuple[int, ...], dtype[Any]], *args: Any, **kwargs: Any) Any
Save an object to a configuration.
It first check if the object has a
savefunction registered and use it.When no specific
savefunction is registered for a object type, it saves recursivly every annotated parameters of the object class.- Parameters:
obj (Any) – the object to save
- Returns:
the object configuration
- Return type:
Any
- Raises:
ConfigurationError – raise a Configuration Error when the object can not be saved to a configuration.
Aliases
Declares functions to handle configuration aliases.
Aliases are Configuration
instances representing a complete or incomplete type and that can be loaded
and completed or overwritten with the load function.
- ConfigurationAlias: TypeAlias = physioblocks.configuration.base.Configuration | dict[str, typing.Any]
type alias for configuration aliases
- add_alias(alias_key: str, alias: Configuration | dict[str, Any]) None
Add an alias to the global alias register
- Parameters:
alias_key (str) – the alias unique key
alias_key – the alias unique key
- has_alias(alias_key: str) bool
Get if an alias is defined for the given key.
- Parameters:
alias_key (str) – the key to test
- Returns:
True if an alias is defined for the key, False otherwise
- Return type:
bool
- unwrap_aliases(item: Any) Any
- unwrap_aliases(configuration: Configuration) Any
- unwrap_aliases(items: dict) dict[str, Any]
- unwrap_aliases(items: list) list[Any]
Process the given item and create a new one with all its alias replaced with their matching configuration object recursivly.
- Parameters:
item – the item to process
item – Configuration | dict | list
- Returns:
the item to process
- Return type:
Configuration | dict | list
- get_alias(alias_key: str) Any
Get an alias from the global alias register.
- Parameters:
alias_key (str) – the alias unique key
- Returns:
the alias
- Return type:
Configuration | dict | list
- remove_alias(alias_key: str) None
Remove an alias from the global alias register
- Parameters:
alias_key (str) – the alias unique key