python-iterutils 0.0.7__tar.gz → 0.0.8__tar.gz
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.
- {python_iterutils-0.0.7 → python_iterutils-0.0.8}/PKG-INFO +1 -1
- {python_iterutils-0.0.7 → python_iterutils-0.0.8}/iterutils/__init__.py +37 -20
- {python_iterutils-0.0.7 → python_iterutils-0.0.8}/pyproject.toml +1 -1
- {python_iterutils-0.0.7 → python_iterutils-0.0.8}/LICENSE +0 -0
- {python_iterutils-0.0.7 → python_iterutils-0.0.8}/iterutils/py.typed +0 -0
- {python_iterutils-0.0.7 → python_iterutils-0.0.8}/readme.md +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# encoding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 0,
|
|
5
|
+
__version__ = (0, 0, 8)
|
|
6
6
|
__all__ = [
|
|
7
7
|
"iterable", "async_iterable", "foreach", "async_foreach", "through", "async_through",
|
|
8
8
|
"wrap_iter", "wrap_aiter", "acc_step", "cut_iter", "run_gen_step", "run_gen_step_iter",
|
|
@@ -12,34 +12,35 @@ __all__ = [
|
|
|
12
12
|
from abc import ABC, abstractmethod
|
|
13
13
|
from asyncio import to_thread
|
|
14
14
|
from collections.abc import (
|
|
15
|
-
AsyncIterable, AsyncIterator,
|
|
15
|
+
AsyncIterable, AsyncIterator, Callable, Generator, Iterable, Iterator,
|
|
16
16
|
)
|
|
17
|
+
from dataclasses import dataclass
|
|
17
18
|
from inspect import isawaitable
|
|
18
|
-
from typing import overload, Any, Literal,
|
|
19
|
+
from typing import overload, Any, Literal, TypeVar
|
|
19
20
|
|
|
20
|
-
from asynctools import async_zip, ensure_async, ensure_aiter
|
|
21
|
+
from asynctools import async_map, async_zip, ensure_async, ensure_aiter
|
|
21
22
|
|
|
22
23
|
|
|
23
24
|
T = TypeVar("T")
|
|
24
25
|
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
@dataclass(slots=True, frozen=True)
|
|
28
|
+
class YieldBase(ABC):
|
|
29
|
+
value: Any
|
|
30
|
+
identity: bool = False
|
|
31
|
+
try_call_me: bool = True
|
|
32
|
+
|
|
28
33
|
@property
|
|
29
34
|
@abstractmethod
|
|
30
35
|
def yield_type(self, /) -> int:
|
|
31
36
|
...
|
|
32
37
|
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
value: Any
|
|
37
|
-
identity: bool = False
|
|
38
|
-
try_call_me: bool = True
|
|
39
|
-
yield_type = 1 # type: ignore
|
|
39
|
+
class Yield(YieldBase):
|
|
40
|
+
yield_type = 1
|
|
40
41
|
|
|
41
42
|
|
|
42
|
-
class YieldFrom(
|
|
43
|
+
class YieldFrom(YieldBase):
|
|
43
44
|
yield_type = 2
|
|
44
45
|
|
|
45
46
|
|
|
@@ -76,18 +77,30 @@ async def async_foreach(ret: Callable, iterable, /, *iterables, threaded: bool =
|
|
|
76
77
|
await ret(arg)
|
|
77
78
|
|
|
78
79
|
|
|
79
|
-
def through(it: Iterable,
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
def through(it: Iterable, /, take_while: None | Callable = None):
|
|
81
|
+
if take_while is None:
|
|
82
|
+
for _ in it:
|
|
83
|
+
pass
|
|
84
|
+
else:
|
|
85
|
+
for v in map(take_while, it):
|
|
86
|
+
if not v:
|
|
87
|
+
break
|
|
82
88
|
|
|
83
89
|
|
|
84
90
|
async def async_through(
|
|
85
91
|
it: Iterable | AsyncIterable,
|
|
86
92
|
/,
|
|
93
|
+
take_while: None | Callable = None,
|
|
87
94
|
threaded: bool = True,
|
|
88
95
|
):
|
|
89
|
-
|
|
90
|
-
|
|
96
|
+
it = ensure_aiter(it, threaded=threaded)
|
|
97
|
+
if take_while is None:
|
|
98
|
+
async for _ in it:
|
|
99
|
+
pass
|
|
100
|
+
else:
|
|
101
|
+
async for v in async_map(take_while, it):
|
|
102
|
+
if not v:
|
|
103
|
+
break
|
|
91
104
|
|
|
92
105
|
|
|
93
106
|
def wrap_iter(
|
|
@@ -309,8 +322,10 @@ def run_gen_step_iter(
|
|
|
309
322
|
identity = False
|
|
310
323
|
try_call_me = True
|
|
311
324
|
if isinstance(ret, YieldBase):
|
|
325
|
+
identity = ret.identity
|
|
326
|
+
try_call_me = ret.try_call_me
|
|
312
327
|
yield_type = ret.yield_type
|
|
313
|
-
ret
|
|
328
|
+
ret = ret.value
|
|
314
329
|
if not identity:
|
|
315
330
|
if try_call_me and callable(ret):
|
|
316
331
|
ret = ret()
|
|
@@ -364,8 +379,10 @@ def run_gen_step_iter(
|
|
|
364
379
|
identity = False
|
|
365
380
|
try_call_me = True
|
|
366
381
|
if isinstance(ret, YieldBase):
|
|
382
|
+
identity = ret.identity
|
|
383
|
+
try_call_me = ret.try_call_me
|
|
367
384
|
yield_type = ret.yield_type
|
|
368
|
-
ret
|
|
385
|
+
ret = ret.value
|
|
369
386
|
if not identity and try_call_me and callable(ret):
|
|
370
387
|
ret = ret()
|
|
371
388
|
return yield_type, ret
|
|
File without changes
|
|
File without changes
|
|
File without changes
|