"""MySQL Connector. Manages interactions with MySQL. """ from contextlib import contextmanager @contextmanager def db_session(session): """Provide a transactional scope around a series of operations. Taken from http://docs.sqlalchemy.org/en/latest/orm/session_basics.html. This handles rollback and closing of session, so there is no need to do that throughout the code. Usage: with db_session() as session: session.execute(query) """ try: yield session session.commit() except: # noqa session.rollback() raise finally: session.close()