3 # © Copyright 2021-2022, Scott Gasch
5 """A helper class for generating thread safe monotonically increasing
13 # This module is commonly used by others in here and should avoid
14 # taking any unnecessary dependencies back on them.
16 logger = logging.getLogger(__name__)
20 def get(name: str, *, start=0) -> int:
22 Returns a thread-safe, monotonically increasing id suitable for use
23 as a globally unique identifier.
25 >>> import id_generator
26 >>> id_generator.get('student_id')
28 >>> id_generator.get('student_id')
30 >>> id_generator.get('employee_id', start=10000)
32 >>> id_generator.get('employee_id', start=10000)
35 if name not in generators:
36 generators[name] = itertools.count(start, 1)
37 x = next(generators[name])
38 logger.debug("Generated next id %d", x)
42 if __name__ == '__main__':