|
2368 | 2368 | { |
2369 | 2369 | "data": { |
2370 | 2370 | "text/plain": [ |
2371 | | - "['c', 'a', 'g', 'e', 'd', 'h', 'b', 'f']" |
| 2371 | + "['e', 'b', 'c', 'g', 'h', 'a', 'f', 'd']" |
2372 | 2372 | ] |
2373 | 2373 | }, |
2374 | 2374 | "execution_count": null, |
|
2855 | 2855 | { |
2856 | 2856 | "data": { |
2857 | 2857 | "text/plain": [ |
2858 | | - "'_oD33Ew5VRVG3N4nztl2CDg'" |
| 2858 | + "'_mutjsmXKSHeVjFdUFGSgNw'" |
2859 | 2859 | ] |
2860 | 2860 | }, |
2861 | 2861 | "execution_count": null, |
|
2952 | 2952 | { |
2953 | 2953 | "data": { |
2954 | 2954 | "text/plain": [ |
2955 | | - "('4d760890', '8c7d7247')" |
| 2955 | + "('215346fd', '8c7d7247')" |
2956 | 2956 | ] |
2957 | 2957 | }, |
2958 | 2958 | "execution_count": null, |
|
3714 | 3714 | "name": "stdout", |
3715 | 3715 | "output_type": "stream", |
3716 | 3716 | "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" |
3719 | 3719 | ] |
3720 | 3720 | } |
3721 | 3721 | ], |
|
4953 | 4953 | { |
4954 | 4954 | "cell_type": "code", |
4955 | 4955 | "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", |
4957 | 4983 | "metadata": {}, |
4958 | 4984 | "outputs": [], |
4959 | 4985 | "source": [ |
4960 | 4986 | "#| export\n", |
4961 | 4987 | "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)" |
4970 | 4990 | ] |
4971 | 4991 | }, |
4972 | 4992 | { |
4973 | 4993 | "cell_type": "code", |
4974 | 4994 | "execution_count": null, |
4975 | | - "id": "d8c47321", |
| 4995 | + "id": "b4d75116", |
4976 | 4996 | "metadata": {}, |
4977 | 4997 | "outputs": [], |
4978 | 4998 | "source": [ |
4979 | 4999 | "async def _agen():\n", |
4980 | 5000 | " for i in [1,2,3]: yield i\n", |
4981 | 5001 | "\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", |
4983 | 5004 | "test_eq([o async for o in maybe_aiter([1,2,3])], [1,2,3])" |
4984 | 5005 | ] |
4985 | 5006 | }, |
|
0 commit comments