Repository navigation
Expand file tree
/
Copy pathtime.py
More file actions
479 lines (406 loc) · 16.6 KB
/
Copy pathtime.py
File metadata and controls
479 lines (406 loc) · 16.6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
"""
This module provides utility functions for handling time-related operations.
Functions::
timedelta_to_seconds: Convert a timedelta to seconds (microseconds as
fraction).
delta_to_seconds: Convert a timedelta or numeric interval to seconds.
delta_to_seconds_or_none: Convert a timedelta to seconds or return None.
format_time: Format a timestamp (timedelta, datetime, or seconds).
timeout_generator: Generate items from an iterable until a timeout.
aio_timeout_generator: Async generate items from an iterable until a
timeout.
aio_generator_timeout_detector: Detect if an async generator has stalled.
aio_generator_timeout_detector_decorator: Decorator for the detector.
"""
# pyright: reportUnnecessaryIsInstance=false
import collections.abc
import datetime
import functools
import itertools
import time
import typing
import python_utils
from python_utils import _aliases, exceptions
#: Item type produced by the time/timeout generators.
_T = typing.TypeVar('_T')
#: Parameter specification for the timeout-detector decorator's target.
_P = typing.ParamSpec('_P')
#: The Unix epoch (1970-01-01) as a naive ``datetime``, used as a reference.
# There might be a better way to get the epoch with tzinfo; please open a
# pull request if you know one.
epoch = datetime.datetime(year=1970, month=1, day=1)
def timedelta_to_seconds(delta: datetime.timedelta) -> _aliases.Number:
"""Convert a timedelta to seconds with the microseconds as fraction.
Note that this method has become largely obsolete with the
`timedelta.total_seconds()` method introduced in Python 2.7.
>>> from datetime import timedelta
>>> '%d' % timedelta_to_seconds(timedelta(days=1))
'86400'
>>> '%d' % timedelta_to_seconds(timedelta(seconds=1))
'1'
>>> '%.6f' % timedelta_to_seconds(timedelta(seconds=1, microseconds=1))
'1.000001'
>>> '%.6f' % timedelta_to_seconds(timedelta(microseconds=1))
'0.000001'
"""
seconds: int = delta.seconds + delta.days * 60 * 60 * 24
# Only convert to float if needed
if delta.microseconds:
# Divide the whole microseconds once, the way `total_seconds()` does.
# Adding a float fraction to a large number of seconds loses digits.
return (seconds * 10**6 + delta.microseconds) / 10**6
else:
return seconds
def delta_to_seconds(interval: _aliases.delta_type) -> _aliases.Number:
"""
Convert a timedelta to seconds.
>>> delta_to_seconds(datetime.timedelta(seconds=1))
1
>>> delta_to_seconds(datetime.timedelta(seconds=1, microseconds=1))
1.000001
>>> delta_to_seconds(1)
1
>>> delta_to_seconds('whatever') # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: Unknown type ...
"""
if isinstance(interval, datetime.timedelta):
return timedelta_to_seconds(interval)
elif isinstance(interval, (int, float)):
return interval
else:
raise TypeError(f'Unknown type {type(interval)}: {interval!r}')
def delta_to_seconds_or_none(
interval: _aliases.delta_type | None,
) -> _aliases.Number | None:
"""Convert a timedelta to seconds, passing ``None`` through unchanged.
Args:
interval: A timedelta or a number of seconds, or ``None``.
Returns:
The interval in seconds, or ``None`` when ``interval`` is ``None``.
>>> delta_to_seconds_or_none(datetime.timedelta(seconds=2))
2
>>> delta_to_seconds_or_none(None) is None
True
"""
if interval is None:
return None
else:
return delta_to_seconds(interval)
def format_time(
timestamp: _aliases.timestamp_type,
precision: datetime.timedelta = datetime.timedelta(seconds=1),
) -> str:
"""Formats timedelta/datetime/seconds.
>>> format_time('1')
'0:00:01'
>>> format_time(1.234)
'0:00:01'
>>> format_time(1)
'0:00:01'
>>> format_time(datetime.datetime(2000, 1, 2, 3, 4, 5, 6))
'2000-01-02 03:04:05'
>>> format_time(datetime.date(2000, 1, 2))
'2000-01-02'
>>> format_time(datetime.timedelta(seconds=3661))
'1:01:01'
>>> format_time(None)
'--:--:--'
>>> format_time(format_time) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: Unknown type ...
"""
if isinstance(timestamp, str):
timestamp = float(timestamp)
if isinstance(timestamp, (int, float)):
try:
timestamp = datetime.timedelta(seconds=timestamp)
except (OverflowError, ValueError):
# Too large for a timedelta, or not a number at all: nan raises
# a ValueError where infinity raises an OverflowError.
timestamp = None
if isinstance(timestamp, datetime.timedelta):
# Truncate the number to the given precision. A timedelta counts in
# whole microseconds, which keeps the modulo exact. In float seconds
# `1.0 % 0.1` is just under 0.1 and the result drops a whole step.
return str(timestamp - timestamp % precision)
elif isinstance(timestamp, datetime.datetime): # pragma: no cover
# Python 2 doesn't have the timestamp method
if hasattr(timestamp, 'timestamp'):
seconds = timestamp.timestamp()
else:
seconds = timedelta_to_seconds(timestamp - epoch)
# Truncate the number to the given precision, in whole microseconds
# for the same reason as above
since_epoch: datetime.timedelta = datetime.timedelta(seconds=seconds)
seconds = (since_epoch - since_epoch % precision).total_seconds()
try: # pragma: no cover
dt = datetime.datetime.fromtimestamp(seconds)
except (ValueError, OSError): # pragma: no cover
dt = datetime.datetime.max
return str(dt)
elif isinstance(timestamp, datetime.date):
return str(timestamp)
elif timestamp is None:
return '--:--:--'
else:
raise TypeError(f'Unknown type {type(timestamp)}: {timestamp!r}')
@typing.overload
def _to_iterable(
iterable: collections.abc.Callable[[], collections.abc.AsyncIterable[_T]]
| collections.abc.AsyncIterable[_T],
) -> collections.abc.AsyncIterable[_T]:
"""Async overload: async iterable or factory in, async iterable out."""
@typing.overload
def _to_iterable(
iterable: collections.abc.Callable[[], collections.abc.Iterable[_T]]
| collections.abc.Iterable[_T],
) -> collections.abc.Iterable[_T]:
"""Sync overload: sync iterable or factory in, sync iterable out."""
def _to_iterable(
iterable: collections.abc.Iterable[_T]
| collections.abc.Callable[[], collections.abc.Iterable[_T]]
| collections.abc.AsyncIterable[_T]
| collections.abc.Callable[[], collections.abc.AsyncIterable[_T]],
) -> collections.abc.Iterable[_T] | collections.abc.AsyncIterable[_T]:
"""Return ``iterable``, calling it first if it is a zero-arg callable."""
if callable(iterable):
return iterable()
else:
return iterable
def timeout_generator(
timeout: _aliases.delta_type,
interval: _aliases.delta_type = datetime.timedelta(seconds=1),
iterable: collections.abc.Iterable[_T]
| collections.abc.Callable[
[], collections.abc.Iterable[_T]
] = itertools.count, # type: ignore[assignment]
interval_multiplier: float = 1.0,
maximum_interval: _aliases.delta_type | None = None,
) -> collections.abc.Iterable[_T]:
"""
Generator that walks through the given iterable (a counter by default)
until the float_timeout is reached with a configurable float_interval
between items.
This can be used to limit the time spent on a slow operation. This can be
useful for testing slow APIs so you get a small sample of the data in a
reasonable amount of time.
After every sleep the interval is multiplied by `interval_multiplier`. No
sleep is longer than `maximum_interval`, and that includes the first one.
A `maximum_interval` of `None` or `0` means that there is no maximum.
>>> for i in timeout_generator(0.1, 0.06):
... # Put your slow code here
... print(i)
0
1
2
>>> timeout = datetime.timedelta(seconds=0.1)
>>> interval = datetime.timedelta(seconds=0.06)
>>> for i in timeout_generator(timeout, interval, itertools.count()):
... print(i)
0
1
2
>>> for i in timeout_generator(1, interval=0.1, iterable='ab'):
... print(i)
a
b
>>> timeout = datetime.timedelta(seconds=0.1)
>>> interval = datetime.timedelta(seconds=0.06)
>>> for i in timeout_generator(timeout, interval, interval_multiplier=2):
... print(i)
0
1
2
"""
float_interval: float = delta_to_seconds(interval)
float_maximum_interval: float | None = delta_to_seconds_or_none(
maximum_interval
)
iterable_ = _to_iterable(iterable)
end = delta_to_seconds(timeout) + time.perf_counter()
for item in iterable_:
yield item
if time.perf_counter() >= end:
break
# The maximum holds for the first sleep as well. Zero is not a
# maximum here, it means the same as `None`.
if float_maximum_interval:
time.sleep(min(float_interval, float_maximum_interval))
else:
time.sleep(float_interval)
float_interval *= interval_multiplier
if float_maximum_interval:
float_interval = min(float_interval, float_maximum_interval)
async def aio_timeout_generator(
timeout: _aliases.delta_type, # noqa: ASYNC109
interval: _aliases.delta_type = datetime.timedelta(seconds=1),
iterable: collections.abc.AsyncIterable[_T]
| collections.abc.Callable[..., collections.abc.AsyncIterable[_T]]
| None = None,
interval_multiplier: float = 1.0,
maximum_interval: _aliases.delta_type | None = None,
) -> collections.abc.AsyncGenerator[_T, None]:
"""
Async generator that walks through the given async iterable (a counter by
default) until the float_timeout is reached with a configurable
float_interval between items.
After every sleep the interval is multiplied by `interval_multiplier`. To
double the interval with each run, specify 2. A value below 1 makes the
interval shorter with each run. No sleep is longer than
`maximum_interval`, and that includes the first one. A `maximum_interval`
of `None` or `0` means that there is no maximum.
Doctests and asyncio are not friends, so no examples. But this function is
effectively the same as the `timeout_generator` but it uses `async for`
instead.
"""
# Imported lazily so that importing `python_utils.time` for its
# synchronous helpers (e.g. ``format_time``) does not pull in ``asyncio``.
import asyncio
from python_utils import aio
if iterable is None:
iterable = typing.cast(
collections.abc.Callable[[], collections.abc.AsyncIterable[_T]],
aio.acount,
)
float_interval: float = delta_to_seconds(interval)
float_maximum_interval: float | None = delta_to_seconds_or_none(
maximum_interval
)
iterable_ = _to_iterable(iterable)
end = delta_to_seconds(timeout) + time.perf_counter()
async for item in iterable_: # pragma: no branch
yield item
if time.perf_counter() >= end:
break
# The maximum holds for the first sleep as well. Zero is not a
# maximum here, it means the same as `None`.
if float_maximum_interval:
await asyncio.sleep(min(float_interval, float_maximum_interval))
else:
await asyncio.sleep(float_interval)
float_interval *= interval_multiplier
if float_maximum_interval: # pragma: no branch
float_interval = min(float_interval, float_maximum_interval)
async def aio_generator_timeout_detector(
generator: collections.abc.AsyncGenerator[_T, None],
timeout: _aliases.delta_type | None = None, # noqa: ASYNC109
total_timeout: _aliases.delta_type | None = None,
on_timeout: collections.abc.Callable[
[
collections.abc.AsyncGenerator[_T, None],
_aliases.delta_type | None,
_aliases.delta_type | None,
BaseException,
],
typing.Any,
]
| None = exceptions.reraise,
**on_timeout_kwargs: collections.abc.Mapping[str, typing.Any],
) -> collections.abc.AsyncGenerator[_T, None]:
"""
This function is used to detect if an asyncio generator has not yielded
an element for a set amount of time.
The `timeout` is the time a single element may take. A `timeout` of `None`
or `0` means that there is no timeout per element. The `total_timeout` is
the time all elements together may take. It is checked between elements,
so it does not end the wait for an element that does not arrive. Use
`timeout` for that.
The `on_timeout` argument is called with the `generator`, `timeout`,
`total_timeout`, `exception` and the extra `**kwargs` to this function as
arguments. It can be a plain function or a coroutine function.
If `on_timeout` is not specified, the exception is reraised.
If `on_timeout` is `None`, the exception is silently ignored and the
generator will finish as normal.
"""
# Imported lazily so importing `python_utils.time` stays asyncio-free.
import asyncio
if total_timeout is None:
total_timeout_end = None
else:
total_timeout_end = time.perf_counter() + delta_to_seconds(
total_timeout
)
timeout_s = python_utils.delta_to_seconds_or_none(timeout)
while True:
try:
if total_timeout_end and time.perf_counter() >= total_timeout_end:
raise asyncio.TimeoutError( # noqa: TRY301
'Total timeout reached'
)
if timeout_s:
yield await asyncio.wait_for(generator.__anext__(), timeout_s)
else:
yield await generator.__anext__()
except asyncio.TimeoutError as exception: # noqa: PERF203
if on_timeout is not None:
result: typing.Any = on_timeout(
generator,
timeout,
total_timeout,
exception,
**on_timeout_kwargs,
)
# A coroutine function hands back something to await, a
# plain function has already done its work by now.
if isinstance(result, collections.abc.Awaitable):
await result
break
except StopAsyncIteration:
break
def aio_generator_timeout_detector_decorator(
timeout: _aliases.delta_type | None = None,
total_timeout: _aliases.delta_type | None = None,
on_timeout: collections.abc.Callable[
[
collections.abc.AsyncGenerator[typing.Any, None],
_aliases.delta_type | None,
_aliases.delta_type | None,
BaseException,
],
typing.Any,
]
| None = exceptions.reraise,
**on_timeout_kwargs: collections.abc.Mapping[str, typing.Any],
) -> collections.abc.Callable[
[collections.abc.Callable[_P, collections.abc.AsyncGenerator[_T, None]]],
collections.abc.Callable[_P, collections.abc.AsyncGenerator[_T, None]],
]:
"""Wrap a generator function with ``aio_generator_timeout_detector``.
Args:
timeout: Per-item timeout. If a single yield takes longer,
``on_timeout`` fires. ``None`` or ``0`` disables the per-item
check.
total_timeout: Overall timeout across the whole generator. It is
checked between items.
on_timeout: Callback invoked on a timeout; defaults to re-raising.
**on_timeout_kwargs: Extra keyword arguments passed to ``on_timeout``.
Returns:
A decorator that wraps an async-generator function so every call is
guarded against stalls.
"""
def _timeout_detector_decorator(
generator: collections.abc.Callable[
_P, collections.abc.AsyncGenerator[_T, None]
],
) -> collections.abc.Callable[
_P, collections.abc.AsyncGenerator[_T, None]
]:
"""Wrap ``generator`` so each call is timeout-guarded."""
@functools.wraps(generator)
def wrapper(
*args: _P.args,
**kwargs: _P.kwargs,
) -> collections.abc.AsyncGenerator[_T, None]:
"""Forward the call to ``aio_generator_timeout_detector``."""
return aio_generator_timeout_detector(
generator(*args, **kwargs),
timeout,
total_timeout,
on_timeout,
**on_timeout_kwargs,
)
return wrapper
return _timeout_detector_decorator