python-iterutils 0.1.3__py3-none-any.whl → 0.1.4__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.
- iterutils/__init__.py +104 -17
- {python_iterutils-0.1.3.dist-info → python_iterutils-0.1.4.dist-info}/METADATA +2 -1
- python_iterutils-0.1.4.dist-info/RECORD +7 -0
- python_iterutils-0.1.3.dist-info/RECORD +0 -7
- {python_iterutils-0.1.3.dist-info → python_iterutils-0.1.4.dist-info}/LICENSE +0 -0
- {python_iterutils-0.1.3.dist-info → python_iterutils-0.1.4.dist-info}/WHEEL +0 -0
iterutils/__init__.py
CHANGED
|
@@ -2,30 +2,34 @@
|
|
|
2
2
|
# encoding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 1,
|
|
5
|
+
__version__ = (0, 1, 4)
|
|
6
6
|
__all__ = [
|
|
7
7
|
"Return", "Yield", "YieldFrom", "iterable", "async_iterable", "foreach", "async_foreach",
|
|
8
|
-
"through", "async_through", "flatten", "async_flatten", "map", "filter", "reduce", "
|
|
9
|
-
"iter_unique", "async_iter_unique", "wrap_iter", "wrap_aiter", "acc_step",
|
|
10
|
-
"run_gen_step", "run_gen_step_iter", "bfs_gen", "with_iter_next",
|
|
8
|
+
"through", "async_through", "flatten", "async_flatten", "map", "filter", "reduce", "zip",
|
|
9
|
+
"chunked", "iter_unique", "async_iter_unique", "wrap_iter", "wrap_aiter", "acc_step",
|
|
10
|
+
"cut_iter", "run_gen_step", "run_gen_step_iter", "bfs_gen", "with_iter_next", "backgroud_loop",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
13
|
from abc import ABC, abstractmethod
|
|
14
|
-
from asyncio import to_thread
|
|
14
|
+
from asyncio import create_task, sleep as async_sleep, to_thread
|
|
15
|
+
from builtins import map as _map, filter as _filter, zip as _zip
|
|
15
16
|
from collections import deque
|
|
16
17
|
from collections.abc import (
|
|
17
18
|
AsyncIterable, AsyncIterator, Awaitable, Buffer, Callable, Generator,
|
|
18
19
|
Iterable, Iterator, MutableSet, Sequence,
|
|
19
20
|
)
|
|
20
|
-
from contextlib import contextmanager
|
|
21
|
+
from contextlib import asynccontextmanager, contextmanager
|
|
21
22
|
from dataclasses import dataclass
|
|
22
23
|
from itertools import batched, pairwise
|
|
23
24
|
from inspect import isawaitable, iscoroutinefunction
|
|
24
|
-
from
|
|
25
|
+
from _thread import start_new_thread
|
|
26
|
+
from time import sleep, time
|
|
27
|
+
from typing import overload, Any, AsyncContextManager, ContextManager, Literal
|
|
25
28
|
|
|
26
29
|
from asynctools import (
|
|
27
30
|
async_filter, async_map, async_reduce, async_zip, async_batched, ensure_async, ensure_aiter,
|
|
28
31
|
)
|
|
32
|
+
from texttools import format_time
|
|
29
33
|
from undefined import undefined, Undefined
|
|
30
34
|
|
|
31
35
|
|
|
@@ -76,7 +80,7 @@ def foreach(
|
|
|
76
80
|
if not (isinstance(iterable, Iterable) and all(isinstance(it, Iterable) for it in iterables)):
|
|
77
81
|
return async_foreach(value, iterable, *iterables)
|
|
78
82
|
if iterables:
|
|
79
|
-
for args in
|
|
83
|
+
for args in _zip(iterable, *iterables):
|
|
80
84
|
value(*args)
|
|
81
85
|
else:
|
|
82
86
|
for arg in iterable:
|
|
@@ -110,8 +114,7 @@ def through(
|
|
|
110
114
|
for _ in iterable:
|
|
111
115
|
pass
|
|
112
116
|
else:
|
|
113
|
-
|
|
114
|
-
for v in map(take_while, iterable):
|
|
117
|
+
for v in _map(take_while, iterable):
|
|
115
118
|
if not v:
|
|
116
119
|
break
|
|
117
120
|
|
|
@@ -181,30 +184,38 @@ async def async_flatten(
|
|
|
181
184
|
|
|
182
185
|
|
|
183
186
|
def map(
|
|
184
|
-
function: Callable,
|
|
187
|
+
function: None | Callable,
|
|
185
188
|
iterable: Iterable | AsyncIterable,
|
|
186
189
|
/,
|
|
187
190
|
*iterables: Iterable | AsyncIterable,
|
|
188
|
-
):
|
|
191
|
+
):
|
|
189
192
|
if (
|
|
190
193
|
iscoroutinefunction(function) or
|
|
191
194
|
isinstance(iterable, AsyncIterable) or
|
|
192
195
|
any(isinstance(i, AsyncIterable) for i in iterables)
|
|
193
196
|
):
|
|
197
|
+
if function is None:
|
|
198
|
+
if iterables:
|
|
199
|
+
return async_zip(iterable, *iterables)
|
|
200
|
+
else:
|
|
201
|
+
return iterable
|
|
194
202
|
return async_map(function, iterable, *iterables)
|
|
195
|
-
|
|
196
|
-
|
|
203
|
+
if function is None:
|
|
204
|
+
if iterables:
|
|
205
|
+
return _zip(iterable, *iterables)
|
|
206
|
+
else:
|
|
207
|
+
return iterable
|
|
208
|
+
return _map(function, iterable, *iterables)
|
|
197
209
|
|
|
198
210
|
|
|
199
211
|
def filter(
|
|
200
|
-
function: Callable,
|
|
212
|
+
function: None | Callable,
|
|
201
213
|
iterable: Iterable | AsyncIterable,
|
|
202
214
|
/,
|
|
203
215
|
):
|
|
204
216
|
if iscoroutinefunction(function) or isinstance(iterable, AsyncIterable):
|
|
205
217
|
return async_filter(function, iterable)
|
|
206
|
-
|
|
207
|
-
return filter(function, iterable)
|
|
218
|
+
return _filter(function, iterable)
|
|
208
219
|
|
|
209
220
|
|
|
210
221
|
def reduce(
|
|
@@ -221,6 +232,16 @@ def reduce(
|
|
|
221
232
|
return reduce(function, iterable, initial)
|
|
222
233
|
|
|
223
234
|
|
|
235
|
+
def zip(
|
|
236
|
+
iterable: Iterable | AsyncIterable,
|
|
237
|
+
/,
|
|
238
|
+
*iterables: Iterable | AsyncIterable,
|
|
239
|
+
):
|
|
240
|
+
if isinstance(iterable, AsyncIterable) or any(isinstance(i, AsyncIterable) for i in iterables):
|
|
241
|
+
return async_zip(iterable, *iterables)
|
|
242
|
+
return _zip(iterable, *iterables)
|
|
243
|
+
|
|
244
|
+
|
|
224
245
|
@overload
|
|
225
246
|
def chunked[T](
|
|
226
247
|
iterable: Iterable[T],
|
|
@@ -747,3 +768,69 @@ def with_iter_next[T](
|
|
|
747
768
|
except StopIteration:
|
|
748
769
|
pass
|
|
749
770
|
|
|
771
|
+
|
|
772
|
+
@overload
|
|
773
|
+
def backgroud_loop(
|
|
774
|
+
call: None | Callable = None,
|
|
775
|
+
/,
|
|
776
|
+
interval: int | float = 0.05,
|
|
777
|
+
*,
|
|
778
|
+
async_: Literal[False] = False,
|
|
779
|
+
) -> ContextManager:
|
|
780
|
+
...
|
|
781
|
+
@overload
|
|
782
|
+
def backgroud_loop(
|
|
783
|
+
call: None | Callable = None,
|
|
784
|
+
/,
|
|
785
|
+
interval: int | float = 0.05,
|
|
786
|
+
*,
|
|
787
|
+
async_: Literal[True],
|
|
788
|
+
) -> AsyncContextManager:
|
|
789
|
+
...
|
|
790
|
+
def backgroud_loop(
|
|
791
|
+
call: None | Callable = None,
|
|
792
|
+
/,
|
|
793
|
+
interval: int | float = 0.05,
|
|
794
|
+
*,
|
|
795
|
+
async_: Literal[False, True] = False,
|
|
796
|
+
) -> ContextManager | AsyncContextManager:
|
|
797
|
+
use_default_call = not callable(call)
|
|
798
|
+
if use_default_call:
|
|
799
|
+
start = time()
|
|
800
|
+
def call():
|
|
801
|
+
print(f"\r\x1b[K{format_time(time() - start)}", end="")
|
|
802
|
+
def run():
|
|
803
|
+
while running:
|
|
804
|
+
try:
|
|
805
|
+
yield call
|
|
806
|
+
except Exception:
|
|
807
|
+
pass
|
|
808
|
+
if interval > 0:
|
|
809
|
+
if async_:
|
|
810
|
+
yield async_sleep(interval)
|
|
811
|
+
else:
|
|
812
|
+
sleep(interval)
|
|
813
|
+
running = True
|
|
814
|
+
if async_:
|
|
815
|
+
async def actx():
|
|
816
|
+
nonlocal running
|
|
817
|
+
try:
|
|
818
|
+
task = create_task(run())
|
|
819
|
+
yield task
|
|
820
|
+
finally:
|
|
821
|
+
running = False
|
|
822
|
+
task.cancel()
|
|
823
|
+
if use_default_call:
|
|
824
|
+
print("\r\x1b[K", end="")
|
|
825
|
+
return asynccontextmanager(actx)()
|
|
826
|
+
else:
|
|
827
|
+
def ctx():
|
|
828
|
+
nonlocal running
|
|
829
|
+
try:
|
|
830
|
+
yield start_new_thread(run, ())
|
|
831
|
+
finally:
|
|
832
|
+
running = False
|
|
833
|
+
if use_default_call:
|
|
834
|
+
print("\r\x1b[K", end="")
|
|
835
|
+
return contextmanager(ctx)()
|
|
836
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: python-iterutils
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Python another itertools.
|
|
5
5
|
Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-iterutils
|
|
6
6
|
License: MIT
|
|
@@ -21,6 +21,7 @@ Classifier: Topic :: Software Development
|
|
|
21
21
|
Classifier: Topic :: Software Development :: Libraries
|
|
22
22
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
23
|
Requires-Dist: python-asynctools (>=0.0.10)
|
|
24
|
+
Requires-Dist: python-texttools (>=0.0.3)
|
|
24
25
|
Requires-Dist: python-undefined (>=0.0.3)
|
|
25
26
|
Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-iterutils
|
|
26
27
|
Description-Content-Type: text/markdown
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
+
iterutils/__init__.py,sha256=aYU53GoLQ7CYy-rcts8b47yo6dp0lJncxs5UPRHb_YA,24752
|
|
3
|
+
iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
python_iterutils-0.1.4.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
5
|
+
python_iterutils-0.1.4.dist-info/METADATA,sha256=1siMQz1thjXxHUIQpTkxst6IY2Y3kfBRdeLeQIWRFOA,1426
|
|
6
|
+
python_iterutils-0.1.4.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
7
|
+
python_iterutils-0.1.4.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
-
iterutils/__init__.py,sha256=5oKgPEE_IkYDJs6oz-EbZl4sbjil2KIpif-m9C--7So,22262
|
|
3
|
-
iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
python_iterutils-0.1.3.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
5
|
-
python_iterutils-0.1.3.dist-info/METADATA,sha256=pjYgGntBIyLr1k2Z2dftvgpWrHHHFmCXSPTS0gJdHIg,1384
|
|
6
|
-
python_iterutils-0.1.3.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
7
|
-
python_iterutils-0.1.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|