Implementing async enumerate
Hello, Python 3.10 comes with the built-in *anext()* but I think we may be missing some other equivalent built-in functions like *list* and *enumerate*. I didn't discuss the idea of creating a PEP for this but I would like to get some help with the implementation. The equivalent Python code would be *async def aenumerate(sequence, start=0): n = start async for elem in sequence: yield n, elem n += 1* The code for enumerate if here https://github.com/python/cpython/blob/2f3a87856c7033227577b9ed0c77ed7531143... The *next* function creates a tuple and return with (n, elem) *result = PyTuple_New(2);PyTuple_SET_ITEM(result, 0, next_index);PyTuple_SET_ITEM(result, 1, next_item);return result;* For the async implementation I would have to instead return an *async_generator_asend* object and that would resolve to the tuple. Calling *tp_as_async->am_anext* and getting the value doesn't seem like the solution because I should not get the result at this stage yet. I would like to have a hint on how to implement this =D
Hi, I suggest to put such function in a Python module and publish it on PyPI. I'm sure that the module will quickly grow with more async flavor of existing functions. Victor On Tue, May 25, 2021 at 4:22 AM Filipe Alves Caixeta <filipecaixeta@gmail.com> wrote:
Hello,
Python 3.10 comes with the built-in anext() but I think we may be missing some other equivalent built-in functions like list and enumerate. I didn't discuss the idea of creating a PEP for this but I would like to get some help with the implementation.
The equivalent Python code would be
async def aenumerate(sequence, start=0): n = start async for elem in sequence: yield n, elem n += 1
The code for enumerate if here https://github.com/python/cpython/blob/2f3a87856c7033227577b9ed0c77ed7531143...
The next function creates a tuple and return with (n, elem)
result = PyTuple_New(2); PyTuple_SET_ITEM(result, 0, next_index); PyTuple_SET_ITEM(result, 1, next_item); return result;
For the async implementation I would have to instead return an async_generator_asend object and that would resolve to the tuple. Calling tp_as_async->am_anext and getting the value doesn't seem like the solution because I should not get the result at this stage yet.
I would like to have a hint on how to implement this =D
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/7ZRSZAOM... Code of Conduct: http://python.org/psf/codeofconduct/
-- Night gathers, and now my watch begins. It shall not end until my death.
https://aiostream.readthedocs.io/en/stable/operators.html#aiostream.stream.e... Is a nice version of an async enumerate. It also handles aclosing
participants (3)
-
Filipe Alves Caixeta
-
Thomas Grainger
-
Victor Stinner