Skip to content

Instantly share code, notes, and snippets.

@thoo
Last active February 22, 2019 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoo/1a9ad3821e5e9ff3840c1cc6cbb44182 to your computer and use it in GitHub Desktop.
Save thoo/1a9ad3821e5e9ff3840c1cc6cbb44182 to your computer and use it in GitHub Desktop.
#save_object #pickle
# pickle has a bug which won't let you save more than large (GB) files
import joblib
def save_obj(obj, name, name_dir='data' ):
"""
Save to pickle.
Parameters
----------
obj : any object
This can be a dictionary or ndarray.
name : str
The name for the object to be saved.
name_dir : str, default 'data'
Name of the directory.
Returns
-------
No return.
Save the pickle object to the local file system.
"""
if not os.path.isdir(name_dir):
os.makedirs(name_dir)
data_path = os.path.join(name_dir, name+'.pkl')
with open(data_path, 'wb') as f:
joblib.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name, name_dir='data' ):
"""
Load the pickle object from the local file system.
Parameters
----------
obj : any object
This can be a dictionary or ndarray.
name : str
The name for the object to be saved.
name_dir : str, default 'data'
Name of the directory.
Returns
-------
object
Return an object such as a dictionary.
"""
data_path = os.path.join(name_dir, name+'.pkl')
with open(data_path, 'rb') as f:
return joblib.load(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment