Skip to content

Instantly share code, notes, and snippets.

@kfatehi
Last active January 31, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfatehi/90e27415e15eaeb19f7b to your computer and use it in GitHub Desktop.
Save kfatehi/90e27415e15eaeb19f7b to your computer and use it in GitHub Desktop.
a partial python shelve implementation backed by postgres
import UserDict
try:
import cPickle as pickle
except ImportError:
import pickle
class PgShelve(UserDict.DictMixin):
def __init__(self, conn):
self.conn = conn
self.dict = self.fetch()
def fetch(self):
dict = {}
cur = self.conn.cursor()
cur.execute("SELECT URL,STATE FROM PAGES")
for row in cur.fetchall():
dict[row[0]] = pickle.loads(str(row[1]))
cur.close()
return dict
def keyExists(self, key, cur):
cur.execute("SELECT COUNT(*) FROM PAGES WHERE URL = %s", (key,))
return cur.fetchone()[0] == 1
def __setitem__(self, key, item):
self.dict[key] = item
cur = self.conn.cursor()
val = pickle.dumps(self.dict[key])
if self.keyExists(key, cur):
cur.execute("UPDATE PAGES SET STATE = %s WHERE URL = %s", (val, key,))
else:
cur.execute("INSERT INTO PAGES (URL, STATE) VALUES (%s, %s)", (key, val,))
self.conn.commit()
cur.close()
def __getitem__(self, key):
return self.dict[key]
def keys(self):
return self.dict.keys()
def sync(self):
pass
if __name__ == "__main__":
import psycopg2
conn = psycopg2.connect(open('db.conf').read())
ps = PgShelve(conn)
ps["foo"] = (True, 2)
assert "foo" in ps
ps.sync()
ps = PgShelve(conn)
assert "foo" in ps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment