-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path_utils.py
More file actions
60 lines (44 loc) · 1.57 KB
/
_utils.py
File metadata and controls
60 lines (44 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
import time
from collections.abc import Iterable, Iterator
from itertools import islice
from typing import Callable, TypeVar
T = TypeVar("T")
def batched(iterable: Iterable[T], size: int) -> Iterator[tuple[T, ...]]:
"""
Returns a batch of the provided size from the provided iterable.
"""
iterator = iter(iterable)
while True:
batch = tuple(islice(iterator, size))
if not batch:
break
yield batch
def waiter(timeout: float | None = None) -> Callable[[float], bool]:
"""
Waiter returns a wait function that sleeps the specified amount of seconds, and
handles timeouts.
The wait function returns True if the timeout was reached, False otherwise.
:param timeout: Timeout in seconds, defaults to None.
:return: Wait function.
"""
if timeout:
deadline = time.time() + timeout
def wait(seconds: float) -> bool:
now = time.time()
# Timeout if the deadline exceeded.
if deadline < now:
return True
# The deadline is not exceeded after the sleep time.
if now + seconds < deadline:
time.sleep(seconds)
return False
# The deadline is exceeded after the sleep time, clamp sleep time to
# deadline, and allow one last attempt until next wait call.
time.sleep(deadline - now)
return False
else:
def wait(seconds: float) -> bool:
time.sleep(seconds)
return False
return wait