I really love dataset library but it lacks a nice wrapper for with statement which will automatically close DB connection.
So I have to create my own wrapper and keep it as a submodule in my projects. It would be nice to have something like this built in the dataset future versions.
import dataset
class DB(object):
"""
small wrapper around dataset which
supports 'with' statements
Example:
with DB() as db:
res = db.query("SELECT * FROM table LIMIT 10")
"""
def __init__(self, database_url):
self.database_url = database_url
def __enter__(self):
self.db = dataset.connect(self.database_url)
return self.db
def __exit__(self, exc_type, exc_val, exc_tb):
self.db.executable.close()
I really love dataset library but it lacks a nice wrapper for
withstatement which will automatically close DB connection.So I have to create my own wrapper and keep it as a submodule in my projects. It would be nice to have something like this built in the
datasetfuture versions.