Code Monkey home page Code Monkey logo

aiostream's Introduction

aiostream

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 +

Requirements

The stream operators rely heavily on asynchronous generators (PEP 525):

  • python >= 3.6

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.

Contact

Vincent Michel: [email protected]

aiostream's People

Contributors

vxgmichel avatar blaiseli avatar brean avatar and-semakin avatar sillyfreak avatar timworx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.