Created
April 20, 2013 00:57
-
-
Save thearn/5424244 to your computer and use it in GitHub Desktop.
Functions for saving and loading python objects using pickle with gzip compression.
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
import pickle | |
import gzip | |
def save(object, filename, protocol = 0): | |
"""Saves a compressed object to disk | |
""" | |
file = gzip.GzipFile(filename, 'wb') | |
file.write(pickle.dumps(object, protocol)) | |
file.close() | |
def load(filename): | |
"""Loads a compressed object from disk | |
""" | |
file = gzip.GzipFile(filename, 'rb') | |
buffer = "" | |
while True: | |
data = file.read() | |
if data == "": | |
break | |
buffer += data | |
object = pickle.loads(buffer) | |
file.close() | |
return object |
sonictl
commented
Dec 18, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment