forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specification.py
117 lines (80 loc) · 2.79 KB
/
specification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
@author: Gordeev Andrey <gordeev.and.and@gmail.com>
*TL;DR
Provides recombination business logic by chaining together using boolean logic.
"""
from abc import abstractmethod
class Specification:
def and_specification(self, candidate):
raise NotImplementedError()
def or_specification(self, candidate):
raise NotImplementedError()
def not_specification(self):
raise NotImplementedError()
@abstractmethod
def is_satisfied_by(self, candidate):
pass
class CompositeSpecification(Specification):
@abstractmethod
def is_satisfied_by(self, candidate):
pass
def and_specification(self, candidate):
return AndSpecification(self, candidate)
def or_specification(self, candidate):
return OrSpecification(self, candidate)
def not_specification(self):
return NotSpecification(self)
class AndSpecification(CompositeSpecification):
_one = Specification()
_other = Specification()
def __init__(self, one, other):
self._one = one
self._other = other
def is_satisfied_by(self, candidate):
return bool(
self._one.is_satisfied_by(candidate)
and self._other.is_satisfied_by(candidate)
)
class OrSpecification(CompositeSpecification):
_one = Specification()
_other = Specification()
def __init__(self, one, other):
self._one = one
self._other = other
def is_satisfied_by(self, candidate):
return bool(
self._one.is_satisfied_by(candidate)
or self._other.is_satisfied_by(candidate)
)
class NotSpecification(CompositeSpecification):
_wrapped = Specification()
def __init__(self, wrapped):
self._wrapped = wrapped
def is_satisfied_by(self, candidate):
return bool(not self._wrapped.is_satisfied_by(candidate))
class User:
def __init__(self, super_user=False):
self.super_user = super_user
class UserSpecification(CompositeSpecification):
def is_satisfied_by(self, candidate):
return isinstance(candidate, User)
class SuperUserSpecification(CompositeSpecification):
def is_satisfied_by(self, candidate):
return getattr(candidate, "super_user", False)
def main():
"""
>>> andrey = User()
>>> ivan = User(super_user=True)
>>> vasiliy = 'not User instance'
>>> root_specification = UserSpecification().and_specification(SuperUserSpecification())
# Is specification satisfied by <name>
>>> root_specification.is_satisfied_by(andrey), 'andrey'
(False, 'andrey')
>>> root_specification.is_satisfied_by(ivan), 'ivan'
(True, 'ivan')
>>> root_specification.is_satisfied_by(vasiliy), 'vasiliy'
(False, 'vasiliy')
"""
if __name__ == "__main__":
import doctest
doctest.testmod()