alchemytools是在任何sqlalchemy项目中使用的一组助手。
Alchemytools的Python项目详细描述
这个项目带来了一组有用的工具,可以在任何SQLAchemly项目中使用
这样做的目的是避免常见的问题,例如:打开/关闭会话、仅在事务结束时提交sessin等。
可用工具
以下是所有可用的炼金术工具。
上下文管理器
管理的
这是基本的上下文管理器,它将在with块结束时自动提交并关闭会话。
withmanaged(MySessionClass)assession:# Do what you need with your session# Here the session is already closed and commited
如果在with块内引发任何异常,会话将回滚并重新引发异常。
为了避免在with块中包含所有函数体,managed函数也作为上下文管理器。
@managed(MySessionClass)deffoo(session,*args,**kwargs):# Do what you need with your sessionpass# call as if the session didn't exist:foo(2,a='b')
每次调用函数时都会打开会话,每次返回或引发异常时都会关闭会话自动提交和回滚规则正常工作
附加选项
^{tt5}$: Sets the autoflush option on the SQLAlchemy session, defaults fo ^{tt6}$
执行测试
$ python setup.py test
withmanaged(MySessionClass)assession:# Do what you need with your session# Here the session is already closed and commited
附加选项
^{tt5}$: Sets the autoflush option on the SQLAlchemy session, defaults fo ^{tt6}$
执行测试
$ python setup.py test
$ python setup.py test