|
13 | 13 | 'stringfmt_names', 'PartialFormatter', 'partial_format', 'truncstr', 'utc2local', 'local2utc', 'trace', |
14 | 14 | 'modified_env', 'ContextManagers', 'shufflish', 'console_help', 'hl_md', 'type2str', 'dataclass_src', |
15 | 15 | 'Unset', 'nullable_dc', 'make_nullable', 'flexiclass', 'asdict', 'vars_pub', 'is_typeddict', 'is_namedtuple', |
16 | | - 'CachedIter', 'CachedAwaitable', 'reawaitable', 'is_async_callable', 'maybe_await', 'noopa', 'flexicache', |
17 | | - 'time_policy', 'mtime_policy', 'timed_cache'] |
18 | | - |
19 | | -# %% ../nbs/03_xtras.ipynb #ecd054a6 |
20 | | -#| export |
21 | | - |
| 16 | + 'CachedIter', 'CachedAwaitable', 'reawaitable', 'is_async_callable', 'maybe_await', 'maybe_aiter', 'mapa', |
| 17 | + 'noopa', 'flexicache', 'time_policy', 'mtime_policy', 'timed_cache'] |
22 | 18 |
|
23 | 19 | # %% ../nbs/03_xtras.ipynb #3401d507 |
24 | 20 | from .imports import * |
@@ -1033,6 +1029,21 @@ async def maybe_await(o): |
1033 | 1029 | from inspect import isawaitable |
1034 | 1030 | return await o if isawaitable(o) else o |
1035 | 1031 |
|
| 1032 | +# %% ../nbs/03_xtras.ipynb #985e3551 |
| 1033 | +def maybe_aiter(items): |
| 1034 | + "Convert `items` to async generator if needed, and return it" |
| 1035 | + if hasattr(items, '__aiter__'): return items |
| 1036 | + else: |
| 1037 | + async def f(items): |
| 1038 | + for item in items: yield item |
| 1039 | + return f(items) |
| 1040 | + |
| 1041 | + |
| 1042 | +# %% ../nbs/03_xtras.ipynb #371d5196 |
| 1043 | +async def mapa(f, items): |
| 1044 | + from asyncio import gather |
| 1045 | + return await gather(*[maybe_await(f(o)) async for o in maybe_aiter(items)]) |
| 1046 | + |
1036 | 1047 | # %% ../nbs/03_xtras.ipynb #02f9f070 |
1037 | 1048 | async def noopa(x=None, *args, **kwargs): |
1038 | 1049 | "Do nothing (async)" |
|
0 commit comments