python-iterutils 0.2.10__tar.gz → 0.2.10.2__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.2.10 → python_iterutils-0.2.10.2}/PKG-INFO +5 -3
- {python_iterutils-0.2.10 → python_iterutils-0.2.10.2}/iterutils/__init__.py +104 -51
- {python_iterutils-0.2.10 → python_iterutils-0.2.10.2}/pyproject.toml +1 -1
- {python_iterutils-0.2.10 → python_iterutils-0.2.10.2}/LICENSE +0 -0
- {python_iterutils-0.2.10 → python_iterutils-0.2.10.2}/iterutils/py.typed +0 -0
- {python_iterutils-0.2.10 → python_iterutils-0.2.10.2}/readme.md +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: python-iterutils
|
|
3
|
-
Version: 0.2.10
|
|
3
|
+
Version: 0.2.10.2
|
|
4
4
|
Summary: Python another itertools.
|
|
5
|
-
Home-page: https://github.com/ChenyangGao/python-modules/tree/main/python-iterutils
|
|
6
5
|
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
7
|
Keywords: iterable,iterutils
|
|
8
8
|
Author: ChenyangGao
|
|
9
9
|
Author-email: wosiwujm@gmail.com
|
|
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python
|
|
|
16
16
|
Classifier: Programming Language :: Python :: 3
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.12
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
20
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
21
|
Classifier: Topic :: Software Development
|
|
21
22
|
Classifier: Topic :: Software Development :: Libraries
|
|
@@ -23,6 +24,7 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
|
23
24
|
Requires-Dist: python-asynctools (>=0.1.3.4)
|
|
24
25
|
Requires-Dist: python-texttools (>=0.0.3)
|
|
25
26
|
Requires-Dist: python-undefined (>=0.0.3)
|
|
27
|
+
Project-URL: Homepage, https://github.com/ChenyangGao/python-modules/tree/main/python-iterutils
|
|
26
28
|
Project-URL: Repository, https://github.com/ChenyangGao/python-modules/tree/main/python-iterutils
|
|
27
29
|
Description-Content-Type: text/markdown
|
|
28
30
|
|
|
@@ -12,17 +12,18 @@ __all__ = [
|
|
|
12
12
|
"flatten", "async_flatten", "collect", "async_collect",
|
|
13
13
|
"group_collect", "async_group_collect", "iter_unique",
|
|
14
14
|
"async_iter_unique", "wrap_iter", "wrap_aiter", "peek_iter", "peek_aiter",
|
|
15
|
-
"acc_step", "cut_iter", "
|
|
15
|
+
"acc_step", "cut_iter", "context", "backgroud_loop", "gen_startup",
|
|
16
|
+
"async_gen_startup", "do_iter", "do_aiter", "bfs_iter", "bfs_gen",
|
|
16
17
|
]
|
|
17
18
|
|
|
18
19
|
from asyncio import create_task, sleep as async_sleep
|
|
19
20
|
from builtins import map as _map, filter as _filter, zip as _zip
|
|
20
21
|
from collections import defaultdict, deque
|
|
21
22
|
from collections.abc import (
|
|
22
|
-
AsyncIterable, AsyncIterator, Awaitable, Buffer,
|
|
23
|
-
Collection, Container, Coroutine, Generator, Iterable,
|
|
24
|
-
Mapping, MutableMapping, MutableSet, MutableSequence,
|
|
25
|
-
ValuesView,
|
|
23
|
+
AsyncGenerator, AsyncIterable, AsyncIterator, Awaitable, Buffer,
|
|
24
|
+
Callable, Collection, Container, Coroutine, Generator, Iterable,
|
|
25
|
+
Iterator, Mapping, MutableMapping, MutableSet, MutableSequence,
|
|
26
|
+
Sequence, ValuesView,
|
|
26
27
|
)
|
|
27
28
|
from contextlib import (
|
|
28
29
|
asynccontextmanager, contextmanager, ExitStack, AsyncExitStack,
|
|
@@ -30,6 +31,7 @@ from contextlib import (
|
|
|
30
31
|
)
|
|
31
32
|
from copy import copy
|
|
32
33
|
from dataclasses import dataclass
|
|
34
|
+
from functools import update_wrapper
|
|
33
35
|
from itertools import batched, chain as _chain, pairwise
|
|
34
36
|
from inspect import iscoroutinefunction, signature
|
|
35
37
|
from sys import _getframe, exc_info
|
|
@@ -624,26 +626,52 @@ def chain[T](
|
|
|
624
626
|
@overload
|
|
625
627
|
def chain_from_iterable[T](
|
|
626
628
|
iterables: Iterable[Iterable[T]],
|
|
627
|
-
threaded:
|
|
629
|
+
threaded: bool = False,
|
|
628
630
|
*,
|
|
629
631
|
async_: Literal[False] = False,
|
|
630
632
|
) -> Iterator[T]:
|
|
631
633
|
...
|
|
632
634
|
@overload
|
|
633
635
|
def chain_from_iterable[T](
|
|
634
|
-
iterables:
|
|
636
|
+
iterables: (
|
|
637
|
+
AsyncIterable[Iterable[T]] |
|
|
638
|
+
AsyncIterable[AsyncIterable[T]] |
|
|
639
|
+
AsyncIterable[Iterable[T] | AsyncIterable[T]]
|
|
640
|
+
),
|
|
641
|
+
threaded: bool = False,
|
|
642
|
+
*,
|
|
643
|
+
async_: bool = False,
|
|
644
|
+
) -> AsyncIterator[T]:
|
|
645
|
+
...
|
|
646
|
+
@overload
|
|
647
|
+
def chain_from_iterable[T](
|
|
648
|
+
iterables: (
|
|
649
|
+
Iterable[Iterable[T]] |
|
|
650
|
+
Iterable[AsyncIterable[T]] |
|
|
651
|
+
Iterable[Iterable[T] | AsyncIterable[T]] |
|
|
652
|
+
AsyncIterable[Iterable[T]] |
|
|
653
|
+
AsyncIterable[AsyncIterable[T]] |
|
|
654
|
+
AsyncIterable[Iterable[T] | AsyncIterable[T]]
|
|
655
|
+
),
|
|
635
656
|
threaded: bool = False,
|
|
636
657
|
*,
|
|
637
658
|
async_: Literal[True],
|
|
638
659
|
) -> AsyncIterator[T]:
|
|
639
660
|
...
|
|
640
661
|
def chain_from_iterable[T](
|
|
641
|
-
iterables:
|
|
662
|
+
iterables: (
|
|
663
|
+
Iterable[Iterable[T]] |
|
|
664
|
+
Iterable[AsyncIterable[T]] |
|
|
665
|
+
Iterable[Iterable[T] | AsyncIterable[T]] |
|
|
666
|
+
AsyncIterable[Iterable[T]] |
|
|
667
|
+
AsyncIterable[AsyncIterable[T]] |
|
|
668
|
+
AsyncIterable[Iterable[T] | AsyncIterable[T]]
|
|
669
|
+
),
|
|
642
670
|
threaded: bool = False,
|
|
643
671
|
*,
|
|
644
672
|
async_: Literal[False, True] = False,
|
|
645
673
|
) -> Iterator[T] | AsyncIterator[T]:
|
|
646
|
-
if async_ or
|
|
674
|
+
if async_ or not isinstance(iterables, Iterable):
|
|
647
675
|
if isinstance(iterables, Iterable):
|
|
648
676
|
return async_chain.from_iterable(iterables, threaded=threaded)
|
|
649
677
|
else:
|
|
@@ -1264,48 +1292,6 @@ def cut_iter(
|
|
|
1264
1292
|
yield stop, stop - start
|
|
1265
1293
|
|
|
1266
1294
|
|
|
1267
|
-
@overload
|
|
1268
|
-
def bfs_gen[T](
|
|
1269
|
-
initial: T,
|
|
1270
|
-
/,
|
|
1271
|
-
unpack_iterator: Literal[False] = False,
|
|
1272
|
-
) -> Generator[T, T | None, None]:
|
|
1273
|
-
...
|
|
1274
|
-
@overload
|
|
1275
|
-
def bfs_gen[T](
|
|
1276
|
-
initial: T | Iterator[T],
|
|
1277
|
-
/,
|
|
1278
|
-
unpack_iterator: Literal[True],
|
|
1279
|
-
) -> Generator[T, T | None, None]:
|
|
1280
|
-
...
|
|
1281
|
-
def bfs_gen[T](
|
|
1282
|
-
initial: T | Iterator[T],
|
|
1283
|
-
/,
|
|
1284
|
-
unpack_iterator: bool = False,
|
|
1285
|
-
) -> Generator[T, T | None, None]:
|
|
1286
|
-
"""辅助函数,返回生成器,用来简化广度优先遍历
|
|
1287
|
-
"""
|
|
1288
|
-
dq: deque[T] = deque()
|
|
1289
|
-
push, pushmany, pop = dq.append, dq.extend, dq.popleft
|
|
1290
|
-
if isinstance(initial, Iterator) and unpack_iterator:
|
|
1291
|
-
pushmany(initial)
|
|
1292
|
-
else:
|
|
1293
|
-
push(initial) # type: ignore
|
|
1294
|
-
while dq:
|
|
1295
|
-
args: None | T = yield (val := pop())
|
|
1296
|
-
if unpack_iterator:
|
|
1297
|
-
while args is not None:
|
|
1298
|
-
if isinstance(args, Iterator):
|
|
1299
|
-
pushmany(args)
|
|
1300
|
-
else:
|
|
1301
|
-
push(args)
|
|
1302
|
-
args = yield val
|
|
1303
|
-
else:
|
|
1304
|
-
while args is not None:
|
|
1305
|
-
push(args)
|
|
1306
|
-
args = yield val
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
1295
|
@overload
|
|
1310
1296
|
def context[T](
|
|
1311
1297
|
func: Callable[..., T],
|
|
@@ -1445,3 +1431,70 @@ def backgroud_loop(
|
|
|
1445
1431
|
print("\r\x1b[K", end="")
|
|
1446
1432
|
return ctx()
|
|
1447
1433
|
|
|
1434
|
+
|
|
1435
|
+
def gen_startup[**Args, G: Generator](func: Callable[Args, G], /):
|
|
1436
|
+
def wrapper(*args: Args.args, **kwds: Args.kwargs) -> G:
|
|
1437
|
+
gen = func(*args, **kwds)
|
|
1438
|
+
next(gen)
|
|
1439
|
+
return gen
|
|
1440
|
+
return update_wrapper(wrapper, func)
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
def async_gen_startup[**Args, G: AsyncGenerator](func: Callable[Args, G], /):
|
|
1444
|
+
async def wrapper(*args: Args.args, **kwds: Args.kwargs) -> G:
|
|
1445
|
+
gen = func(*args, **kwds)
|
|
1446
|
+
await anext(gen)
|
|
1447
|
+
return gen
|
|
1448
|
+
return update_wrapper(wrapper, func)
|
|
1449
|
+
|
|
1450
|
+
|
|
1451
|
+
def do_iter[T](
|
|
1452
|
+
func: Callable[[], T] | Iterable[T],
|
|
1453
|
+
/,
|
|
1454
|
+
sentinel=undefined,
|
|
1455
|
+
sentinel_excs: type[BaseException] | tuple[type[BaseException], ...] = (),
|
|
1456
|
+
) -> Iterator[T]:
|
|
1457
|
+
try:
|
|
1458
|
+
yield from iter(func if callable(func) else iter(func).__next__, sentinel)
|
|
1459
|
+
except sentinel_excs:
|
|
1460
|
+
pass
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
async def do_aiter[T](
|
|
1464
|
+
func: Callable[[], T] | Callable[[], Awaitable[T]] | Iterable[T] | AsyncIterable[T],
|
|
1465
|
+
/,
|
|
1466
|
+
sentinel=undefined,
|
|
1467
|
+
sentinel_excs: type[BaseException] | tuple[type[BaseException], ...] = (),
|
|
1468
|
+
) -> AsyncIterator[T]:
|
|
1469
|
+
if callable(func):
|
|
1470
|
+
func = ensure_async(func)
|
|
1471
|
+
else:
|
|
1472
|
+
func = ensure_aiter(func).__anext__
|
|
1473
|
+
try:
|
|
1474
|
+
while True:
|
|
1475
|
+
v = await func()
|
|
1476
|
+
if v is sentinel:
|
|
1477
|
+
break
|
|
1478
|
+
yield v
|
|
1479
|
+
except StopAsyncIteration:
|
|
1480
|
+
pass
|
|
1481
|
+
except sentinel_excs:
|
|
1482
|
+
pass
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
def bfs_iter[T](*initials: T) -> tuple[Iterator[T], Callable[[T], None]]:
|
|
1486
|
+
dq = deque(initials)
|
|
1487
|
+
return do_iter(dq.popleft, sentinel_excs=IndexError), dq.append
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
@gen_startup
|
|
1491
|
+
def bfs_gen[T](*initials) -> Generator[None | T, T | None, None]:
|
|
1492
|
+
dq = deque(initials)
|
|
1493
|
+
push, pop = dq.append, dq.popleft
|
|
1494
|
+
try:
|
|
1495
|
+
p = yield None
|
|
1496
|
+
while True:
|
|
1497
|
+
p = yield (pop() if p is None else push(p))
|
|
1498
|
+
except IndexError:
|
|
1499
|
+
pass
|
|
1500
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|