一个帮助您避免意大利面代码的库

macaroni的Python项目详细描述


py通心粉

这是一个图书馆,可以帮助你避免 Spaghetti Code。它帮助你 以可测试的方式表达应用程序逻辑。这次解放的全部想法 基于函数式编程和rust的一些概念,主要是 从铁锈中“解开”概念。

为什么

在为应用程序创建业务逻辑时,有时很难 以易于理解的方式构造复杂逻辑。为了帮助你,这个 库已创建。主要目的是帮助您构建 以清晰易读的方式应用程序,使 代码。

例如,如果您有一个crud应用程序,一些逻辑将在 在本例中,create和update函数lib可以通过链接 实现主业务规则所需的功能,并使 需要时重用函数。

importmacaroniasmciclassBussinessLogic(mci.Macaroni):defvalidate_request(self,data):# ...defcheck_user_permissions(self,data):# ...deflog_operation(self,data):# ...defcreate_item(self,data):# ...defupdate_item(self,data):# ...@mci.errordefhandle_error(self,data,error):# ...@mci.exception(DuplicateKey)defhandle_duplicate_item(self,data,exception):# ...defcreate_item(data):bl=BussinessLogic(data)result=(bl.validate_request().check_user_permissions().create_item().log_operation().handle_error().handle_duplicate_item().unwrap())returnresultdefupdate_item(data):bl=BussinessLogic(data)result=(bl.validate_request().check_user_permissions().update_item().log_operation().handle_error().unwrap())returnresult

它还可以帮助您避免处理每个部分的错误和异常处理 您的应用程序逻辑,只允许您在 应用程序执行,自动跳过应用程序逻辑中的步骤 当发生错误或异常时。

安装

pip install macaroni

用法

更多示例可以在./examples目录中找到。

importmacaroniasmciclassCalculator(mci.Macaroni):defincrement(self,value,increment=None):ifincrementisNone:increment=1returnvalue+incrementdefdivide(self,value,denominator):ifdenominator==0:returnmci.Error('invalid denominator {0}'.format(denominator))returnvalue/denominatordefinvalid(self,value):raiseValueError('invalid data')base_val=10calc=Calcultator(base_val)result=calc \
    .increment(2) \
    .divide(2) \
    .unwrap()# this returns the final resultassertresult==6

错误处理:

calc=Calculator(10)result=calc \
    .increment(2)# this is executed \.divide(-3)# this causes an error \.increment()# this is not executed \.unwrap()# returns the errorassertbool(calc.error)==Trueassertresult=='invalid denominator 0'

异常处理:

calc=Calculator(10)try:calc \
        .increment(2)# this is executed \.invalid()# this causes an exception \.increment()# this is not executed \.unwrap()# the exception ValueError is raised on unwrapexceptValueError:passassertbool(calc.exception)==True

测试时,可以使用参数magic=False禁用自动值传递、链接和错误/异常处理:

deftest_increment():calc=Calculator(magic=False)result=calc.increment(10,2)assertresult==12deftest_error():calc=Calculator(magic=False)result=calc.divide(10,0)assertcalc.errorisnotNoneassertresult='invalid denominator 0'deftest_exception()calc=Calculator(magic=False)raised_error=Falsetry:calc.invalid(10)exceptValueError:raised_error=Trueassertraised_error

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

推荐PyPI第三方库


热门话题
junit cucumber为什么会找到“runTest.java”来运行测试?   在Eclipse中找不到java KeyPairGenerator   java NotSerializableException即使在实现Serializable之后   noclassdeffounderror(java字符串连接)为什么会出现这种异常?   java Guice:将接口绑定到由动态代理创建的实例   使用Spring数据neo4j创建空间索引时发生java错误   java对于需要在50多个excel文件上运行并且每个文件平均包含25k行的项目,最佳的方法是什么   javaNIO中的java缓冲区写入/发送消息问题   如何在Java/eclipse中添加不调用super()的警告   JavaSpring:mvcUrl映射错误的id   java应该在getInstance或构造函数中使用Init方法吗?   安卓中的java空指针异常错误   java Jsoup不能完全获取原始html代码