Skip to content

Instantly share code, notes, and snippets.

@sparkida
Created August 28, 2019 14:51
Show Gist options
  • Save sparkida/61cd83d6ae130ae9139ca18023bc042a to your computer and use it in GitHub Desktop.
Save sparkida/61cd83d6ae130ae9139ca18023bc042a to your computer and use it in GitHub Desktop.
Pickle Gzip helper for Jupyter Notebook
"""
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