python的monads使它变得简单和安全。
dry-monads的Python项目详细描述
干单子
使python
的单子变得简单和安全。
功能
- 提供用于编写声明性业务逻辑的原语
- 完全类型化并用
mypy
检查, 也允许您编写类型安全代码 - 没有操作员超载或其他使您眼睛流血的不符合语法的东西
安装
pip install dry-monads
里面是什么?
我们有几个最具代表性的单子:
- Result, Failure, and Success(也称为
Either
、Left
和Right
) - Maybe, Some, and Nothing
我们也关心代码的可读性和开发人员的经验, 因此,我们提供了一些有用的功能,使您的生活更轻松:
示例
fromdry_monads.do_notationimportdo_notationfromdry_monads.eitherimportResult,Success,FailureclassCreateAccountAndUser(object):"""Creates new Account-User pair."""@do_notationdef__call__(self,username:str,email:str)->Result['User',str]:"""Can return a Success(user) or Failure(str_reason)."""user_schema=self._validate_user(username,email).unwrap()account=self._create_account(user_schema).unwrap()returnself._create_user(account)# Protected methods# ...
我们在文件里。
灵感
本模块主要基于: