-
-
Notifications
You must be signed in to change notification settings - Fork 298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataset wrapper for With statement #418
Comments
Can be better with schema: class DB(object):
def __init__(self, database_url: str = None, schema: str = None):
if not database_url:
database_url = os.environ.get("DATABASE_URL", "sqlite://")
self.database_url = database_url.replace("postgres://", "postgresql://")
self.connect_args = None
self.schema = schema
if schema:
self.connect_args = {'connect_args': {'options': '-csearch_path={}'.format(schema)}}
def __enter__(self):
self.db = dataset.connect(self.database_url, schema=self.schema, engine_kwargs=self.connect_args)
return self.db
def __exit__(self, exc_type, exc_val, exc_tb):
self.db.commit()
self.db.close() |
Unfortunately the database object can already be used to enter a context block, but it is being used to start/end transactions. Lines 155 to 169 in 5c2dc8d
In hindsight the proposed behaviour makes more sense, with a transaction context still available from But changing this would break API. @pudo would a PR with this behaviour have any chance of being merged? |
It's possible to get the same effect using just the standard library. Specifically, import contextlib
import dataset
with contextlib.closing(dataset.connect()) as db:
print(db.tables) The |
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.The text was updated successfully, but these errors were encountered: