Skip to content

Instantly share code, notes, and snippets.

@countvajhula
Last active December 13, 2015 19:58
Show Gist options
  • Save countvajhula/4966286 to your computer and use it in GitHub Desktop.
Save countvajhula/4966286 to your computer and use it in GitHub Desktop.
pickle data manager w attempted hooks and synchronizer
import os
import pickle
import transaction
def before_commit(args, kws):
print "operating..."
print args
for arg in args:
print arg
for k,v in kws:
print k,v
print "...done"
def after_commit(success, args, kws):
if success:
print "transaction succeeded"
else:
print "transaction failed"
class synch(object):
def beforeCompletion(self, transaction):
print "Commit started"
def afterCompletion(self, transaction):
print "Commit finished"
class PickleSavepoint(object):
def __init__(self, dm):
self.dm = dm
self.saved_committed = self.dm.uncommitted.copy()
def rollback(self):
self.dm.uncommitted = self.saved_committed.copy()
class PickleDataManager(object):
transaction_manager = transaction.manager
def __init__(self, pickle_path='Data.pk1'):
self.pickle_path = pickle_path
try:
data_file = open(self.pickle_path, 'rb')
except IOError:
data_file = None
uncommitted = {}
if data_file is not None:
try:
uncommitted = pickle.load(data_file)
except EOFError:
pass
self.uncommitted = uncommitted
self.committed = uncommitted.copy()
#
# it's going to act like a dictionary so implement basic dictionary methods
#
def __getitem__(self, name):
return self.uncommitted[name]
def __setitem__(self, name, value):
self.uncommitted[name] = value
def __delitem__(self, name):
del self.uncommitted[name]
def keys(self):
return self.uncommitted.keys()
def values(self):
return self.uncommitted.values()
def items(self):
return self.uncommitted.items()
def __repr__(self):
return self.uncommitted.__repr__() # vs repr(self.uncommitted)?
def __len__(self):
return len(self.uncommitted)
#
# implement transaction protocol methods
#
def abort(self, transaction):
self.uncommitted = self.committed.copy()
def tpc_begin(self, transaction):
pass
def commit(self, transaction):
pass
def tpc_vote(self, transaction):
devnull = open(os.devnull, 'wb')
try:
pickle.dump(self.uncommitted, devnull)
except (TypeError, pickle.PicklingError):
raise ValueError('Unpickleable value cannot be saved')
def tpc_abort(self, transaction):
self.uncommitted = self.committed.copy()
def tpc_finish(self, transaction):
data_file = open(self.pickle_path, 'wb')
try:
pickle.dump(self.uncommitted, data_file)
except:
pass # can't fail here, or we're screwed
self.committed = self.uncommitted.copy()
#self.uncommitted = {}
def savepoint(self):
return PickleSavepoint(self)
def sortKey(self):
return 'pickledm' + str(id(self))
if __name__ == '__main__':
dm = PickleDataManager()
t_curr = transaction.get()
# HOOKS AND SYNCHRONIZERS
#FIXME:this does not call the synch methods on commit()
#transaction.manager.registerSynch(synch())
#FIXME: this causes errors
#t_curr.addBeforeCommitHook(before_commit, args=(1,2), kws={'a':1})
#t_curr.addAfterCommitHook(after_commit, args=(1,2), kws={'a':1})
t_curr.join(dm)
print 'before: ' + str(dm)
if len(dm) == 0:
dm['bar'] = 'foo'
dm['baz'] = 'spam' #['s','p','a','m']
else:
swapcase = lambda v:v.islower() and v.upper() or v.lower()
for k,v in dm.items():
dm[k] = swapcase(v)
transaction.commit()
print 'after: ' + str(dm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment