Created
September 18, 2015 17:57
-
-
Save rm-you/1450b1267d636ecb4dac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DB_ENGINE = <have your engine available here> | |
@contextmanager | |
def get_session(): | |
"""Provide a transactional scope around a series of operations.""" | |
session = sqlalchemy.orm.sessionmaker(bind=DB_ENGINE)() | |
try: | |
yield session | |
session.commit() | |
except: | |
session.rollback() | |
raise | |
finally: | |
session.close() | |
with get_session() as db_session: | |
# Do some DB stuff in you nice fancy db_session | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment