dycw-utilities 0.175.17__py3-none-any.whl → 0.185.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dycw_utilities-0.185.8.dist-info/METADATA +33 -0
- dycw_utilities-0.185.8.dist-info/RECORD +90 -0
- {dycw_utilities-0.175.17.dist-info → dycw_utilities-0.185.8.dist-info}/WHEEL +2 -2
- utilities/__init__.py +1 -1
- utilities/altair.py +8 -6
- utilities/asyncio.py +40 -56
- utilities/atools.py +9 -11
- utilities/cachetools.py +8 -6
- utilities/click.py +4 -3
- utilities/concurrent.py +1 -1
- utilities/constants.py +492 -0
- utilities/contextlib.py +23 -30
- utilities/contextvars.py +1 -23
- utilities/core.py +2581 -0
- utilities/dataclasses.py +16 -119
- utilities/docker.py +139 -45
- utilities/enum.py +1 -1
- utilities/errors.py +2 -16
- utilities/fastapi.py +5 -5
- utilities/fpdf2.py +2 -1
- utilities/functions.py +33 -264
- utilities/http.py +2 -3
- utilities/hypothesis.py +48 -25
- utilities/iterables.py +39 -575
- utilities/jinja2.py +3 -6
- utilities/jupyter.py +5 -3
- utilities/libcst.py +1 -1
- utilities/lightweight_charts.py +4 -6
- utilities/logging.py +17 -15
- utilities/math.py +1 -36
- utilities/more_itertools.py +4 -6
- utilities/numpy.py +2 -1
- utilities/operator.py +2 -2
- utilities/orjson.py +24 -25
- utilities/os.py +4 -185
- utilities/packaging.py +129 -0
- utilities/parse.py +33 -13
- utilities/pathlib.py +2 -136
- utilities/platform.py +8 -90
- utilities/polars.py +34 -31
- utilities/postgres.py +9 -4
- utilities/pottery.py +20 -18
- utilities/pqdm.py +3 -4
- utilities/psutil.py +2 -3
- utilities/pydantic.py +18 -4
- utilities/pydantic_settings.py +7 -9
- utilities/pydantic_settings_sops.py +3 -3
- utilities/pyinstrument.py +4 -4
- utilities/pytest.py +49 -108
- utilities/pytest_plugins/pytest_regressions.py +2 -2
- utilities/pytest_regressions.py +8 -6
- utilities/random.py +2 -8
- utilities/redis.py +98 -94
- utilities/reprlib.py +11 -118
- utilities/shellingham.py +66 -0
- utilities/slack_sdk.py +13 -12
- utilities/sqlalchemy.py +42 -30
- utilities/sqlalchemy_polars.py +16 -25
- utilities/subprocess.py +1166 -148
- utilities/tabulate.py +32 -0
- utilities/testbook.py +8 -8
- utilities/text.py +24 -115
- utilities/throttle.py +159 -0
- utilities/time.py +18 -0
- utilities/timer.py +29 -12
- utilities/traceback.py +15 -22
- utilities/types.py +38 -3
- utilities/typing.py +18 -12
- utilities/uuid.py +1 -1
- utilities/version.py +202 -45
- utilities/whenever.py +22 -150
- dycw_utilities-0.175.17.dist-info/METADATA +0 -34
- dycw_utilities-0.175.17.dist-info/RECORD +0 -103
- utilities/atomicwrites.py +0 -182
- utilities/cryptography.py +0 -41
- utilities/getpass.py +0 -8
- utilities/git.py +0 -19
- utilities/grp.py +0 -28
- utilities/gzip.py +0 -31
- utilities/json.py +0 -70
- utilities/permissions.py +0 -298
- utilities/pickle.py +0 -25
- utilities/pwd.py +0 -28
- utilities/re.py +0 -156
- utilities/sentinel.py +0 -73
- utilities/socket.py +0 -8
- utilities/string.py +0 -20
- utilities/tempfile.py +0 -136
- utilities/tzdata.py +0 -11
- utilities/tzlocal.py +0 -28
- utilities/warnings.py +0 -65
- utilities/zipfile.py +0 -25
- utilities/zoneinfo.py +0 -133
- {dycw_utilities-0.175.17.dist-info → dycw_utilities-0.185.8.dist-info}/entry_points.txt +0 -0
utilities/redis.py
CHANGED
|
@@ -20,14 +20,15 @@ from typing import (
|
|
|
20
20
|
|
|
21
21
|
from redis.asyncio import Redis
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
import utilities.asyncio
|
|
24
|
+
from utilities.asyncio import timeout
|
|
25
|
+
from utilities.constants import MILLISECOND, SECOND
|
|
24
26
|
from utilities.contextlib import enhanced_async_context_manager
|
|
27
|
+
from utilities.core import always_iterable, identity, is_pytest, one
|
|
25
28
|
from utilities.errors import ImpossibleCaseError
|
|
26
|
-
from utilities.functions import ensure_int,
|
|
27
|
-
from utilities.
|
|
28
|
-
from utilities.os import is_pytest
|
|
29
|
+
from utilities.functions import ensure_int, in_milli_seconds, in_seconds
|
|
30
|
+
from utilities.math import safe_round
|
|
29
31
|
from utilities.typing import is_instance_gen
|
|
30
|
-
from utilities.whenever import MILLISECOND, SECOND, to_milliseconds, to_nanoseconds
|
|
31
32
|
|
|
32
33
|
if TYPE_CHECKING:
|
|
33
34
|
from collections.abc import AsyncIterator, Awaitable, Collection, Iterable
|
|
@@ -36,11 +37,16 @@ if TYPE_CHECKING:
|
|
|
36
37
|
from redis.asyncio.client import PubSub
|
|
37
38
|
from redis.typing import EncodableT
|
|
38
39
|
|
|
39
|
-
from utilities.
|
|
40
|
-
|
|
40
|
+
from utilities.types import (
|
|
41
|
+
Duration,
|
|
42
|
+
MaybeIterable,
|
|
43
|
+
MaybeSequence,
|
|
44
|
+
MaybeType,
|
|
45
|
+
TypeLike,
|
|
46
|
+
)
|
|
41
47
|
|
|
42
48
|
|
|
43
|
-
_PUBLISH_TIMEOUT:
|
|
49
|
+
_PUBLISH_TIMEOUT: Duration = SECOND
|
|
44
50
|
|
|
45
51
|
|
|
46
52
|
##
|
|
@@ -57,16 +63,16 @@ class RedisHashMapKey[K, V]:
|
|
|
57
63
|
value: TypeLike[V]
|
|
58
64
|
value_serializer: Callable[[V], bytes] | None = None
|
|
59
65
|
value_deserializer: Callable[[bytes], V] | None = None
|
|
60
|
-
timeout:
|
|
66
|
+
timeout: Duration | None = None
|
|
61
67
|
error: MaybeType[BaseException] = TimeoutError
|
|
62
|
-
ttl:
|
|
68
|
+
ttl: Duration | None = None
|
|
63
69
|
|
|
64
70
|
async def delete(self, redis: Redis, key: K, /) -> int:
|
|
65
71
|
"""Delete a key from a hashmap in `redis`."""
|
|
66
72
|
ser = _serialize( # skipif-ci-and-not-linux
|
|
67
73
|
key, serializer=self.key_serializer
|
|
68
74
|
).decode()
|
|
69
|
-
async with
|
|
75
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
70
76
|
self.timeout, error=self.error
|
|
71
77
|
):
|
|
72
78
|
return await cast("Awaitable[int]", redis.hdel(self.name, ser))
|
|
@@ -77,7 +83,7 @@ class RedisHashMapKey[K, V]:
|
|
|
77
83
|
ser = _serialize( # skipif-ci-and-not-linux
|
|
78
84
|
key, serializer=self.key_serializer
|
|
79
85
|
).decode()
|
|
80
|
-
async with
|
|
86
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
81
87
|
self.timeout, error=self.error
|
|
82
88
|
):
|
|
83
89
|
return await cast("Awaitable[bool]", redis.hexists(self.name, ser))
|
|
@@ -91,7 +97,7 @@ class RedisHashMapKey[K, V]:
|
|
|
91
97
|
|
|
92
98
|
async def get_all(self, redis: Redis, /) -> Mapping[K, V]:
|
|
93
99
|
"""Get a value from a hashmap in `redis`."""
|
|
94
|
-
async with
|
|
100
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
95
101
|
self.timeout, error=self.error
|
|
96
102
|
):
|
|
97
103
|
result = await cast( # skipif-ci-and-not-linux
|
|
@@ -112,7 +118,7 @@ class RedisHashMapKey[K, V]:
|
|
|
112
118
|
ser = [ # skipif-ci-and-not-linux
|
|
113
119
|
_serialize(key, serializer=self.key_serializer) for key in keys
|
|
114
120
|
]
|
|
115
|
-
async with
|
|
121
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
116
122
|
self.timeout, error=self.error
|
|
117
123
|
):
|
|
118
124
|
result = await cast( # skipif-ci-and-not-linux
|
|
@@ -127,7 +133,7 @@ class RedisHashMapKey[K, V]:
|
|
|
127
133
|
|
|
128
134
|
async def keys(self, redis: Redis, /) -> Sequence[K]:
|
|
129
135
|
"""Get the keys of a hashmap in `redis`."""
|
|
130
|
-
async with
|
|
136
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
131
137
|
self.timeout, error=self.error
|
|
132
138
|
):
|
|
133
139
|
result = await cast("Awaitable[Sequence[bytes]]", redis.hkeys(self.name))
|
|
@@ -137,7 +143,7 @@ class RedisHashMapKey[K, V]:
|
|
|
137
143
|
|
|
138
144
|
async def length(self, redis: Redis, /) -> int:
|
|
139
145
|
"""Get the length of a hashmap in `redis`."""
|
|
140
|
-
async with
|
|
146
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
141
147
|
self.timeout, error=self.error
|
|
142
148
|
):
|
|
143
149
|
return await cast("Awaitable[int]", redis.hlen(self.name))
|
|
@@ -156,19 +162,19 @@ class RedisHashMapKey[K, V]:
|
|
|
156
162
|
)
|
|
157
163
|
for key, value in mapping.items()
|
|
158
164
|
}
|
|
159
|
-
async with
|
|
165
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
160
166
|
self.timeout, error=self.error
|
|
161
167
|
):
|
|
162
168
|
result = await cast(
|
|
163
169
|
"Awaitable[int]", redis.hset(self.name, mapping=cast("Any", ser))
|
|
164
170
|
)
|
|
165
171
|
if self.ttl is not None:
|
|
166
|
-
await redis.pexpire(self.name,
|
|
172
|
+
await redis.pexpire(self.name, safe_round(in_milli_seconds(self.ttl)))
|
|
167
173
|
return result # skipif-ci-and-not-linux
|
|
168
174
|
|
|
169
175
|
async def values(self, redis: Redis, /) -> Sequence[V]:
|
|
170
176
|
"""Get the values of a hashmap in `redis`."""
|
|
171
|
-
async with
|
|
177
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
172
178
|
self.timeout, error=self.error
|
|
173
179
|
):
|
|
174
180
|
result = await cast("Awaitable[Sequence[bytes]]", redis.hvals(self.name))
|
|
@@ -188,9 +194,9 @@ def redis_hash_map_key[K, V](
|
|
|
188
194
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
189
195
|
value_serializer: Callable[[V], bytes] | None = None,
|
|
190
196
|
value_deserializer: Callable[[bytes], V] | None = None,
|
|
191
|
-
timeout:
|
|
197
|
+
timeout: Duration | None = None,
|
|
192
198
|
error: MaybeType[BaseException] = TimeoutError,
|
|
193
|
-
ttl:
|
|
199
|
+
ttl: Duration | None = None,
|
|
194
200
|
) -> RedisHashMapKey[K, V]: ...
|
|
195
201
|
@overload
|
|
196
202
|
def redis_hash_map_key[K, V1, V2](
|
|
@@ -203,9 +209,9 @@ def redis_hash_map_key[K, V1, V2](
|
|
|
203
209
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
204
210
|
value_serializer: Callable[[V1 | V2], bytes] | None = None,
|
|
205
211
|
value_deserializer: Callable[[bytes], V1 | V2] | None = None,
|
|
206
|
-
timeout:
|
|
212
|
+
timeout: Duration | None = None,
|
|
207
213
|
error: MaybeType[BaseException] = TimeoutError,
|
|
208
|
-
ttl:
|
|
214
|
+
ttl: Duration | None = None,
|
|
209
215
|
) -> RedisHashMapKey[K, V1 | V2]: ...
|
|
210
216
|
@overload
|
|
211
217
|
def redis_hash_map_key[K, V1, V2, V3](
|
|
@@ -218,9 +224,9 @@ def redis_hash_map_key[K, V1, V2, V3](
|
|
|
218
224
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
219
225
|
value_serializer: Callable[[V1 | V2 | V3], bytes] | None = None,
|
|
220
226
|
value_deserializer: Callable[[bytes], V1 | V2 | V3] | None = None,
|
|
221
|
-
timeout:
|
|
227
|
+
timeout: Duration | None = None,
|
|
222
228
|
error: MaybeType[BaseException] = TimeoutError,
|
|
223
|
-
ttl:
|
|
229
|
+
ttl: Duration | None = None,
|
|
224
230
|
) -> RedisHashMapKey[K, V1 | V2 | V3]: ...
|
|
225
231
|
@overload
|
|
226
232
|
def redis_hash_map_key[K1, K2, V](
|
|
@@ -233,9 +239,9 @@ def redis_hash_map_key[K1, K2, V](
|
|
|
233
239
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
234
240
|
value_serializer: Callable[[V], bytes] | None = None,
|
|
235
241
|
value_deserializer: Callable[[bytes], V] | None = None,
|
|
236
|
-
timeout:
|
|
242
|
+
timeout: Duration | None = None,
|
|
237
243
|
error: MaybeType[BaseException] = TimeoutError,
|
|
238
|
-
ttl:
|
|
244
|
+
ttl: Duration | None = None,
|
|
239
245
|
) -> RedisHashMapKey[K1 | K2, V]: ...
|
|
240
246
|
@overload
|
|
241
247
|
def redis_hash_map_key[K1, K2, V1, V2](
|
|
@@ -248,9 +254,9 @@ def redis_hash_map_key[K1, K2, V1, V2](
|
|
|
248
254
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
249
255
|
value_serializer: Callable[[V1 | V2], bytes] | None = None,
|
|
250
256
|
value_deserializer: Callable[[bytes], V1 | V2] | None = None,
|
|
251
|
-
timeout:
|
|
257
|
+
timeout: Duration | None = None,
|
|
252
258
|
error: MaybeType[BaseException] = TimeoutError,
|
|
253
|
-
ttl:
|
|
259
|
+
ttl: Duration | None = None,
|
|
254
260
|
) -> RedisHashMapKey[K1 | K2, V1 | V2]: ...
|
|
255
261
|
@overload
|
|
256
262
|
def redis_hash_map_key[K1, K2, V1, V2, V3](
|
|
@@ -263,9 +269,9 @@ def redis_hash_map_key[K1, K2, V1, V2, V3](
|
|
|
263
269
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
264
270
|
value_serializer: Callable[[V1 | V2 | V3], bytes] | None = None,
|
|
265
271
|
value_deserializer: Callable[[bytes], V1 | V2 | V3] | None = None,
|
|
266
|
-
timeout:
|
|
272
|
+
timeout: Duration | None = None,
|
|
267
273
|
error: MaybeType[BaseException] = TimeoutError,
|
|
268
|
-
ttl:
|
|
274
|
+
ttl: Duration | None = None,
|
|
269
275
|
) -> RedisHashMapKey[K1 | K2, V1 | V2 | V3]: ...
|
|
270
276
|
@overload
|
|
271
277
|
def redis_hash_map_key[K1, K2, K3, V](
|
|
@@ -278,9 +284,9 @@ def redis_hash_map_key[K1, K2, K3, V](
|
|
|
278
284
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
279
285
|
value_serializer: Callable[[V], bytes] | None = None,
|
|
280
286
|
value_deserializer: Callable[[bytes], V] | None = None,
|
|
281
|
-
timeout:
|
|
287
|
+
timeout: Duration | None = None,
|
|
282
288
|
error: MaybeType[BaseException] = TimeoutError,
|
|
283
|
-
ttl:
|
|
289
|
+
ttl: Duration | None = None,
|
|
284
290
|
) -> RedisHashMapKey[K1 | K2 | K3, V]: ...
|
|
285
291
|
@overload
|
|
286
292
|
def redis_hash_map_key[K1, K2, K3, V1, V2](
|
|
@@ -293,9 +299,9 @@ def redis_hash_map_key[K1, K2, K3, V1, V2](
|
|
|
293
299
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
294
300
|
value_serializer: Callable[[V1 | V2], bytes] | None = None,
|
|
295
301
|
value_deserializer: Callable[[bytes], V1 | V2] | None = None,
|
|
296
|
-
timeout:
|
|
302
|
+
timeout: Duration | None = None,
|
|
297
303
|
error: MaybeType[BaseException] = TimeoutError,
|
|
298
|
-
ttl:
|
|
304
|
+
ttl: Duration | None = None,
|
|
299
305
|
) -> RedisHashMapKey[K1 | K2 | K3, V1 | V2]: ...
|
|
300
306
|
@overload
|
|
301
307
|
def redis_hash_map_key[K1, K2, K3, V1, V2, V3](
|
|
@@ -308,9 +314,9 @@ def redis_hash_map_key[K1, K2, K3, V1, V2, V3](
|
|
|
308
314
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
309
315
|
value_serializer: Callable[[V1 | V2 | V3], bytes] | None = None,
|
|
310
316
|
value_deserializer: Callable[[bytes], V1 | V2 | V3] | None = None,
|
|
311
|
-
timeout:
|
|
317
|
+
timeout: Duration | None = None,
|
|
312
318
|
error: MaybeType[BaseException] = TimeoutError,
|
|
313
|
-
ttl:
|
|
319
|
+
ttl: Duration | None = None,
|
|
314
320
|
) -> RedisHashMapKey[K1 | K2 | K3, V1 | V2 | V3]: ...
|
|
315
321
|
@overload
|
|
316
322
|
def redis_hash_map_key[K, K1, K2, K3, V, V1, V2, V3](
|
|
@@ -323,9 +329,9 @@ def redis_hash_map_key[K, K1, K2, K3, V, V1, V2, V3](
|
|
|
323
329
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
324
330
|
value_serializer: Callable[[V1 | V2 | V3], bytes] | None = None,
|
|
325
331
|
value_deserializer: Callable[[bytes], V1 | V2 | V3] | None = None,
|
|
326
|
-
timeout:
|
|
332
|
+
timeout: Duration | None = None,
|
|
327
333
|
error: MaybeType[BaseException] = TimeoutError,
|
|
328
|
-
ttl:
|
|
334
|
+
ttl: Duration | None = None,
|
|
329
335
|
) -> RedisHashMapKey[K, V]: ...
|
|
330
336
|
def redis_hash_map_key[K, V](
|
|
331
337
|
name: str,
|
|
@@ -337,8 +343,8 @@ def redis_hash_map_key[K, V](
|
|
|
337
343
|
key_deserializer: Callable[[bytes], Any] | None = None,
|
|
338
344
|
value_serializer: Callable[[Any], bytes] | None = None,
|
|
339
345
|
value_deserializer: Callable[[bytes], Any] | None = None,
|
|
340
|
-
timeout:
|
|
341
|
-
ttl:
|
|
346
|
+
timeout: Duration | None = None,
|
|
347
|
+
ttl: Duration | None = None,
|
|
342
348
|
error: MaybeType[BaseException] = TimeoutError,
|
|
343
349
|
) -> RedisHashMapKey[K, V]:
|
|
344
350
|
"""Create a redis key."""
|
|
@@ -367,23 +373,19 @@ class RedisKey[T]:
|
|
|
367
373
|
type: TypeLike[T]
|
|
368
374
|
serializer: Callable[[T], bytes] | None = None
|
|
369
375
|
deserializer: Callable[[bytes], T] | None = None
|
|
370
|
-
timeout:
|
|
376
|
+
timeout: Duration | None = None
|
|
371
377
|
error: MaybeType[BaseException] = TimeoutError
|
|
372
|
-
ttl:
|
|
378
|
+
ttl: Duration | None = None
|
|
373
379
|
|
|
374
380
|
async def delete(self, redis: Redis, /) -> int:
|
|
375
381
|
"""Delete the key from `redis`."""
|
|
376
|
-
async with
|
|
377
|
-
self.timeout, error=self.error
|
|
378
|
-
):
|
|
382
|
+
async with timeout(self.timeout, error=self.error): # skipif-ci-and-not-linux
|
|
379
383
|
response = await redis.delete(self.name)
|
|
380
384
|
return ensure_int(response)
|
|
381
385
|
|
|
382
386
|
async def exists(self, redis: Redis, /) -> bool:
|
|
383
387
|
"""Check if the key exists in `redis`."""
|
|
384
|
-
async with
|
|
385
|
-
self.timeout, error=self.error
|
|
386
|
-
):
|
|
388
|
+
async with timeout(self.timeout, error=self.error): # skipif-ci-and-not-linux
|
|
387
389
|
result = cast("Literal[0, 1]", await redis.exists(self.name))
|
|
388
390
|
match result: # skipif-ci-and-not-linux
|
|
389
391
|
case 0 | 1 as value:
|
|
@@ -393,9 +395,7 @@ class RedisKey[T]:
|
|
|
393
395
|
|
|
394
396
|
async def get(self, redis: Redis, /) -> T:
|
|
395
397
|
"""Get a value from `redis`."""
|
|
396
|
-
async with
|
|
397
|
-
self.timeout, error=self.error
|
|
398
|
-
):
|
|
398
|
+
async with timeout(self.timeout, error=self.error): # skipif-ci-and-not-linux
|
|
399
399
|
result = cast("bytes | None", await redis.get(self.name))
|
|
400
400
|
if result is None: # skipif-ci-and-not-linux
|
|
401
401
|
raise KeyError(self.name)
|
|
@@ -407,9 +407,9 @@ class RedisKey[T]:
|
|
|
407
407
|
"""Set a value in `redis`."""
|
|
408
408
|
ser = _serialize(value, serializer=self.serializer) # skipif-ci-and-not-linux
|
|
409
409
|
ttl = ( # skipif-ci-and-not-linux
|
|
410
|
-
None if self.ttl is None else
|
|
410
|
+
None if self.ttl is None else safe_round(in_milli_seconds(self.ttl))
|
|
411
411
|
)
|
|
412
|
-
async with
|
|
412
|
+
async with timeout( # skipif-ci-and-not-linux
|
|
413
413
|
self.timeout, error=self.error
|
|
414
414
|
):
|
|
415
415
|
response = await redis.set( # skipif-ci-and-not-linux
|
|
@@ -426,9 +426,9 @@ def redis_key[T](
|
|
|
426
426
|
*,
|
|
427
427
|
serializer: Callable[[T], bytes] | None = None,
|
|
428
428
|
deserializer: Callable[[bytes], T] | None = None,
|
|
429
|
-
timeout:
|
|
429
|
+
timeout: Duration | None = None,
|
|
430
430
|
error: MaybeType[BaseException] = TimeoutError,
|
|
431
|
-
ttl:
|
|
431
|
+
ttl: Duration | None = None,
|
|
432
432
|
) -> RedisKey[T]: ...
|
|
433
433
|
@overload
|
|
434
434
|
def redis_key[T1, T2](
|
|
@@ -438,9 +438,9 @@ def redis_key[T1, T2](
|
|
|
438
438
|
*,
|
|
439
439
|
serializer: Callable[[T1 | T2], bytes] | None = None,
|
|
440
440
|
deserializer: Callable[[bytes], T1 | T2] | None = None,
|
|
441
|
-
timeout:
|
|
441
|
+
timeout: Duration | None = None,
|
|
442
442
|
error: MaybeType[BaseException] = TimeoutError,
|
|
443
|
-
ttl:
|
|
443
|
+
ttl: Duration | None = None,
|
|
444
444
|
) -> RedisKey[T1 | T2]: ...
|
|
445
445
|
@overload
|
|
446
446
|
def redis_key[T1, T2, T3](
|
|
@@ -450,9 +450,9 @@ def redis_key[T1, T2, T3](
|
|
|
450
450
|
*,
|
|
451
451
|
serializer: Callable[[T1 | T2 | T3], bytes] | None = None,
|
|
452
452
|
deserializer: Callable[[bytes], T1 | T2 | T3] | None = None,
|
|
453
|
-
timeout:
|
|
453
|
+
timeout: Duration | None = None,
|
|
454
454
|
error: MaybeType[BaseException] = TimeoutError,
|
|
455
|
-
ttl:
|
|
455
|
+
ttl: Duration | None = None,
|
|
456
456
|
) -> RedisKey[T1 | T2 | T3]: ...
|
|
457
457
|
@overload
|
|
458
458
|
def redis_key[T1, T2, T3, T4](
|
|
@@ -462,9 +462,9 @@ def redis_key[T1, T2, T3, T4](
|
|
|
462
462
|
*,
|
|
463
463
|
serializer: Callable[[T1 | T2 | T3 | T4], bytes] | None = None,
|
|
464
464
|
deserializer: Callable[[bytes], T1 | T2 | T3 | T4] | None = None,
|
|
465
|
-
timeout:
|
|
465
|
+
timeout: Duration | None = None,
|
|
466
466
|
error: MaybeType[BaseException] = TimeoutError,
|
|
467
|
-
ttl:
|
|
467
|
+
ttl: Duration | None = None,
|
|
468
468
|
) -> RedisKey[T1 | T2 | T3 | T4]: ...
|
|
469
469
|
@overload
|
|
470
470
|
def redis_key[T1, T2, T3, T4, T5](
|
|
@@ -474,9 +474,9 @@ def redis_key[T1, T2, T3, T4, T5](
|
|
|
474
474
|
*,
|
|
475
475
|
serializer: Callable[[T1 | T2 | T3 | T4 | T5], bytes] | None = None,
|
|
476
476
|
deserializer: Callable[[bytes], T1 | T2 | T3 | T4 | T5] | None = None,
|
|
477
|
-
timeout:
|
|
477
|
+
timeout: Duration | None = None,
|
|
478
478
|
error: MaybeType[BaseException] = TimeoutError,
|
|
479
|
-
ttl:
|
|
479
|
+
ttl: Duration | None = None,
|
|
480
480
|
) -> RedisKey[T1 | T2 | T3 | T4 | T5]: ...
|
|
481
481
|
@overload
|
|
482
482
|
def redis_key[T, T1, T2, T3, T4, T5](
|
|
@@ -486,9 +486,9 @@ def redis_key[T, T1, T2, T3, T4, T5](
|
|
|
486
486
|
*,
|
|
487
487
|
serializer: Callable[[T1 | T2 | T3 | T4 | T5], bytes] | None = None,
|
|
488
488
|
deserializer: Callable[[bytes], T1 | T2 | T3 | T4 | T5] | None = None,
|
|
489
|
-
timeout:
|
|
489
|
+
timeout: Duration | None = None,
|
|
490
490
|
error: MaybeType[BaseException] = TimeoutError,
|
|
491
|
-
ttl:
|
|
491
|
+
ttl: Duration | None = None,
|
|
492
492
|
) -> RedisKey[T]: ...
|
|
493
493
|
def redis_key[T](
|
|
494
494
|
name: str,
|
|
@@ -497,9 +497,9 @@ def redis_key[T](
|
|
|
497
497
|
*,
|
|
498
498
|
serializer: Callable[[Any], bytes] | None = None,
|
|
499
499
|
deserializer: Callable[[bytes], Any] | None = None,
|
|
500
|
-
timeout:
|
|
500
|
+
timeout: Duration | None = None,
|
|
501
501
|
error: MaybeType[BaseException] = TimeoutError,
|
|
502
|
-
ttl:
|
|
502
|
+
ttl: Duration | None = None,
|
|
503
503
|
) -> RedisKey[T]:
|
|
504
504
|
"""Create a redis key."""
|
|
505
505
|
return RedisKey( # skipif-ci-and-not-linux
|
|
@@ -524,7 +524,7 @@ async def publish[T](
|
|
|
524
524
|
/,
|
|
525
525
|
*,
|
|
526
526
|
serializer: Callable[[T], EncodableT],
|
|
527
|
-
timeout:
|
|
527
|
+
timeout: Duration | None = _PUBLISH_TIMEOUT,
|
|
528
528
|
) -> int: ...
|
|
529
529
|
@overload
|
|
530
530
|
async def publish(
|
|
@@ -534,7 +534,7 @@ async def publish(
|
|
|
534
534
|
/,
|
|
535
535
|
*,
|
|
536
536
|
serializer: None = None,
|
|
537
|
-
timeout:
|
|
537
|
+
timeout: Duration | None = _PUBLISH_TIMEOUT,
|
|
538
538
|
) -> int: ...
|
|
539
539
|
@overload
|
|
540
540
|
async def publish[T](
|
|
@@ -544,7 +544,7 @@ async def publish[T](
|
|
|
544
544
|
/,
|
|
545
545
|
*,
|
|
546
546
|
serializer: Callable[[T], EncodableT] | None = None,
|
|
547
|
-
timeout:
|
|
547
|
+
timeout: Duration | None = _PUBLISH_TIMEOUT,
|
|
548
548
|
) -> int: ...
|
|
549
549
|
async def publish[T](
|
|
550
550
|
redis: Redis,
|
|
@@ -553,7 +553,7 @@ async def publish[T](
|
|
|
553
553
|
/,
|
|
554
554
|
*,
|
|
555
555
|
serializer: Callable[[T], EncodableT] | None = None,
|
|
556
|
-
timeout:
|
|
556
|
+
timeout: Duration | None = _PUBLISH_TIMEOUT,
|
|
557
557
|
) -> int:
|
|
558
558
|
"""Publish an object to a channel."""
|
|
559
559
|
match data, serializer: # skipif-ci-and-not-linux
|
|
@@ -565,8 +565,8 @@ async def publish[T](
|
|
|
565
565
|
data_use = serializer(data)
|
|
566
566
|
case never:
|
|
567
567
|
assert_never(never)
|
|
568
|
-
async with
|
|
569
|
-
response = await redis.publish(channel, data_use)
|
|
568
|
+
async with utilities.asyncio.timeout(timeout): # skipif-ci-and-not-linux
|
|
569
|
+
response = await redis.publish(channel, data_use)
|
|
570
570
|
return ensure_int(response) # skipif-ci-and-not-linux
|
|
571
571
|
|
|
572
572
|
|
|
@@ -589,7 +589,7 @@ async def publish_many[T](
|
|
|
589
589
|
/,
|
|
590
590
|
*,
|
|
591
591
|
serializer: Callable[[T], EncodableT] | None = None,
|
|
592
|
-
timeout:
|
|
592
|
+
timeout: Duration | None = _PUBLISH_TIMEOUT,
|
|
593
593
|
) -> Sequence[bool]:
|
|
594
594
|
"""Publish an object/multiple objects to a channel."""
|
|
595
595
|
async with TaskGroup() as tg:
|
|
@@ -615,7 +615,7 @@ async def _try_publish[T](
|
|
|
615
615
|
/,
|
|
616
616
|
*,
|
|
617
617
|
serializer: Callable[[T], EncodableT] | None = None,
|
|
618
|
-
timeout:
|
|
618
|
+
timeout: Duration | None = _PUBLISH_TIMEOUT,
|
|
619
619
|
) -> bool:
|
|
620
620
|
try:
|
|
621
621
|
_ = await publish(redis, channel, data, serializer=serializer, timeout=timeout)
|
|
@@ -627,8 +627,8 @@ async def _try_publish[T](
|
|
|
627
627
|
##
|
|
628
628
|
|
|
629
629
|
|
|
630
|
-
_SUBSCRIBE_TIMEOUT:
|
|
631
|
-
_SUBSCRIBE_SLEEP:
|
|
630
|
+
_SUBSCRIBE_TIMEOUT: Duration = SECOND
|
|
631
|
+
_SUBSCRIBE_SLEEP: Duration = MILLISECOND
|
|
632
632
|
|
|
633
633
|
|
|
634
634
|
@overload
|
|
@@ -639,12 +639,12 @@ def subscribe(
|
|
|
639
639
|
queue: Queue[_RedisMessage],
|
|
640
640
|
/,
|
|
641
641
|
*,
|
|
642
|
-
timeout:
|
|
642
|
+
timeout: Duration | None = _SUBSCRIBE_TIMEOUT,
|
|
643
643
|
output: Literal["raw"],
|
|
644
644
|
error_transform: Callable[[_RedisMessage, Exception], None] | None = None,
|
|
645
645
|
filter_: Callable[[bytes], bool] | None = None,
|
|
646
646
|
error_filter: Callable[[bytes, Exception], None] | None = None,
|
|
647
|
-
sleep:
|
|
647
|
+
sleep: Duration = _SUBSCRIBE_SLEEP,
|
|
648
648
|
) -> AsyncIterator[Task[None]]: ...
|
|
649
649
|
@overload
|
|
650
650
|
@enhanced_async_context_manager
|
|
@@ -654,12 +654,12 @@ def subscribe(
|
|
|
654
654
|
queue: Queue[bytes],
|
|
655
655
|
/,
|
|
656
656
|
*,
|
|
657
|
-
timeout:
|
|
657
|
+
timeout: Duration | None = _SUBSCRIBE_TIMEOUT,
|
|
658
658
|
output: Literal["bytes"],
|
|
659
659
|
error_transform: Callable[[_RedisMessage, Exception], None] | None = None,
|
|
660
660
|
filter_: Callable[[bytes], bool] | None = None,
|
|
661
661
|
error_filter: Callable[[bytes, Exception], None] | None = None,
|
|
662
|
-
sleep:
|
|
662
|
+
sleep: Duration = _SUBSCRIBE_SLEEP,
|
|
663
663
|
) -> AsyncIterator[Task[None]]: ...
|
|
664
664
|
@overload
|
|
665
665
|
@enhanced_async_context_manager
|
|
@@ -669,12 +669,12 @@ def subscribe(
|
|
|
669
669
|
queue: Queue[str],
|
|
670
670
|
/,
|
|
671
671
|
*,
|
|
672
|
-
timeout:
|
|
672
|
+
timeout: Duration | None = _SUBSCRIBE_TIMEOUT,
|
|
673
673
|
output: Literal["text"] = "text",
|
|
674
674
|
error_transform: Callable[[_RedisMessage, Exception], None] | None = None,
|
|
675
675
|
filter_: Callable[[str], bool] | None = None,
|
|
676
676
|
error_filter: Callable[[str, Exception], None] | None = None,
|
|
677
|
-
sleep:
|
|
677
|
+
sleep: Duration = _SUBSCRIBE_SLEEP,
|
|
678
678
|
) -> AsyncIterator[Task[None]]: ...
|
|
679
679
|
@overload
|
|
680
680
|
@enhanced_async_context_manager
|
|
@@ -684,12 +684,12 @@ def subscribe[T](
|
|
|
684
684
|
queue: Queue[T],
|
|
685
685
|
/,
|
|
686
686
|
*,
|
|
687
|
-
timeout:
|
|
687
|
+
timeout: Duration | None = _SUBSCRIBE_TIMEOUT,
|
|
688
688
|
output: Callable[[bytes], T],
|
|
689
689
|
error_transform: Callable[[_RedisMessage, Exception], None] | None = None,
|
|
690
690
|
filter_: Callable[[T], bool] | None = None,
|
|
691
691
|
error_filter: Callable[[T, Exception], None] | None = None,
|
|
692
|
-
sleep:
|
|
692
|
+
sleep: Duration = _SUBSCRIBE_SLEEP,
|
|
693
693
|
) -> AsyncIterator[Task[None]]: ...
|
|
694
694
|
@enhanced_async_context_manager
|
|
695
695
|
async def subscribe[T](
|
|
@@ -698,12 +698,12 @@ async def subscribe[T](
|
|
|
698
698
|
queue: Queue[_RedisMessage] | Queue[bytes] | Queue[T],
|
|
699
699
|
/,
|
|
700
700
|
*,
|
|
701
|
-
timeout:
|
|
701
|
+
timeout: Duration | None = _SUBSCRIBE_TIMEOUT,
|
|
702
702
|
output: Literal["raw", "bytes", "text"] | Callable[[bytes], T] = "text",
|
|
703
703
|
error_transform: Callable[[_RedisMessage, Exception], None] | None = None,
|
|
704
704
|
filter_: Callable[[T], bool] | None = None,
|
|
705
705
|
error_filter: Callable[[T, Exception], None] | None = None,
|
|
706
|
-
sleep:
|
|
706
|
+
sleep: Duration = _SUBSCRIBE_SLEEP,
|
|
707
707
|
) -> AsyncIterator[Task[None]]:
|
|
708
708
|
"""Subscribe to the data of a given channel(s)."""
|
|
709
709
|
channels = list(always_iterable(channels)) # skipif-ci-and-not-linux
|
|
@@ -758,14 +758,14 @@ async def _subscribe_core[T](
|
|
|
758
758
|
queue: Queue[Any],
|
|
759
759
|
/,
|
|
760
760
|
*,
|
|
761
|
-
timeout:
|
|
761
|
+
timeout: Duration | None = _SUBSCRIBE_TIMEOUT,
|
|
762
762
|
error_transform: Callable[[_RedisMessage, Exception], None] | None = None,
|
|
763
763
|
filter_: Callable[[T], bool] | None = None,
|
|
764
764
|
error_filter: Callable[[T, Exception], None] | None = None,
|
|
765
|
-
sleep:
|
|
765
|
+
sleep: Duration = _SUBSCRIBE_SLEEP,
|
|
766
766
|
) -> None:
|
|
767
767
|
timeout_use = ( # skipif-ci-and-not-linux
|
|
768
|
-
None if timeout is None else (
|
|
768
|
+
None if timeout is None else in_seconds(timeout)
|
|
769
769
|
)
|
|
770
770
|
is_subscribe_message = partial( # skipif-ci-and-not-linux
|
|
771
771
|
_is_message, channels={c.encode() for c in channels}
|
|
@@ -783,7 +783,7 @@ async def _subscribe_core[T](
|
|
|
783
783
|
error_filter=error_filter,
|
|
784
784
|
)
|
|
785
785
|
else:
|
|
786
|
-
await
|
|
786
|
+
await utilities.asyncio.sleep(sleep)
|
|
787
787
|
|
|
788
788
|
|
|
789
789
|
def _is_message(
|
|
@@ -893,7 +893,9 @@ def _serialize[T](
|
|
|
893
893
|
obj: T, /, *, serializer: Callable[[T], bytes] | None = None
|
|
894
894
|
) -> bytes:
|
|
895
895
|
if serializer is None: # skipif-ci-and-not-linux
|
|
896
|
-
|
|
896
|
+
import utilities.orjson
|
|
897
|
+
|
|
898
|
+
serializer_use = utilities.orjson.serialize
|
|
897
899
|
else: # skipif-ci-and-not-linux
|
|
898
900
|
serializer_use = serializer
|
|
899
901
|
return serializer_use(obj) # skipif-ci-and-not-linux
|
|
@@ -903,7 +905,9 @@ def _deserialize[T](
|
|
|
903
905
|
data: bytes, /, *, deserializer: Callable[[bytes], T] | None = None
|
|
904
906
|
) -> T:
|
|
905
907
|
if deserializer is None: # skipif-ci-and-not-linux
|
|
906
|
-
|
|
908
|
+
import utilities.orjson
|
|
909
|
+
|
|
910
|
+
deserializer_use = utilities.orjson.deserialize
|
|
907
911
|
else: # skipif-ci-and-not-linux
|
|
908
912
|
deserializer_use = deserializer
|
|
909
913
|
return deserializer_use(data) # skipif-ci-and-not-linux
|