class myStore:
# The store saves to self.fn, and drops a copy in self.fn+version. This is the version
# control. So always load from the current version using the default filename. It also
# contains the current version number so no need for filename antics. All results are
# kept in the local `res` directory so store functions ultimately load and save the `res`
# dict.
# Use as,
# res = myStore() # instantiate, get latest version
# r = res.res # shorthand. Refer to r[] as result dict.
# r['testValue'] = 1 # set dict value/
# res.update() # Writes to disk and updates version.
# del(r); del(res)
# res = myStore() # instantiate, get latest version and data.
# r = res.res
# r['testValue'] # works.
import os, pickle, cPickle
def restore(self):
"""Opens the store file, pulls everything from it. Run this at instanciation only."""
with file(self.root + self.filename, 'r') as a:
self.res = self.cPickle.load(a)
self.version = self.cPickle.load(a)
print 'Loaded data from store v%i' % (self.version)
def __init__(self, filename = ''):
self.root = '/home/starnesm/projects/FT/ipython/'
# if a filename given, use it to name the store. Otherwise, use default.
if filename == '':
self.filename = 'carAndBikeImpedanceMeasurementsData.pickle'
else:
self.filename = filename
self.res = dict() # default values
self.version = 1 # to be replaced if store exists:
if (self.filename) in self.os.listdir(self.root):
# the store exists. Load it and get version number.
with file(self.root + self.filename, 'r') as a:
self.res = self.pickle.load(a)
self.version = self.pickle.load(a)
print 'myStore is at version ', self.version
def update(self):
"""Opens the store file, pickles the thing and puts it in it."""
self.version += 1
with file(self.root + self.filename, 'w') as a:
self.pickle.dump(self.res, a)
self.pickle.dump(self.version, a)
with file(self.root + self.filename + '.' + str(self.version), 'w') as a:
self.pickle.dump(self.res, a)
self.pickle.dump(self.version, a)
print 'Version updated to v', self.version
No comments:
Post a Comment