After reading your edit, maybe you could modify the stream object which returns arbitrary amounts of text? For example, in the stream.next()
method, there is some way the stream generates a string and yields
it when .next()
is called. Could you do something like:
def next(self):
if '\n' in self.remaining:
terms = self.remaining.split('\n')
to_yield, self.remaining = terms[0], ''.join(terms[1:])
yield to_yield
else:
to_yield = self.remaining + self.generate_arbitrary_string()
while '\n' not in to_yield:
to_yield += self.generate_arbitrary_string()
to_yield, self.remaining = terms[0], ''.join(terms[1:])
yield to_yield
This pseudocode assumes that the stream object generates some arbitrary string with generate_arbitrary_string()
. On your first call of next()
, the self.remaining
string should be empty, so you go to the else
statement. There, you concatenate arbitrary strings until you find a newline
character, split the concatenated string at the first newline
character, yield the first half and store the second half in remaining
.
On subsequent calls of next()
, you first check if self.remaining
contains any newline
characters. If so, yield the first line and store the rest. If not, append a new arbitrary string to self.remaining
and continue like above.