[CentralOH] Generator and DRY

Mark Erbaugh mark at microenh.com
Tue Jul 22 23:37:53 CEST 2008


Here are part of method I wrote that provides a generator:

    def read(self, id, size=None):        
        i = gen_data.next()
        try:
            while True:    
                while len(buffer) >= read_size:
                    yield(''.join(buffer[:read_size]))
                    del buffer[:read_size]
                i = gen_data.next()
        except StopIteration:
            pass
        while len(buffer) >= read_size:
            yield(''.join(buffer[:read_size]))
            del buffer[:read_size]
        if len(buffer):
            yield(''.join(buffer))


The purpose of this code is to take data from another generator
(gen_data) and deliver it in chunks of the specified size (except the
last).  Code not shown handles possible decompression (zlib) of the
data.

The interesting thing is that there is some duplicate code:

                while len(buffer) >= read_size:
                    yield(''.join(buffer[:read_size]))
                    del buffer[:read_size]

I couldn't come up with any way to rewrite the loop to eliminate the
duplicate sections. Actually, I could by buffering the data, but I don't
want to do that. The chunks could be quite large.

If this were "normal" code, I could write an embedded function and just
call function that in both places.

However, since the embedded function contains a yield() call, it now
becomes a generator itself and the re-factored code doesn't do work
properly.

Is there a way to create an embedded function where the yield() call is
still tied to the parent function/method?

Thanks,
Mark




More information about the CentralOH mailing list