1
+ from typing import Callable , Tuple
2
+ from ..partition import Partition
3
+
4
+
1
5
class Bounds :
2
6
"""
3
7
Wrapper for numeric-validators to enforce upper and lower limits.
@@ -8,23 +12,28 @@ class Bounds:
8
12
9
13
"""
10
14
11
- def __init__ (self , func , bounds ) :
15
+ def __init__ (self , func : Callable , bounds : Tuple [ float , float ]) -> None :
12
16
"""
13
17
:param func: Numeric validator function. Should return an iterable of values.
18
+ :type func: Callable
14
19
:param bounds: Tuple of (lower, upper) numeric bounds.
20
+ :type bounds: Tuple[float, float]
15
21
"""
16
22
self .func = func
17
23
self .bounds = bounds
18
24
19
- def __call__ (self , * args , ** kwargs ):
25
+ def __call__ (self , * args , ** kwargs ) -> bool :
20
26
lower , upper = self .bounds
21
27
values = self .func (* args , ** kwargs )
22
28
return lower <= min (values ) and max (values ) <= upper
23
29
24
30
@property
25
- def __name__ (self ):
31
+ def __name__ (self ) -> str :
26
32
return "Bounds({},{})" .format (self .func .__name__ , str (self .bounds ))
27
33
34
+ def __repr__ (self ) -> str :
35
+ return "<{}>" .format (self .__name__ )
36
+
28
37
29
38
class UpperBound :
30
39
"""
@@ -33,25 +42,26 @@ class UpperBound:
33
42
This class is meant to be called as a function after instantiation; its
34
43
return is ``True`` if the numeric validator is within a set upper limit,
35
44
and ``False`` otherwise.
36
-
37
45
"""
38
46
39
- def __init__ (self , func , bound ) :
47
+ def __init__ (self , func : Callable , bound : float ) -> None :
40
48
"""
41
49
:param func: Numeric validator function. Should return a comparable value.
50
+ :type func: Callable
42
51
:param bounds: Comparable upper bound.
52
+ :type bounds: float
43
53
"""
44
54
self .func = func
45
55
self .bound = bound
46
56
47
- def __call__ (self , * args , ** kwargs ):
57
+ def __call__ (self , * args , ** kwargs ) -> bool :
48
58
return self .func (* args , ** kwargs ) <= self .bound
49
59
50
60
@property
51
- def __name__ (self ):
61
+ def __name__ (self ) -> str :
52
62
return "UpperBound({} >= {})" .format (self .func .__name__ , self .bound )
53
63
54
- def __repr__ (self ):
64
+ def __repr__ (self ) -> str :
55
65
return "<{}>" .format (self .__name__ )
56
66
57
67
@@ -62,25 +72,26 @@ class LowerBound:
62
72
This class is meant to be called as a function after instantiation; its
63
73
return is ``True`` if the numeric validator is within a set lower limit,
64
74
and ``False`` otherwise.
65
-
66
75
"""
67
76
68
- def __init__ (self , func , bound ) :
77
+ def __init__ (self , func : Callable , bound : float ) -> None :
69
78
"""
70
79
:param func: Numeric validator function. Should return a comparable value.
80
+ :type func: Callable
71
81
:param bounds: Comparable lower bound.
82
+ :type bounds: float
72
83
"""
73
84
self .func = func
74
85
self .bound = bound
75
86
76
- def __call__ (self , * args , ** kwargs ):
87
+ def __call__ (self , * args , ** kwargs ) -> bool :
77
88
return self .func (* args , ** kwargs ) >= self .bound
78
89
79
90
@property
80
- def __name__ (self ):
91
+ def __name__ (self ) -> str :
81
92
return "LowerBound({} <= {})" .format (self .func .__name__ , self .bound )
82
93
83
- def __repr__ (self ):
94
+ def __repr__ (self ) -> str :
84
95
return "<{}>" .format (self .__name__ )
85
96
86
97
@@ -94,21 +105,28 @@ class SelfConfiguringUpperBound:
94
105
This class is meant to be called as a function after instantiation; its
95
106
return is ``True`` if the numeric validator is within a set upper limit,
96
107
and ``False`` otherwise.
97
-
98
108
"""
99
109
100
- def __init__ (self , func ) :
110
+ def __init__ (self , func : Callable ) -> None :
101
111
"""
102
112
:param func: Numeric validator function.
113
+ :type func: Callable
103
114
"""
104
115
self .func = func
105
116
self .bound = None
106
117
107
- def __call__ (self , partition ) :
118
+ def __call__ (self , partition : Partition ) -> bool :
108
119
if not self .bound :
109
120
self .bound = self .func (partition )
110
121
return self .func (partition ) <= self .bound
111
122
123
+ @property
124
+ def __name__ (self ) -> str :
125
+ return "SelfConfiguringUpperBound({})" .format (self .func .__name__ )
126
+
127
+ def __repr__ (self ) -> str :
128
+ return "<{}>" .format (self .__name__ )
129
+
112
130
113
131
class SelfConfiguringLowerBound :
114
132
"""
@@ -120,36 +138,73 @@ class SelfConfiguringLowerBound:
120
138
This class is meant to be called as a function after instantiation; its
121
139
return is ``True`` if the numeric validator is within a set lower limit,
122
140
and ``False`` otherwise.
123
-
124
141
"""
125
142
126
- def __init__ (self , func , epsilon = 0.05 ):
143
+ def __init__ (self , func : Callable , epsilon : float = 0.05 ) -> None :
127
144
"""
128
145
:param func: Numeric validator function.
129
- :param epsilon: Initial "wiggle room" that the validator allows.
146
+ :type func: Callable
147
+ :param epsilon: Initial population deviation allowable by the validator.
148
+ :type epsilon: float
130
149
"""
131
150
self .func = func
132
151
self .bound = None
133
152
self .epsilon = epsilon
134
- self .__name__ = func .__name__
135
153
136
- def __call__ (self , partition ) :
154
+ def __call__ (self , partition : Partition ) -> bool :
137
155
if not self .bound :
138
156
self .bound = self .func (partition ) - self .epsilon
139
157
return self .func (partition ) >= self .bound
140
158
159
+ @property
160
+ def __name__ (self ) -> str :
161
+ return "SelfConfiguringLowerBound({})" .format (self .func .__name__ )
162
+
163
+ def __repr__ (self ) -> str :
164
+ return "<{}>" .format (self .__name__ )
165
+
141
166
142
167
class WithinPercentRangeOfBounds :
143
- def __init__ (self , func , percent ):
168
+ """
169
+ Wrapper for numeric-validators to enforce upper and lower limits
170
+ determined by a percentage of the initial value.
171
+
172
+ When instantiated, the initial upper and lower bounds are set as the
173
+ initial value of the numeric-validator times (1 ± percent).
174
+
175
+ This class is meant to be called as a function after instantiation; its
176
+ return is ``True`` if the numeric validator is within the desired
177
+ percentage range of the initial value, and ``False`` otherwise.
178
+ """
179
+
180
+ def __init__ (self , func : Callable , percent : float ) -> None :
181
+ """
182
+ :param func: Numeric validator function.
183
+ :type func: Callable
184
+ :param percent: Percentage of the initial value to use as the bounds.
185
+ :type percent: float
186
+
187
+ :return: None
188
+
189
+ .. Warning::
190
+ The percentage is assumed to be in the range [0.0, 100.0].
191
+ """
144
192
self .func = func
145
193
self .percent = float (percent ) / 100.0
146
194
self .lbound = None
147
195
self .ubound = None
148
196
149
- def __call__ (self , partition ) :
197
+ def __call__ (self , partition : Partition ) -> bool :
150
198
if not (self .lbound and self .ubound ):
151
199
self .lbound = self .func (partition ) * (1.0 - self .percent )
152
200
self .ubound = self .func (partition ) * (1.0 + self .percent )
153
201
return True
154
202
else :
155
203
return self .lbound <= self .func (partition ) <= self .ubound
204
+
205
+ @property
206
+ def __name__ (self ) -> str :
207
+ return "WithinPercentRangeOfBounds({})" .format (self .func .__name__ )
208
+
209
+ def __repr__ (self ) -> str :
210
+ return "<{}>" .format (self .__name__ )
0 commit comments