Wrap the generator in a buffer?

I have a python generator that gives parts of a file (wsgi app_iter), and I need to pass it to an interface that expects it to have classical methods readand readlines(I want to pass this as wsgi.inputanother Request).

Is it possible to do this so as not to materialize all the content of the generator into memory? The idea is to wrap the generator in something that has readboth readline(for example, BytesIOor StringIO) and do it in a lazy way.

+3
source share
1 answer

, , . - , :

class ReadWrapper:
    def __init__(self, app_iter):
        self.iterator = iter(app_iter)
        self.buffer = ''
    def readline(self):
        while '\n' not in self.buffer:
            try:
                self.buffer += next(self.iterator)
            except StopIteration:
                result = self.buffer
                self.buffer = ''
                return result
        idx = self.buffer.find('\n')
        result = self.buffer[:idx+1]
        self.buffer = self.buffer[idx+1:]
        return result

read() , , \n ( , ).

, self.buffer: \n .

+3

All Articles