Mailing List Archive

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/2f3a87856c7033227577b9ed0c77ed75311430b7/Objects/enumobject.c#L42

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
Re: Implementing async enumerate [ In reply to ]
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/2f3a87856c7033227577b9ed0c77ed75311430b7/Objects/enumobject.c#L42
>
> 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/7ZRSZAOM5NPF3DOMPS2XNS64L7XP5SBN/
> Code of Conduct: http://python.org/psf/codeofconduct/



--
Night gathers, and now my watch begins. It shall not end until my death.
_______________________________________________
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/TZBZXWHMZDQSW7BWXBJTNFKAJDAUCZLL/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: Implementing async enumerate [ In reply to ]
https://aiostream.readthedocs.io/en/stable/operators.html#aiostream.stream.enumerate

Is a nice version of an async enumerate. It also handles aclosing
_______________________________________________
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/OOVBZZA4X2JF57K3GH3II46BYJSUWTCU/
Code of Conduct: http://python.org/psf/codeofconduct/