python-iterutils 0.2.5.1__py3-none-any.whl → 0.2.5.2__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
@@ -75,24 +75,27 @@ def async_iterable(obj, /) -> bool:
75
75
  return isinstance(obj, AsyncIterable)
76
76
 
77
77
 
78
- def _get_async(back: int = 2, /) -> bool:
78
+ def _coalesce(vals, default=None):
79
+ for val in vals:
80
+ if val is not None:
81
+ return val
82
+ return default
83
+
84
+
85
+ @overload
86
+ def _get_async(back: int = 2, /, *, default: Literal[False] = False) -> bool:
87
+ ...
88
+ @overload
89
+ def _get_async[T](back: int = 2, /, *, default: T) -> bool | T:
90
+ ...
91
+ def _get_async[T](back: int = 2, /, *, default: Literal[False] | T = False) -> bool | T:
79
92
  """往上查找,从最近的调用栈的命名空间中获取 `async_` 的值
80
93
  """
81
- f: None | FrameType
82
- f = _getframe(back)
83
- f_globals = f.f_globals
84
- f_locals = f.f_locals
85
- if f_locals is f_globals:
86
- return f_locals.get("async_") or False
87
- while f_locals is not None and f_locals is not f_globals:
88
- if "async_" in f_locals:
89
- if f_locals["async_"] is not None:
90
- return f_locals["async_"]
91
- f = f.f_back
92
- if f is None:
93
- break
94
- f_locals = f.f_locals
95
- return False
94
+ def iter_frams(f: None | FrameType = _getframe(back)):
95
+ while f:
96
+ yield f.f_locals.get("async_")
97
+ f = f.f_back
98
+ return _coalesce(iter_frams(), default)
96
99
 
97
100
 
98
101
  def _run_gen_step(gen: Generator, /):
@@ -271,10 +274,14 @@ def as_gen_step[**Args](
271
274
  gen_step: Callable[Args, Generator],
272
275
  /,
273
276
  ) -> Callable[Args, Any]:
277
+ default_async = _get_async(default=None)
274
278
  def wrapper(*args: Args.args, **kwds: Args.kwargs):
275
279
  return run_gen_step(
276
280
  gen_step,
277
- cast(None | Literal[False, True], kwds.pop("async_", None)),
281
+ _coalesce((
282
+ kwds.pop("async_", None),
283
+ _get_async(default=default_async),
284
+ )),
278
285
  *args,
279
286
  **kwds,
280
287
  )
@@ -285,10 +292,14 @@ def as_gen_step_iter[**Args](
285
292
  gen_step: Callable[Args, Generator],
286
293
  /,
287
294
  ) -> Callable[Args, Iterable | AsyncIterable]:
295
+ default_async = _get_async(default=None)
288
296
  def wrapper(*args: Args.args, **kwds: Args.kwargs):
289
297
  return run_gen_step_iter(
290
298
  gen_step,
291
- cast(None | Literal[False, True], kwds.pop("async_", None)),
299
+ _coalesce((
300
+ kwds.pop("async_", None),
301
+ _get_async(default=default_async),
302
+ )),
292
303
  *args,
293
304
  **kwds,
294
305
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-iterutils
3
- Version: 0.2.5.1
3
+ Version: 0.2.5.2
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=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,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- iterutils/__init__.py,sha256=aoUmnRzgOHOAT4hzUXPJ9A3bvUepx0ueCtjWl4XPT3k,33354
3
- iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_iterutils-0.2.5.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_iterutils-0.2.5.1.dist-info/METADATA,sha256=lp7fYBJQJTjPu5_nd262n54aUP_4k0-ngr_So_iSpbs,1429
6
- python_iterutils-0.2.5.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
7
- python_iterutils-0.2.5.1.dist-info/RECORD,,