-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraints.py
More file actions
122 lines (102 loc) · 3.41 KB
/
Copy pathConstraints.py
File metadata and controls
122 lines (102 loc) · 3.41 KB
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
119
120
121
122
from __future__ import annotations
from typing import Callable
from inspect import signature
class ConstraintError(Exception):
pass
class Constraint:
"""
This class represent a constraint, substantially a function wrapper with some additional features.
A Constraint is immutable
"""
def __init__(self, function: Callable[..., bool], dual: bool = False):
"""
:param function: function that represent the constraint; it should accept exactly 2 params and return a bool
:param dual: typically a constraint isn't commutative, if this param is setted to True, indicates that the constraint is the inverse of the function
:raise ConstraintError: if the passed function doesn't accept exactly 2 params or it isn't a Callable
"""
if not isinstance(function, Callable):
raise ConstraintError
self._function = function
self._cardinality = 2
self._dual: bool = dual
if len(signature(self._function).parameters) != self._cardinality:
raise ConstraintError
def __call__(self, value1, value2) -> bool:
"""
Executes the function constraint passing it the value. If it is a dual constraint, the values will be swapped
:param value1:
:param value2:
:return: a boolean that indicates if the constraint is respected by these values or not
"""
if self._dual:
value1, value2 = value2, value1
return self._function(value1, value2)
def getType(self) -> Callable:
"""
Returns the function inside the constraint
:return: constraint's function
"""
return self._function.__name__
def getDual(self) -> Constraint:
"""
returns the inverse constraint
:return: inverse constraint
"""
return Constraint(self._function, not self._dual)
def equals(a, b) -> bool:
"""
Predefined function constraint: check if two values are equal
:param a:
:param b:
:return: True if a==b, False otherwise
"""
if a == b:
return True
else:
return False
def different(a, b) -> bool:
"""
Predefined function constraint: check if two values are different
:param a:
:param b:
:return: True if a!=b, False otherwise
"""
return not equals(a, b)
def greater(a, b) -> bool:
"""
Predefined function constraint: check if a value is greater than another
:param a:
:param b:
:return: True if a>b, False otherwise
"""
if a > b:
return True
else:
return False
def greaterOrEqual(a, b) -> bool:
"""
Predefined function constraint: check if a value is greater than or equal to another
:param a:
:param b:
:return: True if a>=b, False otherwise
"""
if a >= b:
return True
else:
return False
def lesser(a, b) -> bool:
"""
Predefined function constraint: check if a value is lesser than another
:param a:
:param b:
:return: True if a<b, False otherwise
"""
return not greaterOrEqual(a, b)
def lesserOrEqual(a, b) -> bool:
"""
Predefined function constraint: check if a value is lesser than or equal to another
:param a:
:param b:
:return: True if a<=b, False otherwise
"""
return not greater(a, b)