Skip to main content

Generator-based operators for asynchronous iteration

Project description

Generator-based operators for asynchronous iteration

Synopsis

aiostream provides a collection of stream operators that can be combined to create asynchronous pipelines of operations.

It can be seen as an asynchronous version of itertools, although some aspects are slightly different. Essentially, all the provided operators return a unified interface called a stream. A stream is an enhanced asynchronous iterable providing the following features:

  • Operator pipe-lining - using pipe symbol |

  • Repeatability - every iteration creates a different iterator

  • Safe iteration context - using async with and the stream method

  • Simplified execution - get the last element from a stream using await

  • Slicing and indexing - using square brackets []

  • Concatenation - using addition symbol +

Stream operators

The stream operators are separated in 7 categories:

creation

iterate, preserve, just, call, empty, throw, never, repeat, count, range

transformation

map, enumerate, starmap, cycle, chunks

selection

take, takelast, skip, skiplast, getitem, filter, until, takewhile, dropwhile

combination

map, zip, merge, chain, ziplatest

aggregation

accumulate, reduce, list

advanced

concat, flatten, switch, concatmap, flatmap, switchmap

timing

spaceout, timeout, delay

miscellaneous

action, print

Demonstration

The following example demonstrates most of the streams capabilities:

import asyncio
from aiostream import stream, pipe


async def main():

    # Create a counting stream with a 0.2 seconds interval
    xs = stream.count(interval=0.2)

    # Operators can be piped using '|'
    ys = xs | pipe.map(lambda x: x**2)

    # Streams can be sliced
    zs = ys[1:10:2]

    # Use a stream context for proper resource management
    async with zs.stream() as streamer:

        # Asynchronous iteration
        async for z in streamer:

            # Print 1, 9, 25, 49 and 81
            print('->', z)

    # Streams can be awaited and return the last value
    print('9² = ', await zs)

    # Streams can run several times
    print('9² = ', await zs)

    # Streams can be concatenated
    one_two_three = stream.just(1) + stream.range(2, 4)

    # Print [1, 2, 3]
    print(await stream.list(one_two_three))


# Run main coroutine
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

More examples are available in the example section of the documentation.

Installation

You can install aiostream from PyPI as the aiostream package.

Contact

Vincent Michel: vxgmichel@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiostream-0.6.2.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiostream-0.6.2-py3-none-any.whl (53.8 kB view details)

Uploaded Python 3

File details

Details for the file aiostream-0.6.2.tar.gz.

File metadata

  • Download URL: aiostream-0.6.2.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for aiostream-0.6.2.tar.gz
Algorithm Hash digest
SHA256 481e58c7f94b98f37a81384411ee39336dffb933784753b1cfa0a26f3681cc2c
MD5 5455f1eded55b411df8d1369208a1d8d
BLAKE2b-256 12ae8a1a42957f046887b88c82959676fce24de65ac9654f7090166d0198730b

See more details on using hashes here.

File details

Details for the file aiostream-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: aiostream-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 53.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for aiostream-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e771bfb0a8d6f5e04359992025315fa4cc3b908ddb02822f73bb57d6ed7e9125
MD5 811d5cb7e3f96446d6f54247b05d628e
BLAKE2b-256 d38f627f4a30d25e7bdc83125489b7d913224295e0f2fb890d71349b93b43ad6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page