帮助您创建和管理自己的可回滚事务。

pysyphe的Python项目详细描述


https://circleci.com/gh/SewanDevs/pysyphe.svg?style=svg

帮助您创建和管理自己的可回滚事务。

安装

$ 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”

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java结合两个Date实例使用Joda创建datetime   带有POI和FileOutputStream的java Swing应用程序性能问题   Java中是否存在类似于Python的异常?   Java中有界通配符的泛型   使用字符和子字符串的java不同输出   在Java中获取LDAP模式   java自定义类在自身上生成ClassNotFoundException   java连接到从internet到通过LAN连接到internet的系统的serversocket   java如何通过maven在jaxws中使用jaxb插件?   java Kerberos如何获取主体或客户端名称?   java应用服务器中是否有JDBC的单点登录解决方案?   java是存储矩阵值以便以后访问的更好方法   Java等级计算器。开始这门课我需要一些帮助   未找到Android“libc++\u shared.so”中的java OpenCV实现   当我使用web配置部署描述符时使用java。xml。它显示了错误   java为什么我的ArrayList的add方法冲突,如何编辑我的自定义add方法   java创建一个以集合为键的映射?   java如何将用户输入到数字三角形中   为什么Java在读取LinkedHashMap时会自动从字符串转换为整数   类动态创建和读取java类