[CentralOH] cStringIO

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Wed Nov 6 14:07:54 CET 2013


On Wed, 6 Nov 2013 11:36:52 +0800, iynaix <iynaix at gmail.com> wrote:

> from functools import partial
> 
> f = cStringIO.StringIO(s)
> records = iter(partial(f.read, CHUNK_SIZE), '')
> for r in records:
>     print CHUNK_SIZE, repr(r)
> f.close()

Thanks! That's much better than my ugly code.

>     print CHUNK_SIZE, repr(r)

If one is going to print CHUNK_SIZE, 
one might as well do it outside the loop. 
I wanted to see the size of individual r's, 
particularly the last one which can be different 
than CHUNK_SIZE, hence the len(r) as follows. 

    print len(r), repr(r)

> 3. file is a python builtin function, as an alias for open, not a good name
> for a variable. :)

Wow, that was pretty stupid of me. Thanks for catching that. 



More information about the CentralOH mailing list