class Cache(dict):
format = """SELECT * FROM "{}" WHERE name = ?""".format
def __init__(self, table):
self.sql = self.format(table)
self.connect()
def __missing__(self, key):
try:
c = self.db.cursor()
c.execute(self.sql, (key,))
value = c.fetchone()
self[key] = value
return value
except Exception as e:
print(e, file=sys.stderr)
try:
self.connect()
except Exception as ee:
print(ee, file=sys.stderr)
def connect(self):
self.db = sqlite3.connect("db")
class PostgresCache(Cache):
format = """SELECT * FROM "{}" WHERE name = %s""".format
def connect(self):
self.db = psycopg2.connect(user='q', host='sv', database='q')