Last active
July 16, 2022 08:51
-
-
Save ken-itakura/2a496b5cd9facff775cf6c3f63d8ebae to your computer and use it in GitHub Desktop.
Avoid redoing long computation in a jupyter cell by pickling the result
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 os | |
# Helper functions | |
def pickle_them(pickle_file, *objs): | |
print(f"saving to {pickle_file}") | |
with open(pickle_file, 'wb') as f: | |
for obj in objs: | |
pickle.dump(obj, f) | |
def read_pickle(pickle_file): | |
if not os.path.exists(pickle_file): | |
return None | |
print(f"reading from {pickle_file}") | |
data=[] | |
with open(pickle_file, 'rb') as f: | |
while True: | |
try: | |
data.append(pickle.load(f)) | |
except EOFError: | |
break | |
if len(data) == 1: | |
return data[0] | |
else: | |
return tuple(data) | |
# | |
# In cell to apply | |
# | |
pickle_file="testpkl.pkl" | |
data = read_pickle(pickle_file) | |
if data is None: # if pickle file not exists, do computation and pickle result | |
a = 123 | |
b = "abc" | |
c = 0.12 | |
pickle_them(pickle_file, a, b, c) | |
else: | |
a, b, c = data # if pickle exists, read values from it | |
print(a, b, c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment