I have a generator producing a list of strings. Is there a utility/adapter in Python that could make it look like a file?
For example,
JavaScript
x
20
20
1
>>> def str_fn():
2
for c in 'a', 'b', 'c':
3
yield c * 3
4
5
>>> for s in str_fn():
6
print s
7
8
aaa
9
bbb
10
ccc
11
>>> stream = some_magic_adaptor(str_fn())
12
>>> while True:
13
data = stream.read(4)
14
if not data:
15
break
16
print data
17
aaab
18
bbcc
19
c
20
Because data may be big and needs to be streamable (each fragment is a few kilobytes, the entire stream is tens of megabytes), I do not want to eagerly evaluate the whole generator before passing it to stream adaptor.
Advertisement
Answer
Here’s a solution that should read from your iterator in chunks.
JavaScript
1
33
33
1
class some_magic_adaptor:
2
def __init__( self, it ):
3
self.it = it
4
self.next_chunk = ""
5
def growChunk( self ):
6
self.next_chunk = self.next_chunk + self.it.next()
7
def read( self, n ):
8
if self.next_chunk == None:
9
return None
10
try:
11
while len(self.next_chunk)<n:
12
self.growChunk()
13
rv = self.next_chunk[:n]
14
self.next_chunk = self.next_chunk[n:]
15
return rv
16
except StopIteration:
17
rv = self.next_chunk
18
self.next_chunk = None
19
return rv
20
21
22
def str_fn():
23
for c in 'a', 'b', 'c':
24
yield c * 3
25
26
ff = some_magic_adaptor( str_fn() )
27
28
while True:
29
data = ff.read(4)
30
if not data:
31
break
32
print data
33