[CentralOH] cStringIO

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Wed Nov 6 21:52:36 CET 2013


On Wed, 6 Nov 2013 09:01:21 -0500, Joe Shaw <joe at joeshaw.org> wrote:

> I'd probably also make use of contextlib.closing rather than managing the
> file by itself.

Thanks. That's what I was looking for, 
and tried but failed to do in the previously attached notebook file. 

> with contextlib.closing(cStringIO.StringIO(s)) as f:
>     # previous code here, no need to explicitly close() the file afterward

A complete example is in the newly attached ipython notebook file. 

> This ensures that the file is closed even in the case of error (and is,
> IMO, cleaner than a try-finally block)

Yum! Context manager goodness! 

http://docs.python.org/2.7/library/contextlib.html
http://docs.python.org/3/library/contextlib.html
http://docs.python.org/2.7/library/functools.html#functools.partial
http://docs.python.org/2.7/library/functions.html#iter



More information about the CentralOH mailing list