X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=id_generator.py;fp=id_generator.py;h=4e650dca9f13662a1ace7013d4f00bcd3a0082e2;hb=709370b2198e09f1dbe195fe8813602a3125b7f6;hp=c5a0d93e6908838c1f382b386a64957f3c2ea3fc;hpb=b10d30a46e601c9ee1f843241f2d69a1f90f7a94;p=python_utils.git diff --git a/id_generator.py b/id_generator.py index c5a0d93..4e650dc 100644 --- a/id_generator.py +++ b/id_generator.py @@ -12,11 +12,22 @@ generators = {} def get(name: str) -> int: """ - def __init__(self): - self.my_unique_id = id_generator.get("student_id") + Returns a thread safe monotonically increasing id suitable for use + as a globally unique identifier. + + >>> import id_generator + >>> id_generator.get('student_id') + 0 + >>> id_generator.get('student_id') + 1 """ if name not in generators: generators[name] = itertools.count() x = next(generators[name]) logger.debug(f"Generated next id {x}") return x + + +if __name__ == '__main__': + import doctest + doctest.testmod()