python-iterutils 0.1.4__py3-none-any.whl → 0.1.5__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
@@ -2,7 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 1, 4)
5
+ __version__ = (0, 1, 5)
6
6
  __all__ = [
7
7
  "Return", "Yield", "YieldFrom", "iterable", "async_iterable", "foreach", "async_foreach",
8
8
  "through", "async_through", "flatten", "async_flatten", "map", "filter", "reduce", "zip",
@@ -15,10 +15,10 @@ from asyncio import create_task, sleep as async_sleep, to_thread
15
15
  from builtins import map as _map, filter as _filter, zip as _zip
16
16
  from collections import deque
17
17
  from collections.abc import (
18
- AsyncIterable, AsyncIterator, Awaitable, Buffer, Callable, Generator,
18
+ AsyncIterable, AsyncIterator, Awaitable, Buffer, Callable, Coroutine, Generator,
19
19
  Iterable, Iterator, MutableSet, Sequence,
20
20
  )
21
- from contextlib import asynccontextmanager, contextmanager
21
+ from contextlib import asynccontextmanager, contextmanager, ExitStack, AsyncExitStack
22
22
  from dataclasses import dataclass
23
23
  from itertools import batched, pairwise
24
24
  from inspect import isawaitable, iscoroutinefunction
@@ -769,6 +769,48 @@ def with_iter_next[T](
769
769
  pass
770
770
 
771
771
 
772
+ @overload
773
+ def context[T](
774
+ func: Callable[..., T],
775
+ *ctxs: ContextManager,
776
+ async_: Literal[False] = False,
777
+ ) -> T:
778
+ ...
779
+ @overload
780
+ def context[T](
781
+ func: Callable[..., T] | Callable[..., Awaitable[T]],
782
+ *ctxs: ContextManager | AsyncContextManager,
783
+ async_: Literal[True],
784
+ ) -> Coroutine[Any, Any, T]:
785
+ ...
786
+ def context[T](
787
+ func: Callable[..., T] | Callable[..., Awaitable[T]],
788
+ *ctxs: ContextManager | AsyncContextManager,
789
+ async_: Literal[False, True] = False,
790
+ ) -> T | Coroutine[Any, Any, T]:
791
+ if async_:
792
+ async def call():
793
+ args: list = []
794
+ add_arg = args.append
795
+ with ExitStack() as stack:
796
+ async with AsyncExitStack() as async_stack:
797
+ enter = stack.enter_context
798
+ async_enter = async_stack.enter_async_context
799
+ for ctx in ctxs:
800
+ if isinstance(ctx, AsyncContextManager):
801
+ add_arg(await async_enter(ctx))
802
+ else:
803
+ add_arg(enter(ctx))
804
+ ret = func(*args)
805
+ if isawaitable(ret):
806
+ ret = await ret
807
+ return ret
808
+ return call()
809
+ else:
810
+ with ExitStack() as stack:
811
+ return func(*map(stack.enter_context, ctxs)) # type: ignore
812
+
813
+
772
814
  @overload
773
815
  def backgroud_loop(
774
816
  call: None | Callable = None,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-iterutils
3
- Version: 0.1.4
3
+ Version: 0.1.5
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=VRL_byJ8rSDBFDYPZFnxmG7AzDJ7EXC_i1AjYi_9RZM,26174
3
+ iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_iterutils-0.1.5.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_iterutils-0.1.5.dist-info/METADATA,sha256=tLN1tAcz_WipgczszAbxMcKFImyyiBehvVnjwY_8_sk,1426
6
+ python_iterutils-0.1.5.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
7
+ python_iterutils-0.1.5.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- iterutils/__init__.py,sha256=aYU53GoLQ7CYy-rcts8b47yo6dp0lJncxs5UPRHb_YA,24752
3
- iterutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_iterutils-0.1.4.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_iterutils-0.1.4.dist-info/METADATA,sha256=1siMQz1thjXxHUIQpTkxst6IY2Y3kfBRdeLeQIWRFOA,1426
6
- python_iterutils-0.1.4.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
7
- python_iterutils-0.1.4.dist-info/RECORD,,