1- from typing import Awaitable , Callable , Generator , List , Optional , TypeAlias , TypeVar
1+ from typing import overload
22
33try :
44 from typing import Self
@@ -12,19 +12,9 @@ SONYFLAKE_MACHINE_ID_BITS: int
1212SONYFLAKE_MACHINE_ID_MAX : int
1313SONYFLAKE_MACHINE_ID_OFFSET : int
1414SONYFLAKE_TIME_OFFSET : int
15- AsyncSleep : TypeAlias = Callable [[float ], Awaitable [None ]]
16- T = TypeVar ("T" )
17-
18- async def sleep_wrapper (obj : T , sleep : AsyncSleep , to_sleep : float ) -> T :
19- """C version of:
20-
21- async def sleep_wrapper(obj, sleep, to_sleep):
22- await sleep(to_sleep)
23- return obj
24- """
2515
2616class SonyFlake :
27- def __init__ (self , * machine_id : int , start_time : Optional [ int ] = None ):
17+ def __init__ (self , * machine_id : int , start_time : int | None = None ):
2818 """Initialize SonyFlake ID generator.
2919
3020 Args:
@@ -47,7 +37,7 @@ class SonyFlake:
4737 def __next__ (self ) -> int :
4838 """Produce a SonyFlake ID."""
4939
50- def __call__ (self , n : int , / ) -> List [int ]:
40+ def __call__ (self , n : int , / ) -> list [int ]:
5141 """Generate multiple SonyFlake IDs at once.
5242
5343 Roughly equivalent to `[next(sf) for _ in range(n)]`, but more
@@ -68,76 +58,10 @@ class SonyFlake:
6858 List of ids.
6959 """
7060
71- class AsyncSonyFlake :
72- """Async wrapper for :class:`SonyFlake`.
73-
74- Implements Awaitable and AsyncIterator protocols.
75-
76- Important:
77- Main state is stored in SonyFlake. This class has minimum logic on its
78- own, the only difference is that instead of doing thread-blocking sleep,
79- it is delegated to the provided ``sleep``. Instance of
80- class:`sleep_wrapper` is returned in ``__call__``, ``__anext__`` and
81- ``__await``.
82-
83- Usage:
84-
85- .. code-block:: python
86-
87- import asyncio
88-
89- sf = SonyFlake(0x1337, 0xCAFE, start_time=1749081600)
90- asf = AsyncSonyFlake(sf, asyncio.sleep)
91-
92- print(await asf)
93- print(await asf(5))
94-
95- async for id_ in asf:
96- print(id_)
97- break # AsyncSonyFlake is an infinite generator
98- """
99-
100- def __init__ (self , sf : SonyFlake , sleep : AsyncSleep ) -> None :
101- """Initialize AsyncSonyFlake ID generator.
102-
103- Args:
104- sf: Instance of the :class:`SonyFlake`.
105- sleep: Either `asyncio.sleep` or `trio.sleep`.
106-
107- Raises:
108- ValueError: Invalid values of ``machine_id`` or ``start_time``.
109- TypeError: ``machine_id`` or ``start_time`` are not integers.
110- """
111-
112- def __call__ (self , n : int ) -> Awaitable [list [int ]]:
113- """Generate multiple SonyFlake IDs at once.
114-
115- Roughly equivalent to `[await asf for _ in range(n)]`, but more
116- efficient. This method saves on task/context switches and syscalls for
117- getting current time.
118-
119- Important:
120- The more ids you request, the more other coroutines have to wait
121- upon the next id(s).
122-
123- Args:
124- n: Number of ids to generate. Must be greater than 0.
125-
126- Raises:
127- ValueError: if n <= 0
128-
129- Returns:
130- List of ids.
131- """
132-
133- def __await__ (self ) -> Generator [None , None , int ]:
134- """Produce a SonyFlake ID."""
135-
136- def __anext__ (self ) -> Awaitable [int ]:
137- """Produce a SonyFlake ID."""
138-
139- def __aiter__ (self ) -> Self :
140- """Returns ``self``."""
61+ @overload
62+ def _raw (self , n : int ) -> tuple [list [int ], float ]: ...
63+ @overload
64+ def _raw (self , n : None ) -> tuple [int , float ]: ...
14165
14266class MachineIDLCG :
14367 """A simple LCG producing ints suitable to be used as ``machine_id``.
0 commit comments