dycw-utilities 0.113.4__py3-none-any.whl → 0.114.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-utilities
3
- Version: 0.113.4
3
+ Version: 0.114.0
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,7 +1,7 @@
1
- utilities/__init__.py,sha256=f-gfMxj416K6fDUn3GBZ_ctE4iCAIW3yKwMD5u5-rOw,60
1
+ utilities/__init__.py,sha256=qECk5Uaq4SsKcMpFjNUaChs2N78j3evztQRVgKylt4g,60
2
2
  utilities/altair.py,sha256=Gpja-flOo-Db0PIPJLJsgzAlXWoKUjPU1qY-DQ829ek,9156
3
3
  utilities/astor.py,sha256=xuDUkjq0-b6fhtwjhbnebzbqQZAjMSHR1IIS5uOodVg,777
4
- utilities/asyncio.py,sha256=6Pv0xK2jP55iF6EYjY1g6_rFGOeJyihxRNxXnOsWv14,15691
4
+ utilities/asyncio.py,sha256=a4riQIfTYCe4oxXUbbuP7YB3tIlzmYxYDC9XygdsrQc,17637
5
5
  utilities/atomicwrites.py,sha256=geFjn9Pwn-tTrtoGjDDxWli9NqbYfy3gGL6ZBctiqSo,5393
6
6
  utilities/atools.py,sha256=IYMuFSFGSKyuQmqD6v5IUtDlz8PPw0Sr87Cub_gRU3M,1168
7
7
  utilities/cachetools.py,sha256=C1zqOg7BYz0IfQFK8e3qaDDgEZxDpo47F15RTfJM37Q,2910
@@ -64,9 +64,9 @@ utilities/rich.py,sha256=t50MwwVBsoOLxzmeVFSVpjno4OW6Ufum32skXbV8-Bs,1911
64
64
  utilities/scipy.py,sha256=X6ROnHwiUhAmPhM0jkfEh0-Fd9iRvwiqtCQMOLmOQF8,945
65
65
  utilities/sentinel.py,sha256=3jIwgpMekWgDAxPDA_hXMP2St43cPhciKN3LWiZ7kv0,1248
66
66
  utilities/shelve.py,sha256=HZsMwK4tcIfg3sh0gApx4-yjQnrY4o3V3ZRimvRhoW0,738
67
- utilities/slack_sdk.py,sha256=SeDNMh24IPiEBWoGMdgvrflUaFa9TGlTS03H9-NKaQw,4132
67
+ utilities/slack_sdk.py,sha256=Gbla983KulSSXnNyzaXgYQLKoq84KvLH8SdhxU-jQ0Q,4126
68
68
  utilities/socket.py,sha256=K77vfREvzoVTrpYKo6MZakol0EYu2q1sWJnnZqL0So0,118
69
- utilities/sqlalchemy.py,sha256=GWzp54TP3F2mGhxPTn0c56KxxDeN9VKLMagcRSELhf4,35453
69
+ utilities/sqlalchemy.py,sha256=bs7rD1f8yB0uaFMYgmjo8wEoGow0x6aiELSYTPY_Img,35447
70
70
  utilities/sqlalchemy_polars.py,sha256=wjJpoUo-yO9E2ujpG_06vV5r2OdvBiQ4yvV6wKCa2Tk,15605
71
71
  utilities/statsmodels.py,sha256=koyiBHvpMcSiBfh99wFUfSggLNx7cuAw3rwyfAhoKpQ,3410
72
72
  utilities/streamlit.py,sha256=U9PJBaKP1IdSykKhPZhIzSPTZsmLsnwbEPZWzNhJPKk,2955
@@ -87,7 +87,7 @@ utilities/warnings.py,sha256=un1LvHv70PU-LLv8RxPVmugTzDJkkGXRMZTE2-fTQHw,1771
87
87
  utilities/whenever.py,sha256=iLRP_-8CZtBpHKbGZGu-kjSMg1ZubJ-VSmgSy7Eudxw,17787
88
88
  utilities/zipfile.py,sha256=24lQc9ATcJxHXBPc_tBDiJk48pWyRrlxO2fIsFxU0A8,699
89
89
  utilities/zoneinfo.py,sha256=-Xm57PMMwDTYpxJdkiJG13wnbwK--I7XItBh5WVhD-o,1874
