Skip to content

Commit 444a696

Browse files
committed
fixes #803
1 parent cdab0df commit 444a696

3 files changed

Lines changed: 50 additions & 27 deletions

File tree

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@
852852
'fastcore.xtras.stringfmt_names': ('xtras.html#stringfmt_names', 'fastcore/xtras.py'),
853853
'fastcore.xtras.time_policy': ('xtras.html#time_policy', 'fastcore/xtras.py'),
854854
'fastcore.xtras.timed_cache': ('xtras.html#timed_cache', 'fastcore/xtras.py'),
855+
'fastcore.xtras.to_aiter': ('xtras.html#to_aiter', 'fastcore/xtras.py'),
855856
'fastcore.xtras.trace': ('xtras.html#trace', 'fastcore/xtras.py'),
856857
'fastcore.xtras.trim_wraps': ('xtras.html#trim_wraps', 'fastcore/xtras.py'),
857858
'fastcore.xtras.truncstr': ('xtras.html#truncstr', 'fastcore/xtras.py'),

fastcore/xtras.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
'stringfmt_names', 'PartialFormatter', 'partial_format', 'truncstr', 'utc2local', 'local2utc', 'trace',
1414
'modified_env', 'ContextManagers', 'shufflish', 'console_help', 'hl_md', 'type2str', 'dataclass_src',
1515
'Unset', 'nullable_dc', 'make_nullable', 'flexiclass', 'asdict', 'vars_pub', 'is_typeddict', 'is_namedtuple',
16-
'CachedIter', 'CachedAwaitable', 'reawaitable', 'is_async_callable', 'maybe_await', 'maybe_aiter', 'mapa',
17-
'noopa', 'flexicache', 'time_policy', 'mtime_policy', 'timed_cache']
16+
'CachedIter', 'CachedAwaitable', 'reawaitable', 'is_async_callable', 'maybe_await', 'to_aiter',
17+
'maybe_aiter', 'mapa', 'noopa', 'flexicache', 'time_policy', 'mtime_policy', 'timed_cache']
1818

1919
# %% ../nbs/03_xtras.ipynb #3401d507
2020
from .imports import *
@@ -1029,16 +1029,17 @@ async def maybe_await(o):
10291029
from inspect import isawaitable
10301030
return await o if isawaitable(o) else o
10311031

1032-
# %% ../nbs/03_xtras.ipynb #985e3551
1032+
# %% ../nbs/03_xtras.ipynb #fc70459b
1033+
async def to_aiter(items):
1034+
"Async yield each item in `items` with `asyncio.sleep(0)` between"
1035+
for item in items:
1036+
await asyncio.sleep(0)
1037+
yield item
1038+
1039+
# %% ../nbs/03_xtras.ipynb #5337427e
10331040
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:
1039-
await asyncio.sleep(0)
1040-
yield item
1041-
return f(items)
1041+
"If `items` already async, return it; otherwise to_aiter"
1042+
return items if hasattr(items, '__aiter__') else to_aiter(items)
10421043

10431044
# %% ../nbs/03_xtras.ipynb #371d5196
10441045
async def mapa(f, items):

nbs/03_xtras.ipynb

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@
23682368
{
23692369
"data": {
23702370
"text/plain": [
2371-
"['c', 'a', 'g', 'e', 'd', 'h', 'b', 'f']"
2371+
"['e', 'b', 'c', 'g', 'h', 'a', 'f', 'd']"
23722372
]
23732373
},
23742374
"execution_count": null,
@@ -2855,7 +2855,7 @@
28552855
{
28562856
"data": {
28572857
"text/plain": [
2858-
"'_oD33Ew5VRVG3N4nztl2CDg'"
2858+
"'_mutjsmXKSHeVjFdUFGSgNw'"
28592859
]
28602860
},
28612861
"execution_count": null,
@@ -2952,7 +2952,7 @@
29522952
{
29532953
"data": {
29542954
"text/plain": [
2955-
"('4d760890', '8c7d7247')"
2955+
"('215346fd', '8c7d7247')"
29562956
]
29572957
},
29582958
"execution_count": null,
@@ -3714,8 +3714,8 @@
37143714
"name": "stdout",
37153715
"output_type": "stream",
37163716
"text": [
3717-
"Num Events: 9, Freq/sec: 353.6\n",
3718-
"Most recent: ▂▇▁▆ 262.9 253.7 278.7 240.9 273.2\n"
3717+
"Num Events: 9, Freq/sec: 338.8\n",
3718+
"Most recent: ▂▇▁▆ 280.6 255.3 284.5 238.9 279.7\n"
37193719
]
37203720
}
37213721
],
@@ -4953,33 +4953,54 @@
49534953
{
49544954
"cell_type": "code",
49554955
"execution_count": null,
4956-
"id": "985e3551",
4956+
"id": "fc70459b",
4957+
"metadata": {},
4958+
"outputs": [],
4959+
"source": [
4960+
"#| export\n",
4961+
"async def to_aiter(items):\n",
4962+
" \"Async yield each item in `items` with `asyncio.sleep(0)` between\"\n",
4963+
" for item in items:\n",
4964+
" await asyncio.sleep(0)\n",
4965+
" yield item"
4966+
]
4967+
},
4968+
{
4969+
"cell_type": "code",
4970+
"execution_count": null,
4971+
"id": "879e3a1e",
4972+
"metadata": {},
4973+
"outputs": [],
4974+
"source": [
4975+
"test_eq([o async for o in to_aiter([10,20,30])], [10,20,30])\n",
4976+
"test_eq([o async for o in to_aiter([])], [])"
4977+
]
4978+
},
4979+
{
4980+
"cell_type": "code",
4981+
"execution_count": null,
4982+
"id": "5337427e",
49574983
"metadata": {},
49584984
"outputs": [],
49594985
"source": [
49604986
"#| export\n",
49614987
"def maybe_aiter(items):\n",
4962-
" \"Convert `items` to async generator if needed, and return it\"\n",
4963-
" if hasattr(items, '__aiter__'): return items\n",
4964-
" else:\n",
4965-
" async def f(items):\n",
4966-
" for item in items:\n",
4967-
" await asyncio.sleep(0)\n",
4968-
" yield item\n",
4969-
" return f(items)"
4988+
" \"If `items` already async, return it; otherwise to_aiter\"\n",
4989+
" return items if hasattr(items, '__aiter__') else to_aiter(items)"
49704990
]
49714991
},
49724992
{
49734993
"cell_type": "code",
49744994
"execution_count": null,
4975-
"id": "d8c47321",
4995+
"id": "b4d75116",
49764996
"metadata": {},
49774997
"outputs": [],
49784998
"source": [
49794999
"async def _agen():\n",
49805000
" for i in [1,2,3]: yield i\n",
49815001
"\n",
4982-
"test_eq(maybe_aiter(aiter_:=_agen()), aiter_) # already async, returned as-is\n",
5002+
"ag = _agen()\n",
5003+
"test_eq([o async for o in maybe_aiter(ag)], [1,2,3])\n",
49835004
"test_eq([o async for o in maybe_aiter([1,2,3])], [1,2,3])"
49845005
]
49855006
},

0 commit comments

Comments
 (0)