python-iterutils 0.0.3__py3-none-any.whl → 0.0.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/.DS_Store ADDED
Binary file
iterutils/__init__.py CHANGED
@@ -2,13 +2,15 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 3)
5
+ __version__ = (0, 0, 4)
6
6
  __all__ = [
7
7
  "iterable", "async_iterable", "foreach", "async_foreach", "through", "async_through",
8
- "wrap_iter", "wrap_aiter", "acc_step", "cut_iter",
8
+ "wrap_iter", "wrap_aiter", "acc_step", "cut_iter", "run_gen_step",
9
9
  ]
10
10
 
11
- from collections.abc import AsyncIterable, AsyncIterator, Callable, Iterable, Iterator
11
+ from asyncio import to_thread
12
+ from collections.abc import AsyncIterable, AsyncIterator, Callable, Generator, Iterable, Iterator
13
+ from inspect import isawaitable
12
14
  from typing import Any, TypeVar
13
15
 
14
16
  from asynctools import async_zip, ensure_async, ensure_aiter
@@ -93,7 +95,8 @@ def wrap_iter(
93
95
  break
94
96
  except BaseException as e:
95
97
  if callable(callexit):
96
- callexit(it, e)
98
+ if not callexit(it, e):
99
+ raise
97
100
  else:
98
101
  raise
99
102
  finally:
@@ -161,3 +164,52 @@ def cut_iter(
161
164
  if start != stop:
162
165
  yield stop, stop - start
163
166
 
167
+
168
+ def run_gen_step(
169
+ gen_step: Generator[Callable, Any, T] | Callable[[], Generator[Callable, Any, T]],
170
+ *,
171
+ async_: bool = False,
172
+ threaded: bool = False,
173
+ ) -> T:
174
+ if callable(gen_step):
175
+ gen = gen_step()
176
+ close = gen.close
177
+ else:
178
+ gen = gen_step
179
+ close = None
180
+ send = gen.send
181
+ if async_:
182
+ async def process():
183
+ try:
184
+ if threaded:
185
+ func = await to_thread(send, None)
186
+ else:
187
+ func = send(None)
188
+ while True:
189
+ ret = func()
190
+ if isawaitable(ret):
191
+ ret = await ret
192
+ if threaded:
193
+ func = await to_thread(send, ret)
194
+ else:
195
+ func = send(ret)
196
+ except StopIteration as e:
197
+ return e.value
198
+ finally:
199
+ if close is not None:
200
+ if threaded:
201
+ await to_thread(close)
202
+ else:
203
+ close()
204
+ return process()
205
+ else:
206
+ try:
207
+ func = send(None)
208
+ while True:
209
+ func = send(func())
210
+ except StopIteration as e:
211
+ return e.value
212
+ finally:
213
+ if close is not None:
214
+ close()
215
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-iterutils
3
- Version: 0.0.3
3
+ Version: 0.0.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
@@ -0,0 +1,8 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ iterutils/.DS_Store,sha256=Kj3OcVIE2j02GFjUj8pr1YvdK4YSjlslELr_iRcRv68,6148
3
+ iterutils/__init__.py,sha256=M1KlG0BMF4oZIP4QU18m5riMnWO0oJKNP_SqVDlWKJo,6033
4
+ iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ python_iterutils-0.0.4.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
6
+ python_iterutils-0.0.4.dist-info/METADATA,sha256=3CGonLEb1lXVVrsrd-08p1vMexFfRTdE6MxeCneF4vU,1382
7
+ python_iterutils-0.0.4.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
8
+ python_iterutils-0.0.4.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- iterutils/__init__.py,sha256=OgXsu7O6TGsuz0Gwje-zNs7x_e_gd7_i3kbnCQSk_Tg,4535
3
- iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_iterutils-0.0.3.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_iterutils-0.0.3.dist-info/METADATA,sha256=zoTHrnVp4rEPFIglB0w9wXnPWFiClAa9njBGsOGvRAk,1382
6
- python_iterutils-0.0.3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
- python_iterutils-0.0.3.dist-info/RECORD,,