帮助您创建和管理自己的可回滚事务。
pysyphe的Python项目详细描述
帮助您创建和管理自己的可回滚事务。
安装
$ pip install pysyphe
测试
测试应该在Python2.7和Python3.6下运行,以测试所有内容
$ pip install tox $ tox -e py27,py36
覆盖率报告将是PY27和PY36覆盖率的合并。
功能
可回滚操作
创建操作并将其链接到管道中:
>>>frompysyphe.actionsimportActionsPipeline,Action>>>defhello_world():...print("Hello world!")...>>>defim_alive():...print("I'm Alive!!!")...>>>action1=Action(hello_world)>>>action1.do()Helloworld!>>>action2=Action(im_alive)>>>pipeline=ActionsPipeline([action1,action2])>>>pipeline.do()Helloworld!I'm Alive!!!
为您的操作创建回滚:
>>>defhello_world():...print("Hello world!")...>>>defgoodbye_world():...print("Goodbye world!")...>>>action=Action(hello_world,goodbye_world)>>>action.do()Helloworld!>>>action.undo()Goodbyeworld!
回滚管道:
>>>defhello_world():...print("Hello world!")...>>>defgoodbye_world():...print("Goodbye world!")...>>>defim_alive():...print("I'm Alive!!!")...>>>defim_dead():...print("I'm Dead!!!")...>>>pipeline=ActionsPipeline([...Action(im_alive,im_dead),...Action(hello_world,goodbye_world)])>>>pipeline.undo()# Nothing to rollback>>>pipeline.do()I'm Alive!!!Helloworld!>>>pipeline.undo()# Will be done in reverse order.Goodbyeworld!I'm Dead!!!
仅回滚已执行的操作:
>>>defhello_world():...print("Hello world!")...>>>defgoodbye_world():...print("Goodbye world!")...>>>defim_alive():...print("I'm Alive!!!")...>>>defim_dead():...print("I'm Dead!!!")...>>>deffailure():...raiseException("I broke your pipeline, what are you gonna do?")...>>>pipeline=ActionsPipeline([...Action(hello_world,goodbye_world),...Action(failure,lambda:None),# rollback will be an empty function...Action(im_alive,im_dead)])>>>try:...pipeline.do()...exceptException:...pipeline.undo()...Helloworld!Goodbyeworld!
使用状态定义操作:
>>>frompysyphe.actionsimportstatefull_action>>>@statefull_action(["name"])...defhello(state):...print("Hello {}".format(state["name"]))...state["name"]="Dear "+state["name"]...>>>@hello.rollback_action()...defgoodbye(state):...print("Goodbye {}".format(state["name"]))...>>>action=hello.get_prepared_action(name="reader")# It must be prepared for state to be inialised>>>action.do()Helloreader>>>action.undo()GoodbyeDearreader>>>action.do()HelloDearreader>>>action.do()HelloDearDearreader
具有状态的链式操作:
>>>@statefull_action(["name"])...defhello(state):...print("Hello {}".format(state["name"]))...state["name"]="Dear "+state["name"]...>>>@hello.rollback_action()...defgoodbye(state):...print("Goodbye {}".format(state["name"]))...>>>action=hello.get_prepared_action(name="reader")>>>action2=hello.get_prepared_action(name=action.state.ref_to("name"))# We can access the state of a previous action but read only !>>>action3=hello.get_prepared_action(name=action2.state.ref_to("name"))>>>pipeline=ActionsPipeline([action,action2,action3)])>>>pipeline.do()HelloreaderHelloDearreaderHelloDearDearreader>>>pipeline.undo()GoodbyeDearDearDearreaderGoodbyeDearDearreaderGoodbyeDearreader>>>pipeline.do()HelloDearreaderHelloDearDearreaderHelloDearDearDearreader>>>pipeline.undo()GoodbyeDearDearDearDearreaderGoodbyeDearDearDearreaderGoodbyeDearDearreader
事务
创建事务处理程序并管理它们:
>>>frompysyphe.transactionsimportTransactionHandler,TransactionsManager>>>classLoggingTransactionHandler(TransactionHandler):...def__init__(self,name,will_fail):...self.name=name...self.will_fail=will_fail...defbegin(self):...print("BEGIN {}!".format(self.name))...defexecute(self):...ifself.will_fail:...raiseException("Your transaction failed, what are you gonna do?")...defcommit(self):...print("COMMIT {}!".format(self.name))...defrollback(self):...print("ROLLBACK {}!".format(self.name))...>>>tran_success=LoggingTransactionHandler("first",will_fail=False)>>>tran_fail=LoggingTransactionHandler("second",will_fail=True)>>>manager=TransactionsManager()>>>manager.add_transaction_handler(tran_success)>>>withmanager.begin():...manager.execute()...manager.commit()...BEGINfirst!COMMITfirst!>>>manager=TransactionsManager()>>>manager.add_transaction_handler(tran_success)>>>manager.add_transaction_handler(tran_fail)>>>withmanager.begin():# The transaction manager will rollback all transactions if an exception occurs....manager.execute()...manager.commit()...BEGINfirst!BEGINsecond!ROLLBACKfirst!ROLLBACKsecond!Traceback(mostrecentcalllast):File"<stdin>",line-,in<module>File".../pysyphe/transactions.py",line-,inexecutetransaction_handler.execute()File"<stdin>",line-,inexecuteException:Yourtransactionfailed,whatareyougonnado?
待办事项
- 生成文档
- 在文档中添加“如何正确编写单元操作以充分利用pysyphe”