Skip to content

Instantly share code, notes, and snippets.

@rednafi
Last active June 30, 2020 18:03
Show Gist options
  • Save rednafi/0257287315e320ae59eed11a5ace4185 to your computer and use it in GitHub Desktop.
Save rednafi/0257287315e320ae59eed11a5ace4185 to your computer and use it in GitHub Desktop.
Python's dict-like custom data structure that can store data in any SQLAlchemy supported database. Uses transaction.
"""
This is a self contained custom data structure with dict like
key-value storage capabilities.
* Can store the key-value pairs in any sqlalchemy supported db
* Employs thread safe transactional scope
* Modular, just change the session_scope to use a different db
* This example uses sqlite db for demonstration purpose
The code is inspired by Raymond Hettinger's talk `Build powerful,
new data structures with Python's abstract base classes`.
https://www.youtube.com/watch?v=S_ipdVNSFlo
MIT License
Copyright (c) 2020 Redowan Delowar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from collections.abc import MutableMapping
from contextlib import contextmanager
from operator import itemgetter
from sqlalchemy import create_engine, text
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import sessionmaker
def create_transaction_session(dburl):
# an engine, which the Session will use for connection resources
some_engine = create_engine(dburl)
# create a configured "Session" class
Session = sessionmaker(bind=some_engine)
@contextmanager
def session_scope():
"""Provide a transactional scope around a series of operations."""
session = Session()
try:
yield session
session.commit()
except OperationalError:
pass
except Exception:
session.rollback()
raise
finally:
session.close()
return session_scope
session_scope = create_transaction_session("sqlite:///foo.db")
class SQLAlechemyDict(MutableMapping):
def __init__(self, dbname, session_scope, items=None, **kwargs):
self.dbname = dbname
self.session_scope = session_scope
if items is None:
items = []
with self.session_scope() as session:
session.execute("CREATE TABLE Dict (key text, value text)")
session.execute("CREATE UNIQUE INDEX KIndx ON Dict (key)")
self.update(items, **kwargs)
def __setitem__(self, key, value):
if key in self:
del self[key]
with self.session_scope() as session:
session.execute(
text("INSERT INTO Dict VALUES (:key, :value)"),
{"key": key, "value": value},
)
def __getitem__(self, key):
with self.session_scope() as session:
r = session.execute(
text("SELECT value FROM Dict WHERE key=:key"), {"key": key}
)
row = r.fetchone()
if row is None:
raise KeyError(key)
return row[0]
def __delitem__(self, key):
if key not in self:
raise KeyError(key)
with self.session_scope() as session:
session.execute(text("DELETE FROM Dict WHERE key=:key"), {"key": key})
def __len__(self):
with self.session_scope() as session:
r = session.execute("SELECT COUNT(*) FROM Dict")
return next(r)[0]
def __iter__(self):
with self.session_scope() as session:
r = session.execute("SELECT key FROM Dict")
return map(itemgetter(0), r.fetchall())
def __repr__(self):
return (
f"{type(self).__name__}(dbname={self.dbname!r}, items={list(self.items())})"
)
def vacuum(self):
with self.session_scope() as session:
session.execute("VACUUM;")
if __name__ == "__main__":
# test the struct
sqladict = SQLAlechemyDict(
dbname="foo.db", session_scope=session_scope, items={"hello": "world"}
)
print(sqladict)
sqladict["key"] = "val"
for key in sqladict:
print(key)
# >>> SQLAlechemyDict(dbname='foo.db', items=[('hello', 'world'), ('key', 'val')])
# >>> hello
# >>> key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment