templates.util.instance_manager =============================== .. py:module:: templates.util.instance_manager .. autoapi-nested-parse:: This module provides the InstanceManagerMixin class, making it easy to manage instances of a class. This is useful in serialization/deserialization logic. Attributes ---------- .. autoapisummary:: templates.util.instance_manager.T Classes ------- .. autoapisummary:: templates.util.instance_manager.StrictClassProperty templates.util.instance_manager.InstanceManagerMixin Module Contents --------------- .. py:data:: T .. py:class:: StrictClassProperty(default_factory: type[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. .. py:class:: InstanceManagerMixin 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. .. py:method:: save_globally(overwrite: bool = False) -> None Save this instance in a global list for accessibility using its identifier. :param overwrite: 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 .. py:method:: remove_from_global_list() -> None If this instance is saved in the global list, remove it. .. py:method:: clear_instances(all_types: bool = False) -> None :classmethod: Clear the list of globally saved instances of the current derived class. :param all_types: also clear instances of other derived classes. .. py:method:: from_id(identifier: int) -> Self :classmethod: Return the instance with the given identifier that is stored in the global list. :param identifier: Identifier of the instance to retrieve. :raise KeyError: If no iinstance with this ID was found in the global list. :return: the instance belonging to this identifier. .. py:method:: from_id_arguments(*args: Any, **kwargs: Any) -> Self :classmethod: 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. :param \*args: regular arguments that would normally go into the constructor :param \**kwargs: regular keyword arguments that would normally go into the constructor :return: An instance with the same ID if one has been saved globally .. py:property:: identifier :type: 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. :raise KeyError: In cast there is a mismatch between the argument names in id_from_arguments and the parameter names and properties of the class. :raise TypeError: At least one argument name from id_from_arguments correponds with a callable rather than an attribute or property. :raise AttributeError: If the class does not have the attribute __identifier, which can happen if the constructor is not called. :return: An identifier of type int .. py:method:: id_from_arguments(*args: Any, **kwargs: Any) -> int :classmethod: :abstractmethod: 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. :param \*args: regular arguments :param \**kwargs: regular keyword arguments :return: identifier of type int