SimTk/openmm — pull request #177 by swails (2013-10-18 13:44:08+00:00)
Title: Support gzip and bzip2 compression for StateDataReporter (based on filename extension)

Users were already able to pass gzip.GzipFile objects to StateDataReporter. bz2.BZ2File objects do not have 'flush', so attempts to use a BZ2File object passed to StateDataReporter resulted in [AttributeError: 'bz2.BZ2File' object has no attribute 'flush']

I don't think the flush calls are necessary for a few reasons:

1) When StateDataReporter opens files, it opens them unbuffered so the flush() calls did nothing.

2) If users want to pass their own file object, they can create whatever buffer size they want without the file being forcibly flushed. [1]

3) Not all objects that have a 'write' method implement 'flush', as the bz2.BZ2File example above shows. This also lets users implement their own 'file-like' destination (like a GUI text window) without having to implement a flush method. (I have an example using Tkinter).

[1] - I realize that it's common to pass sys.stdout to StateDataReporter. When printing directly to a terminal, stdout is unbuffered already. When redirecting stdout to a file, python buffers stdout, but you can still unbuffer stdout with the following code:

import sys, os
stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)