python-iterutils 0.0.1__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.
- LICENSE +21 -0
- iterutils/__init__.py +109 -0
- iterutils/py.typed +0 -0
- python_iterutils-0.0.1.dist-info/LICENSE +21 -0
- python_iterutils-0.0.1.dist-info/METADATA +42 -0
- python_iterutils-0.0.1.dist-info/RECORD +7 -0
- python_iterutils-0.0.1.dist-info/WHEEL +4 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
iterutils/__init__.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
__version__ = (0, 0, 1)
|
|
6
|
+
__all__ = ["iterable", "async_iterable", "acc_step", "cut_iter", "asyncify_iter"]
|
|
7
|
+
|
|
8
|
+
from asyncio import to_thread
|
|
9
|
+
from collections.abc import AsyncGenerator, AsyncIterable, Generator, Iterable, Iterator
|
|
10
|
+
from typing import overload, Any, Optional, TypeVar
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
TI = TypeVar("TI")
|
|
14
|
+
TO = TypeVar("TO")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def iterable(it, /) -> bool:
|
|
18
|
+
try:
|
|
19
|
+
return isinstance(iter(it), Iterable)
|
|
20
|
+
except TypeError:
|
|
21
|
+
return False
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def async_iterable(it, /) -> bool:
|
|
25
|
+
try:
|
|
26
|
+
return isinstance(iter(it), AsyncIterable)
|
|
27
|
+
except TypeError:
|
|
28
|
+
return False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def acc_step(
|
|
32
|
+
start: int,
|
|
33
|
+
stop: Optional[int] = None,
|
|
34
|
+
step: int = 1,
|
|
35
|
+
) -> Iterator[tuple[int, int, int]]:
|
|
36
|
+
if stop is None:
|
|
37
|
+
start, stop = 0, start
|
|
38
|
+
for i in range(start + step, stop, step):
|
|
39
|
+
yield start, (start := i), step
|
|
40
|
+
if start != stop:
|
|
41
|
+
yield start, stop, stop - start
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def cut_iter(
|
|
45
|
+
start: int,
|
|
46
|
+
stop: Optional[int] = None,
|
|
47
|
+
step: int = 1,
|
|
48
|
+
) -> Iterator[tuple[int, int]]:
|
|
49
|
+
if stop is None:
|
|
50
|
+
start, stop = 0, start
|
|
51
|
+
for start in range(start + step, stop, step):
|
|
52
|
+
yield start, step
|
|
53
|
+
if start != stop:
|
|
54
|
+
yield stop, stop - start
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@overload
|
|
58
|
+
async def _asyncify_iter(it: Generator[TI, TO, Any], /, wait_for_thread: bool) -> AsyncGenerator[TI, TO]: ...
|
|
59
|
+
@overload
|
|
60
|
+
async def _asyncify_iter(it: Iterable[TO], /, wait_for_thread: bool) -> AsyncGenerator[Any, TO]: ...
|
|
61
|
+
async def _asyncify_iter(it, /, wait_for_thread: bool = False):
|
|
62
|
+
if wait_for_thread:
|
|
63
|
+
if isinstance(it, Generator):
|
|
64
|
+
send = it.send
|
|
65
|
+
def nextval(val):
|
|
66
|
+
try:
|
|
67
|
+
return send(val), None
|
|
68
|
+
except BaseException as e:
|
|
69
|
+
return None, e
|
|
70
|
+
else:
|
|
71
|
+
getnext = iter(it).__next__
|
|
72
|
+
def nextval(val):
|
|
73
|
+
try:
|
|
74
|
+
return getnext(), None
|
|
75
|
+
except BaseException as e:
|
|
76
|
+
return None, e
|
|
77
|
+
val = None
|
|
78
|
+
while True:
|
|
79
|
+
yield_val, exc = await to_thread(nextval, val)
|
|
80
|
+
if exc is None:
|
|
81
|
+
yield yield_val
|
|
82
|
+
elif isinstance(exc, StopIteration):
|
|
83
|
+
break
|
|
84
|
+
else:
|
|
85
|
+
raise exc
|
|
86
|
+
elif isinstance(it, Generator):
|
|
87
|
+
send = it.send
|
|
88
|
+
val = None
|
|
89
|
+
try:
|
|
90
|
+
while True:
|
|
91
|
+
val = yield send(val)
|
|
92
|
+
except StopIteration:
|
|
93
|
+
pass
|
|
94
|
+
else:
|
|
95
|
+
for val in it:
|
|
96
|
+
yield val
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@overload
|
|
100
|
+
def asyncify_iter(it: AsyncIterable[TO], /, wait_for_thread: bool) -> AsyncIterable[TO]: ...
|
|
101
|
+
@overload
|
|
102
|
+
def asyncify_iter(it: Generator[TI, TO, Any], /, wait_for_thread: bool) -> AsyncGenerator[TI, TO]: ...
|
|
103
|
+
@overload
|
|
104
|
+
def asyncify_iter(it: Iterable[TO], /, wait_for_thread: bool) -> AsyncGenerator[Any, TO]: ...
|
|
105
|
+
def asyncify_iter(it, /, wait_for_thread: bool = False):
|
|
106
|
+
if isinstance(it, AsyncIterable):
|
|
107
|
+
return it
|
|
108
|
+
return _asyncify_iter(it, wait_for_thread=wait_for_thread)
|
|
109
|
+
|
iterutils/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: python-iterutils
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python another itertools.
|
|
5
|
+
Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-iterutils
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: iterable,iterutils
|
|
8
|
+
Author: ChenyangGao
|
|
9
|
+
Author-email: wosiwujm@gmail.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Topic :: Software Development
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-iterutils
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Python another itertools.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
You can install from [pypi](https://pypi.org/project/python-iterutils/)
|
|
32
|
+
|
|
33
|
+
```console
|
|
34
|
+
pip install -U python-iterutils
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import iterutils
|
|
41
|
+
```
|
|
42
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
+
iterutils/__init__.py,sha256=rlt5eSf9A7_58WR8dDYgS-Fe40nHMGiwurUeymnuyro,3123
|
|
3
|
+
iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
python_iterutils-0.0.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
5
|
+
python_iterutils-0.0.1.dist-info/METADATA,sha256=wByxjlr3955Z05zMMiOncJ2dwkA86vHRsV72UXn2D1I,1349
|
|
6
|
+
python_iterutils-0.0.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
|
7
|
+
python_iterutils-0.0.1.dist-info/RECORD,,
|