I would like a class that in its initialize checks if filename
exists. If it does it should initialize itself with filename
, otherwise it should run init. At a later point I can then run a save
method, saving the entire object.
A sketch of what I want:
class data(object):
def __init__(self, filename):
if does_not_exist(filename): # create new
[... expensive computations]
self.save(filename)
else: # load existing
with open(filename,'rb') as fp:
self = pickle.load(fp)
def save(self, filename):
with open(filename,'wb') as fp:
pickle.dump(self, fp)
When loading I know that I can do something like
tmp = pickle.load(fp)
self.a = tmp.a
self.b = tmb.b
...
But I hope that there is a better way
I assume this question has been asked before, but couldn't find it :/