hyperion-sdk 0.2.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.
hyperion/__init__.py ADDED
File without changes
hyperion/asyncutils.py ADDED
@@ -0,0 +1,79 @@
1
+ import asyncio
2
+ import inspect
3
+ import sys
4
+ from collections.abc import AsyncIterable, AsyncIterator, Callable, Coroutine, Iterable
5
+ from typing import TYPE_CHECKING, Any, Generic, TypeVar
6
+
7
+ from hyperion.logging import get_logger
8
+
9
+ if sys.version_info >= (3, 11) and TYPE_CHECKING:
10
+ from typing import Self
11
+
12
+ if sys.version_info < (3, 11) and TYPE_CHECKING:
13
+ from typing_extensions import Self
14
+
15
+ T = TypeVar("T")
16
+
17
+
18
+ logger = get_logger("hyperion-asyncutils")
19
+
20
+
21
+ async def iter_async(iterable: Iterable[T]) -> AsyncIterator[T]:
22
+ for item in iterable:
23
+ yield item
24
+
25
+
26
+ async def aiter_any(
27
+ iterable: Iterable[T] | AsyncIterable[T] | Callable[[], Iterable[T] | AsyncIterable[T]],
28
+ ) -> AsyncIterator[T]:
29
+ asyncgen: AsyncIterable[T]
30
+ if inspect.isasyncgen(iterable) or isinstance(iterable, AsyncIterable):
31
+ asyncgen = iterable
32
+ elif inspect.isasyncgenfunction(iterable):
33
+ asyncgen = iterable()
34
+ elif inspect.isgenerator(iterable) or isinstance(iterable, Iterable):
35
+ asyncgen = iter_async(iterable)
36
+ elif inspect.isgeneratorfunction(iterable):
37
+ asyncgen = iter_async(iterable())
38
+ else:
39
+ raise TypeError("Provided value cannot be iterated over.")
40
+ async for item in asyncgen:
41
+ yield item
42
+
43
+
44
+ def get_loop() -> asyncio.AbstractEventLoop:
45
+ """Get the current running loop or create a new one if not running."""
46
+ try:
47
+ return asyncio.get_running_loop()
48
+ except RuntimeError:
49
+ loop = asyncio.new_event_loop()
50
+ asyncio.set_event_loop(loop)
51
+ return loop
52
+
53
+
54
+ class AsyncTaskQueue(Generic[T]):
55
+ def __init__(self, maxsize: int = 0) -> None:
56
+ self.tasklist: list[asyncio.Task[T]] = []
57
+ self._entered = False
58
+ self.maxsize = maxsize
59
+
60
+ async def _flush(self) -> None:
61
+ logger.debug(f"Max size of {self.maxsize} reached, waiting for enqueued tasks to finish.")
62
+ await asyncio.gather(*self.tasklist)
63
+ self.tasklist = []
64
+
65
+ async def __aenter__(self) -> "Self":
66
+ self.tasklist = []
67
+ self._entered = True
68
+ return self
69
+
70
+ async def __aexit__(self, *args: Any) -> None:
71
+ self._entered = False
72
+ await self._flush()
73
+
74
+ async def add_task(self, coroutine: Coroutine[Any, Any, T]) -> None:
75
+ if not self._entered:
76
+ raise RuntimeError(f"{self.__class__.__name__!r} must be used in an 'async with' context.")
77
+ if self.maxsize and len(self.tasklist) >= self.maxsize:
78
+ await self._flush()
79
+ self.tasklist.append(asyncio.create_task(coroutine))
@@ -0,0 +1,8 @@
1
+ from .catalog import AssetNotFoundError, Catalog
2
+ from .schema import SchemaStore
3
+
4
+ __all__ = [
5
+ "AssetNotFoundError",
6
+ "Catalog",
7
+ "SchemaStore",
8
+ ]