w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Is there a generator that will turn a stream of text into a stream of lines?

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 '
' in self.remaining:
        terms = self.remaining.split('
')
        to_yield, self.remaining = terms[0], ''.join(terms[1:])
        yield to_yield
    else:
        to_yield = self.remaining + self.generate_arbitrary_string()
        while '
' 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.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.