将业务逻辑封装在命令类中。

mutations的Python项目详细描述


突变

pypi-version

将您的业务逻辑组合成对输入进行清理和验证的命令。

安装

$ pip install mutations

工作原理:

  1. 子类mutations.Mutation
  2. 定义您的输入
  3. 在命令中定义一个execute方法。
  4. 运行它,如下所示:SimpleMutation.run(foo='bar')

要了解更多信息,请参见this blog post

示例

importmutationsclassUserSignup(mutations.Mutation):"""Define the inputs to your mutation here. """email=mutations.fields.CharField(required=True)full_name=mutations.fields.CharField(required=True)send_welcome_email=mutations.fields.Boolean(required=False,default=True)defvalidate_email_address(self):"""Custom validation for a field.        If you encounter any validation errors and want to raise, you should        raise mutation.ValidationError or some sublcass thereof. Otherwise, it        assumes there were no problems.        Any function beginning with `validate_` is assumed to be a validator        function and will be run before the mutation can execute.        """ifnotself.email.is_valid():raisemutations.ValidationError("email_not_valid","Email is not valid.")defexecute(self):"""Executes the mutation.        This method does the heavy lifting. You can call it by calling .run() on        your mutation class.        """user=User.objects.create(email=self.email,name=self.full_name)ifself.send_welcome_email:EmailServer.deliver(recipient=self.email)returnuser

调用命令

>>>result=UserSignup.run(email=email,full_name="Bob Boblob")>>>result.successTrue>>>result.return_value<Userid=...>>>>result.errorsresult=...
>>>result=UserSignup.run(email=None)>>>result.successFalse>>>result.errorsmutations.ErrorDict({'email':['email_not_valid']})>>>result.valueNone

仅运行验证

>>>result=UserSignup.validate(email=email,full_name="Bob Boblob")>>>result.is_validTrue

测试

$ make tests

当您准备发布时,请确保测试通过了2.7版 通过运行tox:

$ tox

版本控制

这个项目使用Semantic Versioning

谢谢

感谢赛普拉斯的红宝石。我创建这个库是因为我在为Python寻找类似的东西

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

推荐PyPI第三方库


热门话题
java使用McClickListener单击了什么元素   Java时间戳在Oracle时间戳中不同情况下存储12 PM的奇怪行为   java无法使用事件总线对运行在不同机器上的垂直体进行通信   java Mockserver:收到请求后进行回调   java无法将Json字符串转换为Map<string,Object>   java如何按升序排列输出?   java视图行,带有oracle键。jbo。在SrCategoryParentIterator中找不到键[300100120394155]   javafxmysql连接示例   java正在等待加载完成   java是否可以将同一个有状态会话bean实例注入多个其他会话bean?   java无法让万向节检测离开或进入区域   使用JavaCV和OpenCV的java提供了dyld:lazy符号绑定失败:找不到符号:__sincos_stret   xml解析无法使用Java读取xml文档   java无法更改工具栏的颜色   javaapachesshd和JSCH   java无法在firebase存储中检索图像url   java问题与executeUpdate   同一应用程序中不同活动之间的java SharedReference