Skip to content

Commit

Permalink
update simplePickle for basic python 2 & 3 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
bcdaniels committed Apr 14, 2022
1 parent 6041dbb commit 2b234aa
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions SirIsaac/simplePickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@
import io, sys
import pickle

# include some old name variants for back-compatability
#import fittingProblem
#sys.modules['FittingProblem'] = fittingProblem
if sys.version_info.major > 2:
# Python 3
import pickle
else:
# Python 2
import cPickle as pickle

def save(obj,filename):
fout = io.open(filename,'wb')
# Note: we currently save using backward-compatible protocol 2
pickle.dump(obj,fout,2)
fout.close()

def load(filename):
fin = io.open(filename,'rb')
obj = pickle.load(fin)
try:
obj = pickle.load(fin)
except UnicodeDecodeError:
# try using backward-compatible encoding in case
# the file was saved using python 2
fin.close()
fin = io.open(filename,'rb')
obj = pickle.load(fin,encoding='bytes')
fin.close()
return obj

0 comments on commit 2b234aa

Please sign in to comment.