Optionally surface exceptions that happen under executors by reading
[python_utils.git] / id_generator.py
index c5a0d93e6908838c1f382b386a64957f3c2ea3fc..d4c70166bb3c52974793840cb8bebbf288500a49 100644 (file)
@@ -10,13 +10,29 @@ logger = logging.getLogger(__name__)
 generators = {}
 
 
-def get(name: str) -> int:
+def get(name: str, *, start=0) -> 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
+    >>> id_generator.get('employee_id', start=10000)
+    10000
+    >>> id_generator.get('employee_id', start=10000)
+    10001
     """
     if name not in generators:
-        generators[name] = itertools.count()
+        generators[name] = itertools.count(start, 1)
     x = next(generators[name])
     logger.debug(f"Generated next id {x}")
     return x
+
+
+if __name__ == '__main__':
+    import doctest
+
+    doctest.testmod()