Source code for mojo.jobManagement.management.idGenerator

"""
IdGenerator
===========
IdGenerator is a class build as "Borgpattern". Every instance will share the state.
Therefore getId generates global unique ids in every Thread. The first id is one.
By calling reset the ids will start at one again.
"""


[docs]class IdGenerator: """Class to create unique ids. Can be reseted. """ __shared_state = {} def __init__(self): """Every instance of IdGenerator gets as self.__dict__ the classvariable __shared_state. Therefore is the state of all instances the same. If no start-id is set, the currentId will be set to zero. """ self.__dict__ = self.__shared_state try: self.__currentId except AttributeError: self.__currentId = -1 def __call__(self): """Generates and returns a new id when called. """ return self.getId()
[docs] def getId(self): """Increases the current id and returns it. :return: a new unique id :rtype: int """ self.__currentId = self.__currentId + 1 return self.__currentId
[docs] def reset(self): """Set the current id to zero again. """ self.__currentId = -1