90
- dycw_utilities-0.113.4.dist-info/METADATA,sha256=ErT951cvoqudHh2P5_G-_Jhl0SziHUJZ8O8lJV4cGi8,12943
91
- dycw_utilities-0.113.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
92
- dycw_utilities-0.113.4.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
93
- dycw_utilities-0.113.4.dist-info/RECORD,,
90
+ dycw_utilities-0.114.0.dist-info/METADATA,sha256=plrwlVumL52G3Ix8B_ywj9b6hpqMXiheJfgTbbD5P_c,12943
91
+ dycw_utilities-0.114.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
92
+ dycw_utilities-0.114.0.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
93
+ dycw_utilities-0.114.0.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.113.4"
3
+ __version__ = "0.114.0"
utilities/asyncio.py CHANGED
@@ -4,7 +4,6 @@ from abc import ABC, abstractmethod
4
4
  from asyncio import (
5
5
  CancelledError,
6
6
  Event,
7
- Lock,
8
7
  PriorityQueue,
9
8
  Queue,
10
9
  QueueEmpty,
@@ -17,7 +16,7 @@ from asyncio import (
17
16
  sleep,
18
17
  timeout,
19
18
  )
20
- from collections.abc import Callable
19
+ from collections.abc import Callable, Iterable, Mapping
21
20
  from contextlib import (
22
21
  AsyncExitStack,
23
22
  _AsyncGeneratorContextManager,
@@ -39,11 +38,17 @@ from typing import (
39
38
  override,
40
39
  )
41
40
 
42
- from utilities.datetime import MILLISECOND, datetime_duration_to_float
41
+ from utilities.datetime import MILLISECOND, MINUTE, SECOND, datetime_duration_to_float
43
42
  from utilities.errors import ImpossibleCaseError
44
43
  from utilities.functions import ensure_int, ensure_not_none
45
44
  from utilities.sentinel import Sentinel, sentinel
46
- from utilities.types import MaybeCallableEvent, THashable, TSupportsRichComparison
45
+ from utilities.types import (
46
+ Coroutine1,
47
+ MaybeCallableEvent,
48
+ MaybeType,
49
+ THashable,
50
+ TSupportsRichComparison,
51
+ )
47
52
 
48
53
  if TYPE_CHECKING:
49
54
  from asyncio import _CoroutineLike
@@ -234,7 +239,6 @@ class QueueProcessor(AsyncService, Generic[_T]):
234
239
  sleep: Duration = MILLISECOND
235
240
  _await_upon_aenter: bool = field(default=False, init=False, repr=False)
236
241
  _queue: Queue[_T] = field(init=False, repr=False)
237
- _lock: Lock = field(default_factory=Lock, init=False, repr=False)
238
242
 
239
243
  def __post_init__(self) -> None:
240
244
  self._queue = self.queue_type(
@@ -259,9 +263,9 @@ class QueueProcessor(AsyncService, Generic[_T]):
259
263
  await self._run()
260
264
  await sleep_dur(duration=self.sleep)
261
265
 
262
- async def _get_items_nowait(self, *, max_size: int | None = None) -> Sequence[_T]:
266
+ def _get_items_nowait(self, *, max_size: int | None = None) -> Sequence[_T]:
263
267
  """Get items from the queue; no waiting."""
264
- return await get_items_nowait(self._queue, max_size=max_size, lock=self._lock)
268
+ return get_items_nowait(self._queue, max_size=max_size)
265
269
 
266
270
  @abstractmethod
267
271
  async def _process_item(self, item: _T, /) -> None:
@@ -276,7 +280,7 @@ class QueueProcessor(AsyncService, Generic[_T]):
276
280
  async def _run(self) -> None:
277
281
  """Run the processer."""
278
282
  try:
279
- (item,) = await self._get_items_nowait(max_size=1)
283
+ (item,) = self._get_items_nowait(max_size=1)
280
284
  except ValueError:
281
285
  raise QueueEmpty from None
282
286
  try:
@@ -320,6 +324,70 @@ class ExceptionProcessor(QueueProcessor[Exception | type[Exception]]):
320
324
  ##
321
325
 
322
326
 
327
+ @dataclass(kw_only=True)
328
+ class InfiniteLooper(ABC, Generic[THashable]):
329
+ """An infinite loop which can throw exceptions by setting events."""
330
+
331
+ events: Mapping[THashable, Event] = field(
332
+ default_factory=dict, init=False, repr=False
333
+ )
334
+ sleep_core: Duration = SECOND
335
+ sleep_restart: Duration = MINUTE
336
+
337
+ def __post_init__(self) -> None:
338
+ self._reset_events()
339
+
340
+ async def __call__(self) -> Coroutine1[None]:
341
+ while True:
342
+ try:
343
+ self._reset_events()
344
+ try:
345
+ await self.initialize()
346
+ except Exception as error: # noqa: BLE001
347
+ self.error_upon_initialize(error)
348
+ await sleep_dur(duration=self.sleep_restart)
349
+ else:
350
+ while True:
351
+ try:
352
+ event = next(
353
+ key
354
+ for (key, value) in self.events.items()
355
+ if value.is_set()
356
+ )
357
+ except StopIteration:
358
+ await self.core()
359
+ await sleep_dur(duration=self.sleep_core)
360
+ else:
361
+ raise self.events_and_exceptions[event]
362
+ except Exception as error: # noqa: BLE001
363
+ self.error_upon_core(error)
364
+ await sleep_dur(duration=self.sleep_restart)
365
+
366
+ @property
367
+ @abstractmethod
368
+ def events_and_exceptions(self) -> Mapping[THashable, MaybeType[BaseException]]:
369
+ """A mapping of events to exceptions."""
370
+
371
+ async def initialize(self) -> None:
372
+ """Initialize the loop."""
373
+
374
+ async def core(self) -> None:
375
+ """Run the core."""
376
+
377
+ def error_upon_initialize(self, error: Exception, /) -> None:
378
+ _ = error
379
+
380
+ def error_upon_core(self, error: Exception, /) -> None:
381
+ _ = error
382
+
383
+ def _reset_events(self) -> None:
384
+ """Reset the events."""
385
+ self.events = {event: Event() for event in self.events_and_exceptions}
386
+
387
+
388
+ ##
389
+
390
+
323
391
  class UniquePriorityQueue(PriorityQueue[tuple[TSupportsRichComparison, THashable]]):
324
392
  """Priority queue with unique tasks."""
325
393
 
@@ -395,9 +463,7 @@ def get_event(
395
463
  ##
396
464
 
397
465
 
398
- async def get_items(
399
- queue: Queue[_T], /, *, max_size: int | None = None, lock: Lock | None = None
400
- ) -> list[_T]:
466
+ async def get_items(queue: Queue[_T], /, *, max_size: int | None = None) -> list[_T]:
401
467
  """Get items from a queue; if empty then wait."""
402
468
  try:
403
469
  items = [await queue.get()]
@@ -406,28 +472,12 @@ async def get_items(
406
472
  return []
407
473
  raise
408
474
  max_size_use = None if max_size is None else (max_size - 1)
409
- if lock is None:
410
- items.extend(await get_items_nowait(queue, max_size=max_size_use))
411
- else:
412
- async with lock:
413
- items.extend(await get_items_nowait(queue, max_size=max_size_use))
475
+ items.extend(get_items_nowait(queue, max_size=max_size_use))
414
476
  return items
415
477
 
416
478
 
417
- async def get_items_nowait(
418
- queue: Queue[_T], /, *, max_size: int | None = None, lock: Lock | None = None
419
- ) -> list[_T]:
479
+ def get_items_nowait(queue: Queue[_T], /, *, max_size: int | None = None) -> list[_T]:
420
480
  """Get items from a queue; no waiting."""
421
- if lock is None:
422
- return _get_items_nowait_core(queue, max_size=max_size)
423
- async with lock:
424
- return _get_items_nowait_core(queue, max_size=max_size)
425
-
426
-
427
- def _get_items_nowait_core(
428
- queue: Queue[_T], /, *, max_size: int | None = None
429
- ) -> list[_T]:
430
- """Get all the items from a queue; no waiting."""
431
481
  items: list[_T] = []
432
482
  if max_size is None:
433
483
  while True:
@@ -447,6 +497,21 @@ def _get_items_nowait_core(
447
497
  ##
448
498
 
449
499
 
500
+ async def put_items(items: Iterable[_T], queue: Queue[_T], /) -> None:
501
+ """Put items into a queue; if full then wait."""
502
+ for item in items:
503
+ await queue.put(item)
504
+
505
+
506
+ def put_items_nowait(items: Iterable[_T], queue: Queue[_T], /) -> None:
507
+ """Put items into a queue; no waiting."""
508
+ for item in items:
509
+ queue.put_nowait(item)
510
+
511
+
512
+ ##
513
+
514
+
450
515
  async def sleep_dur(*, duration: Duration | None = None) -> None:
451
516
  """Sleep which accepts durations."""
452
517
  if duration is None:
@@ -532,6 +597,8 @@ __all__ = [
532
597
  "get_event",
533
598
  "get_items",
534
599
  "get_items_nowait",
600
+ "put_items",
601
+ "put_items_nowait",
535
602
  "sleep_dur",
536
603
  "stream_command",
537
604
  "timeout_dur",
utilities/slack_sdk.py CHANGED
@@ -78,7 +78,7 @@ class SlackHandler(Handler, QueueProcessor[str]):
78
78
  @override
79
79
  async def _process_item(self, item: str, /) -> None:
80
80
  """Process the first item."""
81
- items = list(chain([item], await self._get_items_nowait()))
81
+ items = list(chain([item], self._get_items_nowait()))
82
82
  text = "\n".join(items)
83
83
  try:
84
84
  async with timeout_dur(duration=self.timeout):
utilities/sqlalchemy.py CHANGED
@@ -627,7 +627,7 @@ class Upserter(QueueProcessor[_InsertItem]):
627
627
  @override
628
628
  async def _process_item(self, item: _InsertItem, /) -> None:
629
629
  """Process the first item."""
630
- items = list(chain([item], await self._get_items_nowait()))
630
+ items = list(chain([item], self._get_items_nowait()))
631
631
  await self._pre_upsert(items)
632
632
  await upsert_items(
633
633
  self.engine,