Skip to content

Utility classes and functions for working with async iterators in Python

License

Notifications You must be signed in to change notification settings

fenhl/more_aitertools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This Python package is intended as an add-on to aioitertools, filling some gaps in its API. The aim of the project is to eventually provide async versions of all builtins and everything in more_itertools. It currently provides:

  • The class Queue which is both a subclass of asyncio.Queue and an async iterator.
  • The collate async iterator, which is an async variant of heapq.merge (aka more_itertools.collate). Currently does not support reverse but does support key.
  • The consume coroutine, which is an async variant of more_itertools.consume. It takes an iterable (sync or async) and an optional number n (defaults to infinity), and advances it by n steps, dropping the yielded values. If the iterable has fewer than n items remaining, it is consumed entirely, no exception is raised.
  • The merge async iterator, which takes an iterable of iterables, and yields items from them in the order they arrive. See the example. Both the outer iterable and the inner iterables may be sync or async.
  • The never async iterator, which never yields or returns.
  • The tuple coroutine, which takes an iterable and collects it into a tuple, similar to aioitertools.list and aioitertools.set.
  • The unique_justseen async iterator, which is an async variant of more_itertools.unique_justseen. It takes an iterable (sync or async) and an optional function key (defaults to identity), and yields only those items from the iterable where the result of key is different from that of the previously yielded item.
  • The wait async iterator, which takes an iterable (sync or async) of futures, and yields results from the front of the iterable as soon as they become available. Using this is similar to calling asyncio.wait in return_when=ALL_COMPLETED mode and taking the first return value, except it can handle an empty iterable, .result() does not have to be called, futures near the start can be used before later futures become available, and futures yielded from the iterable are started before continuing the iteration.

Python 3.7 is required. For a version compatible with Python 3.6, see the py36 branch.

Example

>>> import aitertools
>>> import asyncio
>>> import more_aitertools
>>> async def slow_count(seconds):
...     async for i in aitertools.count():
...         yield i
...         await asyncio.sleep(seconds)
... 
>>> async def slow_counts(limit):
...     for seconds in range(1, limit + 1):
...         yield slow_count(seconds)
... 
>>> sync(aitertools.alist(aitertools.islice(more_aitertools.merge(slow_counts(3)), 10)))
[0, 0, 0, 1, 1, 2, 3, 1, 2, 4]

About

Utility classes and functions for working with async iterators in Python

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages