PNNL-CIM-Tools / CIM-Graph

Python library for creating in-memory labeled property graphs for creating, parsing, and editing CIM power system models
https://cimantic-graphs.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11 stars 8 forks source link

Add seed argument to cim.Identity.uuid() function #79

Open afisher1 opened 1 month ago

afisher1 commented 1 month ago

All the user to set the seed for the random number generator in cim.Identity.uuid(mRID, uri, name) by adding a fourth argument seed which can be a string or int. if mRID, uri, or name is defined it behaves exactly the same as it currently does regardless of whether seed was set or not. seed is only used when the other arguments are None.

afisher1 commented 1 week ago
def uuid(self, mRID:str = None, uri:str = None, name:str = None**, seed:str = None**) -> UUID:
        **if not seed:
            seed = ''**
        invalid_mrid = True
        self.__uuid__ = self.__uuid_meta__()
        # If URI is specified, try creating from UUID from URI
        if uri is not None:
            # Handle inconsistent capitalization / underscores
            if uri.strip('_') != uri:
                self.__uuid__.uri_has_underscore = True
            if uri.lower() != uri:
                self.__uuid__.uri_is_capitalized = True
            try:
                self.identifier = UUID(uri.strip('_').lower())
                invalid_mrid = False
            except:
                seed = seed + uri
                _log.warning(f'URI {uri} not a valid UUID, generating new UUID')
        if mRID is not None:
            # Handle inconsistent capitalization / underscores
            if mRID.strip('_') != mRID:
                self.__uuid__.mrid_has_underscore = True
                if uri is None:
                    self.__uuid__.uri_has_underscore = True
            if mRID.lower() != mRID:
                self.__uuid__.mrid_is_capitalized = True
                if uri is None:
                    self.__uuid__.uri_is_capitalized = True
                try:
                    self.identifier = UUID(mRID.strip('_').lower())
                    invalid_mrid = False
                except:
                    self.mRID = mRID
                    seed = seed + mRID
                    _log.warning(f'mRID {mRID} not a valid UUID, generating new UUID')
        # Otherwise, build UUID using unique name as a seed
        if invalid_mrid:
            if name is not None:
                seed = seed + f'{self.__class__.__name__}:{name}'
                randomGenerator = Random(seed)
                self.__uuid__.uuid = UUID(int=randomGenerator.getrandbits(128), version=4)
                self.name = name
            **elif seed is not None:
                randomGenerator = Random(seed)
                self.__uuid__.uuid = UUID(int=randomGenerator.getrandbits(128), version=4)**
            else:
                self.__uuid__.uuid = uuid4()
            self.identifier = self.__uuid__.uuid
            # Write mRID string for backwards compatibility
            if 'mRID' in self.__dataclass_fields__:
                if mRID is not None:
                    self.mRID = mRID
                else:
                    self.mRID = str(self.identifier)