微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使迭代器适应Python中类似文件对象的行为

如何解决使迭代器适应Python中类似文件对象的行为

这是应该从迭代器中分批读取的解决方案。

class some_magic_adaptor:
  def __init__( self, it ):
    self.it = it
    self.next_chunk = ""
  def growChunk( self ):
    self.next_chunk = self.next_chunk + self.it.next()
  def read( self, n ):
    if self.next_chunk == None:
      return None
    try:
      while len(self.next_chunk)<n:
        self.growChunk()
      rv = self.next_chunk[:n]
      self.next_chunk = self.next_chunk[n:]
      return rv
    except stopiteration:
      rv = self.next_chunk
      self.next_chunk = None
      return rv


def str_fn():
  for c in 'a', 'b', 'c':
    yield c * 3

ff = some_magic_adaptor( str_fn() )

while True:
  data = ff.read(4)
  if not data:
    break
  print data

解决方法

我有一个生成器,生成一个字符串列表。Python中是否有实用程序/适配器可以使它看起来像文件?

例如,

>>> def str_fn():
...     for c in 'a','b','c':
...         yield c * 3
... 
>>> for s in str_fn():
...     print s
... 
aaa
bbb
ccc
>>> stream = some_magic_adaptor(str_fn())
>>> while True:
...    data = stream.read(4)
...    if not data:
...        break
...    print data
aaab
bbcc
c

因为数据可能很大并且需要可流式传输(每个片段只有几千字节,所以整个流都是几十兆字节),所以我不想急于评估整个生成器,然后再将其传递给流适配器。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。