-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.py
46 lines (33 loc) · 1.18 KB
/
constraint.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
from enum import Enum
import operator
from functools import reduce
class Operator(Enum):
le = operator.le
lt = operator.lt
ge = operator.ge
gt = operator.gt
eq = operator.eq
def describe(self):
# self is the member here
return self.name, self.value
def __str__(self):
return 'selected operator: {0}'.format(self.value)
def compare(self, lhs: float, rhs: float) -> bool:
return self.value(lhs, rhs)
class Constraint:
weight = None
def __init__(self, _operator: Operator, value: float, weight=1):
self.weight = weight
self._operator = _operator
self.value = value
def match(self, value) -> bool:
return self._operator.compare(self.value, value)
def __str__(self):
return "[x %s %f]" % (self._operator._name_, self.value)
class Constraints:
def __init__(self, constraints):
self.constraints = constraints
def validate(self, value):
return not (False in list(map(lambda constraint: constraint.match(value), self.constraints)))
def __str__(self):
return "constraints: {\n" + reduce(lambda x, y: str(x) + '\n' + str(y), self.constraints) + "\n}"