forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 3
/
specification.py
118 lines (76 loc) · 2.71 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
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Gordeev Andrey <[email protected]>
*TL;DR80
Provides recombination business logic by chaining together using boolean logic.
"""
from abc import abstractmethod
class Specification(object):
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(object):
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)
if __name__ == '__main__':
print('Specification')
andrey = User()
ivan = User(super_user=True)
vasiliy = 'not User instance'
root_specification = UserSpecification().\
and_specification(SuperUserSpecification())
print(root_specification.is_satisfied_by(andrey))
print(root_specification.is_satisfied_by(ivan))
print(root_specification.is_satisfied_by(vasiliy))
### OUTPUT ###
# Specification
# False
# True
# False