Python:将iterable转换为流? [英] Python: Convert an iterable to a stream?

查看:132
本文介绍了Python:将iterable转换为流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含字符串的iterable,是否有一种简单的方法可以将其转换为流?我想做这样的事情:

If I've got an iterable containing strings, is there a simple way to turn it into a stream? I want to do something like this:

def make_file():
    yield "hello\n"
    yield "world\n"

output = tarfile.TarFile(…)
stream = iterable_to_stream(make_file())
output.addfile(…, stream)


推荐答案

这是我的流式迭代器 urllib3 通过iterables支持流式分块请求:

Here's my streaming iterator an experimental branch of urllib3 supporting streaming chunked request via iterables:

class IterStreamer(object):
    """
    File-like streaming iterator.
    """
    def __init__(self, generator):
        self.generator = generator
        self.iterator = iter(generator)
        self.leftover = ''

    def __len__(self):
        return self.generator.__len__()

    def __iter__(self):
        return self.iterator

    def next(self):
        return self.iterator.next()

    def read(self, size):
        data = self.leftover
        count = len(self.leftover)

        if count < size:
            try:
                while count < size:
                    chunk = self.next()
                    data += chunk
                    count += len(chunk)
            except StopIteration:
                pass

        self.leftover = data[size:]

        return data[:size]

上下文来源:
https ://github.com/shazow/urllib3/blob/filepost-stream/urllib3/filepost.py#L23

相关单元测试:
https://github.com/shazow/urllib3/ blob / filepost-stream / test / test_filepost.py#L9

这个代码还没有进入稳定分支,因为无大小的分块请求是支持不足,但它应该是你想要做的事情的良好基础。有关如何使用它的示例,请参阅源链接。

Alas this code hasn't made it into the stable branch yet as sizeless chunked requests are poorly supported, but it should be a good foundation for what you're trying to do. See the source link for examples showing how it can be used.

这篇关于Python:将iterable转换为流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