# if there's a need to do more gzip, put that to a function
string = StringIO()
gzip = GzipFile(fileobj=string, mode='w')
data = str(data) # the gzip module can't handle unicode objects
gzip.write(data)
gzip.close()
data = string.getvalue()