规范模式的简单python实现。
sutoppu的Python项目详细描述
苏托普
sutopu(英文{em1}$stop中的日语)是规范模式的简单python实现。
什么是规范模式?
In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.
更多信息:Eric Evans and Martin Fowler article about Specifications
基本用法
安装
$ pip install sutoppu
使用量
fromsutoppuimportSpecificationclassFruit:def__init__(self,color:str,sweet:bool,bitter:bool):self.color=colorself.sweet=sweetself.bitter=bitterclassFruitIsBitter(Specification):description='The given fruit must be bitter.'defis_satisfied_by(self,fruit:Fruit):returnfruit.bitterisTrueclassFruitIsSweet(Specification):description='The given fruit must be sweet.'defis_satisfied_by(self,fruit:Fruit):returnfruit.sweetisTrueclassFruitIsColored(Specification):description='The given fruit must be {color}.'def__init__(self,color):super().__init__()self.color=colorself.description=self.description.format(color=color)defis_satisfied_by(self,fruit:Fruit):returnself.color==fruit.color
>>>lemon=Fruit(color='yellow',sweet=False,bitter=True)>>>is_a_lemon=FruitIsColored('yellow')&FruitIsBitter()&~FruitIsSweet()>>>is_a_lemon.is_satisfied_by(lemon)True
操作员
以及:
>>>my_spec=SpecificationA()&SpecificationB()
或:
>>>my_spec=SpecificationA()|SpecificationB()
不:
>>>my_spec=~SpecificationA()
较轻的语法
如果您觉得is_satisfied_by
方法不太方便,也可以直接调用下面的规范。
>>>lemon=Fruit(color='yellow',sweet=False,bitter=True)>>>is_a_lime=FruitIsColored('green')&FruitIsBitter()&~FruitIsSweet()>>>is_a_lime(lemon)False
错误报告
在复杂的规则中,很难知道哪个规范失败了。sutoppu允许在使用后通过获取errors
属性列出所有失败的规范。
每次使用规范时,都会重置errors
属性。对于每个失败的规范,它都返回一个dict,其中包含key的规范类的名称和value的类中提供的描述。在规范因not
条件失败的情况下,描述的前缀是Not ~
。
>>>apple=Fruit(color='red',sweet=True,bitter=False)>>>is_a_lemon=FruitIsColored('yellow')&FruitIsBitter()&~FruitIsSweet()>>>is_a_lemon.is_satisfied_by(apple)False>>>is_a_lemon.errors{'FruitIsColored':'The given fruit must be yellow.','FruitIsBitter':'The given fruit must be bitter.','FruitIsSweet':'Not ~ The given fruit must be sweet.'}