our team have to snippet like below:
this:
buf = StringIO.StringIO()
gzip.GzipFile(fileobj=buf, mode='wb').write(foo)
...
and this one:
buf = StringIO.StringIO()
tmp = gzip.GzipFile(fileobj=buf, mode='wb')
tmp.write(foo)
...
which one is more pythonic?
EDITED: I have try/catch to do the error handling work, but which one is more pythonic?
Either is fine. Choose the one you find to be most readable.
Personally, I'd go with the first one, the use of the extra variable doesn't add anything for me.
That said, in this context ecatmur is correct that you need to close the GZipFile
instance after writing all the data to it, as that flushes the compression data to buf
and adds the gzip CRC and size information. Without this your data is potentially incomplete!