[docs]classIdentifier:""" A class to represent an identifier with a namespace and an ID. :param namespace: The namespace of the identifier. :type namespace: str :param id: The ID of the identifier. :type id: str """
[docs]def__init__(self,namespace:str,id:str):""" Initialize an Identifier instance. :param namespace: The namespace of the identifier. :type namespace: str :param id: The ID of the identifier. :type id: str """self.namespace=namespaceself.id=id
[docs]@classmethoddeffrom_string(cls,namespaced_string:str):""" Create an Identifier instance from a namespaced string. :param namespaced_string: A string in the format 'namespace:id'. :type namespaced_string: str :return: An Identifier instance. :rtype: Identifier :raises ValueError: If the string is not in the format 'namespace:id'. """try:namespace,id=namespaced_string.split(':')returncls(namespace,id)exceptValueError:raiseValueError("String must be in the format 'namespace:id'")
def__eq__(self,other):""" Check equality with another Identifier instance. :param other: Another Identifier instance to compare with. :type other: Identifier :return: True if both Identifiers are equal, False otherwise. :rtype: bool """ifisinstance(other,Identifier):returnself.namespace==other.namespaceandself.id==other.idreturnFalsedef__hash__(self):""" Compute the hash value of the Identifier. :return: The hash value of the Identifier. :rtype: int """returnhash((self.namespace,self.id))def__str__(self):""" Return the string representation of the Identifier. :return: The string representation in the format 'namespace:id'. :rtype: str """returnf"{self.namespace}:{self.id}"