[docs]classRegistry(Generic[T]):""" A generic registry to store and retrieve items by their identifiers. """
[docs]def__init__(self):""" Initialize an empty registry. """self._registry:Dict[Identifier,T]={}
[docs]defregister(self,identifier:Identifier,value:T):""" Register an item with its identifier in the registry. :param identifier: The identifier for the item. :type identifier: Identifier :param value: The item to be registered. :type value: T :raises ValueError: If the identifier is not an instance of the Identifier class. """ifnotisinstance(identifier,Identifier):raiseValueError("Identifier must be an instance of Identifier class.")self._registry[identifier]=value
[docs]defget(self,identifier:Identifier)->T|None:""" Retrieve an item from the registry by its identifier. :param identifier: The identifier of the item to retrieve. :type identifier: Identifier :return: The item associated with the identifier, or None if not found. :rtype: T | None """try:returnself._registry.get(identifier)exceptValueError:returnNone
[docs]defget_id(self,value:T)->Identifier|None:""" Retrieve the identifier of an item in the registry. :param value: The item whose identifier is to be retrieved. :type value: T :return: The identifier associated with the item, or None if not found. :rtype: Identifier | None """try:forkey,dvalueinself._registry.items():ifdvalue==value:returnkeyexceptValueError:returnNonereturnNone