Created
August 28, 2019 14:51
-
-
Save sparkida/61cd83d6ae130ae9139ca18023bc042a to your computer and use it in GitHub Desktop.
Pickle Gzip helper for Jupyter Notebook
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
""" | |
Helper to Pickle objects with bz2 compression | |
""" | |
import bz2, pickle, os.path | |
def save_object(filename, data_object): | |
print('saving object "' + filename + '"') | |
save_file = bz2.BZ2File(filename, 'w') | |
pickle.dump(data_object, save_file) | |
save_file.close() | |
print('object saved') | |
#returns None if no file exists | |
def load_object(filename): | |
if not os.path.exists(filename): | |
return None | |
print('loading object: "' + filename + '"') | |
fh = bz2.open(filename, 'rb') | |
return pickle.load(fh, encoding='bytes') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment