跳转到主要内容

基于生成器的异步迭代操作符

项目描述

基于生成器的异步迭代操作符

摘要

aiostream 提供了一组流操作符,可以组合起来创建异步操作流水线。

它可以被看作是 itertools 的异步版本,尽管某些方面略有不同。本质上,所有提供的操作符都返回一个统一的接口,称为流。流是一个增强的异步可迭代对象,提供以下功能

  • 操作符管道化 - 使用管道符号 |

  • 可重复性 - 每次迭代创建一个不同的迭代器

  • 安全迭代上下文 - 使用 async withstream 方法

  • 简化执行 - 使用 await 从流中获取最后一个元素

  • 切片和索引 - 使用方括号 []

  • 连接 - 使用加号 +

流操作符

流操作符 分为7个类别

创建

iteratepreservejustcallemptythrowneverrepeatcountrange

转换

mapenumeratestarmapcyclechunks

选择

taketakelastskipskiplastgetitemfilteruntiltakewhiledropwhile

组合

mapzipmergechainziplatest

聚合

accumulatereducelist

高级

concatflattenswitchconcatmapflatmapswitchmap

计时

spaceouttimeoutdelay

杂项

actionprint

演示

以下示例演示了大多数流的功能

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()

更多示例请参阅文档的示例部分

安装

您可以从PyPI作为aiostream软件包安装aiostream。

联系

Vincent Michel: vxgmichel@gmail.com

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

aiostream-0.6.2.tar.gz (67.8 kB 查看哈希值)

上传时间

构建分布

aiostream-0.6.2-py3-none-any.whl (53.8 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面