Last active
October 29, 2015 15:38
-
-
Save soaxelbrooke/a062d2a36a1092a3e258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GzipBuffer(object): | |
def __init__(self): | |
self.len = 0 | |
self.buffer = io.BytesIO() | |
self.writer = gzip.GzipFile(fileobj=self.buffer, mode='wb') | |
def append(self, thing): | |
self.len += 1 | |
self.writer.write(thing) | |
def flush(self): | |
self.writer.close() | |
self.buffer.seek(0, 0) | |
results = self.buffer.read() | |
self.buffer.seek(0, 0) | |
self.buffer.truncate() | |
self.writer = gzip.GzipFile(fileobj=self.buffer, mode='wb') | |
self.len = 0 | |
return results | |
def __len__(self): | |
return self.len | |
buffer = GzipBuffer() | |
for row in data: | |
buffer.append(row) | |
with open('fakepath', 'wb') as outfile: | |
outfile.write(buffer.flush()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment