如何将可迭代对象转换为流? [英] How to convert an iterable to a stream?
问题描述
如果我有一个包含字符串的可迭代对象,是否有一种简单的方法可以将其转换为流?我想做这样的事情:
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 支持通过可迭代的流式分块请求:
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
Source with context: 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.
这篇关于如何将可迭代对象转换为流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!