Skip to content

Instantly share code, notes, and snippets.

@komuw
Created March 22, 2019 15:59
Show Gist options
  • Save komuw/bea09b4de273424e2c4acb600d49d346 to your computer and use it in GitHub Desktop.
Save komuw/bea09b4de273424e2c4acb600d49d346 to your computer and use it in GitHub Desktop.
convert list to iterable
import asyncio
import collections
class Iterable:
def __init__(self, my_list):
self.pool = collections.deque(my_list, maxlen=None)
self.len = len(self.pool)
self.index = 0
def __aiter__(self):
return self
async def __anext__(self):
self.index += 1
if self.index > self.len:
raise StopAsyncIteration
return self.pool.popleft()
async def cool():
async for i in Iterable(my_list=["AA", 1, 2, 3, 5, "ZZ"]):
print(i)
asyncio.run(cool())
@komuw
Copy link
Author

komuw commented Mar 22, 2019

python hello.py

AA
1
2
3
5
ZZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment