Created
September 26, 2011 12:16
-
-
Save PetrGlad/1242103 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from pv.core import PSys, Log | |
from pysqlite2 import dbapi2 as sqlite | |
import os, time | |
class Root: | |
def __init__(self): | |
self.attributes = {} | |
self.dbConn = sqlite.connect(":memory:") | |
self.createInitialSchema() | |
def createInitialSchema(self): | |
"Alternatively it can also be a separate transaction" | |
cur = self.dbConn.cursor() | |
cur.execute("create table props(name string, value string);") | |
def __getstate__(self, value): | |
value = {"attributes": self.attributes, | |
"dbState" : self.dbConn.iterdump()} | |
return value | |
def __setstate__(self, value): | |
self.attributes = value["attributes"] | |
self.dbConn = sqlite.connect(":memory:") | |
for sqlCmd in value["dbState"]: | |
self.dbConn.cursor().execute(sqlCmd) | |
class DbTx: | |
def __init__(self, name, val): | |
self.name = name | |
self.val = val | |
def __call__(self, root): | |
cur = root.dbConn.cursor() | |
cur.execute("insert into props (name, value) values (?, ?)", [self.name, self.val]) | |
root.attributes["dirty"] = True | |
def someFn(root): | |
"Simple operation" | |
root.attributes["dirty"] = False | |
def showProps(root): | |
cur = root.dbConn.cursor() | |
return cur.execute("select * from props").fetchall() | |
if __name__ == "__main__": | |
dataDir = "./data" | |
if not os.path.isdir(dataDir): | |
os.makedirs(dataDir) | |
psys = PSys(Log(dataDir), lambda : Root()) | |
psys.exe(DbTx("level", time.time())) | |
psys.exe(someFn) | |
print "props", showProps(psys.root) | |
print "attributes", psys.root.attributes | |
psys.log.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment