python-iterutils 0.2.5.2__py3-none-any.whl → 0.2.5.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 CHANGED
@@ -6,7 +6,7 @@ __version__ = (0, 2, 5)
6
6
  __all__ = [
7
7
  "Yield", "YieldFrom", "iterable", "async_iterable",
8
8
  "run_gen_step", "run_gen_step_iter", "as_gen_step",
9
- "as_gen_step_iter", "split_cm, ""with_iter_next",
9
+ "as_gen_step_iter", "split_cm", "with_iter_next",
10
10
  "map", "filter", "reduce", "zip", "chain",
11
11
  "chain_from_iterable", "chunked", "foreach",
12
12
  "async_foreach", "through", "async_through",
@@ -32,7 +32,7 @@ from contextlib import (
32
32
  from copy import copy
33
33
  from dataclasses import dataclass
34
34
  from itertools import batched, chain as _chain, pairwise
35
- from inspect import isawaitable, iscoroutinefunction, signature
35
+ from inspect import iscoroutinefunction, signature
36
36
  from sys import _getframe, exc_info
37
37
  from _thread import start_new_thread
38
38
  from time import sleep, time
@@ -63,16 +63,9 @@ class YieldFrom:
63
63
  value: Any
64
64
 
65
65
 
66
- def iterable(obj, /) -> bool:
67
- """判断对象是不是 Iterable
68
- """
69
- return isinstance(obj, Iterable)
70
-
71
-
72
- def async_iterable(obj, /) -> bool:
73
- """判断对象是不是 AsyncIterable
74
- """
75
- return isinstance(obj, AsyncIterable)
66
+ iterable = Iterable.__instancecheck__
67
+ async_iterable = AsyncIterable.__instancecheck__
68
+ isawaitable = Awaitable.__instancecheck__
76
69
 
77
70
 
78
71
  def _coalesce(vals, default=None):
@@ -114,14 +107,17 @@ async def _run_gen_step_async(gen: Generator, /):
114
107
  send = gen.send
115
108
  throw = gen.throw
116
109
  try:
117
- ret: Awaitable = send(None)
110
+ ret: Any = send(None)
118
111
  while True:
119
- try:
120
- value: Any = await ret
121
- except BaseException as e:
122
- ret = throw(e)
112
+ if isawaitable(ret):
113
+ try:
114
+ value: Any = (await ret)
115
+ except BaseException as e:
116
+ ret = throw(e)
117
+ else:
118
+ ret = send(value)
123
119
  else:
124
- ret = send(value)
120
+ ret = send(ret)
125
121
  except StopIteration as e:
126
122
  if isawaitable(e.value):
127
123
  return await e.value
@@ -178,10 +174,10 @@ async def _run_gen_step_async_iter(gen: Generator, /) -> AsyncIterator:
178
174
  send = gen.send
179
175
  throw = gen.throw
180
176
  try:
181
- ret: Awaitable | Yield | YieldFrom = send(None)
177
+ ret: Any = send(None)
182
178
  while True:
183
179
  try:
184
- if isinstance(ret, Awaitable):
180
+ if isawaitable(ret):
185
181
  value: Any = await ret
186
182
  else:
187
183
  value = ret.value
@@ -484,9 +480,12 @@ def zip(
484
480
  ):
485
481
  """
486
482
  """
487
- if threaded or isinstance(iterable, AsyncIterable) or any(isinstance(i, AsyncIterable) for i in iterables):
488
- return async_zip(iterable, *iterables, threaded=threaded)
489
- return _zip(iterable, *iterables)
483
+ if (not threaded and
484
+ isinstance(iterable, Iterable) and
485
+ all(isinstance(it, Iterable) for it in iterables)
486
+ ):
487
+ return _zip(iterable, *iterables)
488
+ return async_zip(iterable, *iterables, threaded=threaded)
490
489
 
491
490
 
492
491
  @overload
@@ -511,9 +510,12 @@ def chain[T](
511
510
  *iterables: Iterable[T] | AsyncIterable[T],
512
511
  threaded: bool = False,
513
512
  ) -> Iterator[T] | AsyncIterator[T]:
514
- if threaded or not isinstance(iterable, Iterable):
515
- return async_chain(iterable, *iterables, threaded=threaded)
516
- return _chain(iterable, *iterables) # type: ignore
513
+ if (not threaded and
514
+ isinstance(iterable, Iterable) and
515
+ all(isinstance(it, Iterable) for it in iterables)
516
+ ):
517
+ return _chain(iterable, *iterables) # type: ignore
518
+ return async_chain(iterable, *iterables, threaded=threaded)
517
519
 
518
520
 
519
521
  @overload
@@ -593,14 +595,17 @@ def foreach(
593
595
  ):
594
596
  """
595
597
  """
596
- if threaded or not (isinstance(iterable, Iterable) and all(isinstance(it, Iterable) for it in iterables)):
597
- return async_foreach(value, iterable, *iterables, threaded=threaded)
598
- if iterables:
599
- for args in _zip(iterable, *iterables):
600
- value(*args)
601
- else:
602
- for arg in iterable:
603
- value(arg)
598
+ if (not threaded and
599
+ isinstance(iterable, Iterable) and
600
+ all(isinstance(it, Iterable) for it in iterables)
601
+ ):
602
+ if iterables:
603
+ for args in _zip(iterable, *iterables):
604
+ value(*args)
605
+ else:
606
+ for arg in iterable:
607
+ value(arg)
608
+ return async_foreach(value, iterable, *iterables, threaded=threaded)
604
609
 
605
610
 
606
611
  async def async_foreach(
@@ -1124,7 +1129,7 @@ def context[T](
1124
1129
  add_arg(enter(ctx))
1125
1130
  ret = func(*args)
1126
1131
  if isawaitable(ret):
1127
- ret = await ret
1132
+ ret = await cast(Awaitable, ret)
1128
1133
  return ret
1129
1134
  return call()
1130
1135
  else:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-iterutils
3
- Version: 0.2.5.2
3
+ Version: 0.2.5.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,7 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ iterutils/__init__.py,sha256=RdMUsfkgNEg9_5Lf4YNfCP8oB_PXJSdd_EHxeitP0Bk,33740
3
+ iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_iterutils-0.2.5.4.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_iterutils-0.2.5.4.dist-info/METADATA,sha256=fpaQOW9phx56FoA1NE19KlnqaJgYBrwhQGxeZHfxazs,1429
6
+ python_iterutils-0.2.5.4.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
7
+ python_iterutils-0.2.5.4.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- iterutils/__init__.py,sha256=PCXCgTYvEOJ5GCXeZEILDFYfCk9O5elECgTLJXWYpxw,33637
3
- iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_iterutils-0.2.5.2.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_iterutils-0.2.5.2.dist-info/METADATA,sha256=JM7iFXIRu01IwlLEMQsJq6NZeW56MNPHH3kl8WseTas,1429
6
- python_iterutils-0.2.5.2.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
7
- python_iterutils-0.2.5.2.dist-info/RECORD,,