python-iterutils 0.0.3.1__tar.gz → 0.0.4.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-iterutils
3
- Version: 0.0.3.1
3
+ Version: 0.0.4.1
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
@@ -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
@@ -162,3 +164,65 @@ def cut_iter(
162
164
  if start != stop:
163
165
  yield stop, stop - start
164
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
+ throw = gen.throw
182
+ if async_:
183
+ async def process():
184
+ try:
185
+ if threaded:
186
+ func = await to_thread(send, None)
187
+ else:
188
+ func = send(None)
189
+ while True:
190
+ try:
191
+ ret = func()
192
+ if isawaitable(ret):
193
+ ret = await ret
194
+ except BaseException as e:
195
+ if threaded:
196
+ func = await to_thread(throw, e)
197
+ else:
198
+ func = throw(e)
199
+ else:
200
+ if threaded:
201
+ func = await to_thread(send, ret)
202
+ else:
203
+ func = send(ret)
204
+ except StopIteration as e:
205
+ return e.value
206
+ finally:
207
+ if close is not None:
208
+ if threaded:
209
+ await to_thread(close)
210
+ else:
211
+ close()
212
+ return process()
213
+ else:
214
+ try:
215
+ func = send(None)
216
+ while True:
217
+ try:
218
+ ret = func()
219
+ except BaseException as e:
220
+ func = throw(e)
221
+ else:
222
+ func = send(ret)
223
+ except StopIteration as e:
224
+ return e.value
225
+ finally:
226
+ if close is not None:
227
+ close()
228
+
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-iterutils"
3
- version = "0.0.3.1"
3
+ version = "0.0.4.1"
4
4
  description = "Python another itertools."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"