templates.util.instance_manager module¶
This module provides the InstanceManagerMixin class, making it easy to manage instances of a class.
This is useful in serialization/deserialization logic.
- class templates.util.instance_manager.InstanceManagerMixin[source]¶
Bases:
ABC
This Mixin class provides functionality to save instances of a class globally and retrieve them using an identifier.
This is class is used for example by EncryptionScheme. Upon receiving a ciphertext, it must be deserialized and the corresponding EncryptionScheme instance to which the ciphertext belongs must be found. By keeping track of all created EncryptionScheme instances by a unique identifier, we can retrieve the EncryptionScheme belonging to a received ciphertext.
- __init__()[source]¶
Initialize the InstanceManagerMixin.
This method should be called by the constructor of the derived class.
- classmethod __init_subclass__(**kwargs)[source]¶
Constructor for subclasses.
Ensures that the subclass is registered as a derived class of its superclass.
- Return type:
None
- classmethod clear_instances(all_types=False)[source]¶
Clear the list of globally saved instances of the current derived class.
- Parameters:
all_types (
bool
) – also clear instances of other derived classes.- Return type:
None
- classmethod from_id(identifier)[source]¶
Return the instance with the given identifier that is stored in the global list.
- Parameters:
identifier (
int
) – Identifier of the instance to retrieve.- Raises:
KeyError – If no iinstance with this ID was found in the global list.
- Return type:
Self
- Returns:
the instance belonging to this identifier.
- classmethod from_id_arguments(*args, **kwargs)[source]¶
Function that calls id_from_arguments to obtain an identifier for this instance and then retrieves an instance from the global list of saved schemes using from_id.
- Parameters:
*args (
Any
) – regular arguments that would normally go into the constructor**kwargs (
Any
) – regular keyword arguments that would normally go into the constructor
- Return type:
Self
- Returns:
An instance with the same ID if one has been saved globally
- abstractmethod classmethod id_from_arguments(*args, **kwargs)[source]¶
Method that turns the arguments for the constructor into an identifier. This identifier is used to find constructor calls that would result in identical instances.
- Parameters:
*args (
Any
) – regular arguments**kwargs (
Any
) – regular keyword arguments
- Return type:
int
- Returns:
identifier of type int
- property identifier: int¶
Property that returns an identifier for the instance. It calls id_from_arguments and inspects id_from_arguments to see which parameter names are required. It then searches for these parameter names in the attributes and properties of self and uses their values as input to the function. Note that this requires the parameter names to id_from_arguments to have the same name as their respective attributes/properties in the scheme.
- Raises:
KeyError – In cast there is a mismatch between the argument names in id_from_arguments and the parameter names and properties of the class.
TypeError – At least one argument name from id_from_arguments correponds with a callable rather than an attribute or property.
AttributeError – If the class does not have the attribute __identifier, which can happen if the constructor is not called.
- Returns:
An identifier of type int
- remove_from_global_list()[source]¶
If this instance is saved in the global list, remove it.
- Return type:
None
- save_globally(overwrite=False)[source]¶
Save this instance in a global list for accessibility using its identifier.
- Parameters:
overwrite (
bool
) – overwrite an entry in the global list of the IDs coincide- Raises:
KeyError – If the ID already exists in the global list and overwrite is False
- Return type:
None
- class templates.util.instance_manager.StrictClassProperty(default_factory)[source]¶
Bases:
Generic
[T
]This class provides a property that is scoped to a class rather than an instance. This means that the property is shared by all instances of a class. This is useful for example to keep track of all instances of a class globally.
It is similar to using @property together with @classmethod, but this has been deprecated as of Python3.11.
- __get__(instance, owner)[source]¶
Get the value of the property for the given class.
- Parameters:
instance (
Any
) – The instance of the class for which to get the property.owner (
Any
) – The class for which to get the property.
- Return type:
TypeVar
(T
)- Returns:
The value of the property for the given class.