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
|
"""Non-pipable creation operators."""
from __future__ import annotations
import sys
import asyncio
import inspect
import builtins
import itertools
from typing import (
AsyncIterable,
Awaitable,
Iterable,
Protocol,
TypeVar,
AsyncIterator,
cast,
)
from typing_extensions import ParamSpec
from ..stream import time
from ..core import operator, streamcontext
__all__ = [
"iterate",
"preserve",
"just",
"call",
"throw",
"empty",
"never",
"repeat",
"range",
"count",
]
T = TypeVar("T")
P = ParamSpec("P")
# Hack for python 3.8 compatibility
if sys.version_info < (3, 9):
P = TypeVar("P")
# Convert regular iterables
@operator
async def from_iterable(it: Iterable[T]) -> AsyncIterator[T]:
"""Generate values from a regular iterable."""
for item in it:
await asyncio.sleep(0)
yield item
@operator
def from_async_iterable(ait: AsyncIterable[T]) -> AsyncIterator[T]:
"""Generate values from an asynchronous iterable.
Note: the corresponding iterator will be explicitely closed
when leaving the context manager."""
return streamcontext(ait)
@operator
def iterate(it: AsyncIterable[T] | Iterable[T]) -> AsyncIterator[T]:
"""Generate values from a sychronous or asynchronous iterable."""
if isinstance(it, AsyncIterable):
return from_async_iterable.raw(it)
if isinstance(it, Iterable):
return from_iterable.raw(it)
raise TypeError(f"{type(it).__name__!r} object is not (async) iterable")
@operator
async def preserve(ait: AsyncIterable[T]) -> AsyncIterator[T]:
"""Generate values from an asynchronous iterable without
explicitly closing the corresponding iterator."""
async for item in ait:
yield item
# Simple operators
@operator
async def just(value: T) -> AsyncIterator[T]:
"""Await if possible, and generate a single value."""
if inspect.isawaitable(value):
yield await value
else:
yield value
Y = TypeVar("Y", covariant=True)
class SyncCallable(Protocol[P, Y]):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Y:
...
class AsyncCallable(Protocol[P, Y]):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Awaitable[Y]:
...
@operator
async def call(
func: SyncCallable[P, T] | AsyncCallable[P, T], *args: P.args, **kwargs: P.kwargs
) -> AsyncIterator[T]:
"""Call the given function and generate a single value.
Await if the provided function is asynchronous.
"""
if asyncio.iscoroutinefunction(func):
async_func = cast("AsyncCallable[P, T]", func)
yield await async_func(*args, **kwargs)
else:
sync_func = cast("SyncCallable[P, T]", func)
yield sync_func(*args, **kwargs)
@operator
async def throw(exc: Exception) -> AsyncIterator[None]:
"""Throw an exception without generating any value."""
if False:
yield
raise exc
@operator
async def empty() -> AsyncIterator[None]:
"""Terminate without generating any value."""
if False:
yield
@operator
async def never() -> AsyncIterator[None]:
"""Hang forever without generating any value."""
if False:
yield
future: asyncio.Future[None] = asyncio.Future()
try:
await future
finally:
future.cancel()
@operator
def repeat(
value: T, times: int | None = None, *, interval: float = 0.0
) -> AsyncIterator[T]:
"""Generate the same value a given number of times.
If ``times`` is ``None``, the value is repeated indefinitely.
An optional interval can be given to space the values out.
"""
args = () if times is None else (times,)
it = itertools.repeat(value, *args)
agen = from_iterable.raw(it)
return time.spaceout.raw(agen, interval) if interval else agen
# Counting operators
@operator
def range(*args: int, interval: float = 0.0) -> AsyncIterator[int]:
"""Generate a given range of numbers.
It supports the same arguments as the builtin function.
An optional interval can be given to space the values out.
"""
agen = from_iterable.raw(builtins.range(*args))
return time.spaceout.raw(agen, interval) if interval else agen
@operator
def count(
start: int = 0, step: int = 1, *, interval: float = 0.0
) -> AsyncIterator[int]:
"""Generate consecutive numbers indefinitely.
Optional starting point and increment can be defined,
respectively defaulting to ``0`` and ``1``.
An optional interval can be given to space the values out.
"""
agen = from_iterable.raw(itertools.count(start, step))
return time.spaceout.raw(agen, interval) if interval else agen
|