Last active
February 22, 2019 21:23
-
-
Save thoo/1a9ad3821e5e9ff3840c1cc6cbb44182 to your computer and use it in GitHub Desktop.
#save_object #pickle
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
# 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