-
Notifications
You must be signed in to change notification settings - Fork 216
/
validators.py
556 lines (431 loc) · 18.6 KB
/
validators.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module containing canned measurement validators.
Additional validators may be registered by passing them to the Register()
method. They can then be accessed directly as attributes on the validators
module, and will typically be a type, instances of which are callable:
from openhtf.util import validators
from openhtf.util import measurements
class MyLessThanValidator(ValidatorBase):
def __init__(self, limit) -> None:
self.limit = limit
# This will be invoked to test if the measurement is 'PASS' or 'FAIL'.
def __call__(self, value) -> bool:
return value < self.limit
# Name defaults to the validator's __name__ attribute unless overridden.
validators.register(MyLessThanValidator, name='LessThan')
# Now you can refer to the validator by name directly on measurements.
@measurements.measures(
measurements.Measurement('my_measurement').LessThan(4))
def MyPhase(test):
test.measurements.my_measurement = 5 # Will have outcome 'FAIL'
If implemented as a class, inherit from a suitable base class defined in this
module; such validators may have specialized handling by the infrastructure that
you can leverage.
For simpler validators, you don't need to register them at all, you can
simply attach them to the Measurement with the .with_validator() method:
def LessThan4(value) -> bool:
return value < 4
@measurements.measures(
measurements.Measurement('my_measurement).with_validator(LessThan4))
def MyPhase(test: htf.TestApi) -> None:
test.measurements.my_measurement = 5 # Will also 'FAIL'
Notes:
Note the extra level of indirection when registering a validator. This allows
you to parameterize your validator (like in the LessThan example) when it is
being applied to the measurement. If you don't need this level of indirection,
it's recommended that you simply use .with_validator() instead.
Also note the validator will be str()'d in the output, so if you want a
meaningful description of what it does, you should implement a __str__ method.
Validators must also be deepcopy()'able, and may need to implement __deepcopy__
if they are implemented by a class that has internal state that is not copyable
by the default copy.deepcopy().
"""
import abc
import math
import numbers
import re
from typing import Callable, Dict, Optional, Union
from openhtf import util
class ValidatorBase(abc.ABC):
@abc.abstractmethod
def __call__(self, value) -> bool:
"""Should validate value, returning a boolean result."""
_ValidatorFactoryT = Union[Callable[..., ValidatorBase]]
_VALIDATORS: Dict[str, _ValidatorFactoryT] = {}
def register(validator: _ValidatorFactoryT,
name: Optional[str] = None) -> _ValidatorFactoryT:
name = name or validator.__name__
if name in _VALIDATORS:
raise ValueError('Duplicate validator name', name)
_VALIDATORS[name] = validator
return validator
def has_validator(name: str) -> bool:
return name in _VALIDATORS
def create_validator(name: str, *args, **kwargs) -> ValidatorBase:
return _VALIDATORS[name](*args, **kwargs)
_identity = lambda x: x
class RangeValidatorBase(ValidatorBase, abc.ABC):
@abc.abstractproperty
def minimum(self):
"""Should return the minimum, inclusive value of the range."""
@abc.abstractproperty
def maximum(self):
"""Should return the maximum, inclusive value of the range."""
@abc.abstractproperty
def marginal_minimum(self):
"""Should return the marginal minimum, inclusive value of the range."""
@abc.abstractproperty
def marginal_maximum(self):
"""Should return the marginal maximum, inclusive value of the range."""
@abc.abstractmethod
def is_marginal(self, value) -> bool:
"""Validates the value using the marginal limits."""
# Built-in validators below this line
class AllInRangeValidator(RangeValidatorBase):
"""Validator to verify a list of values are with in a range."""
def __init__(self,
minimum,
maximum,
marginal_minimum=None,
marginal_maximum=None) -> None:
super(AllInRangeValidator, self).__init__()
if minimum is None and maximum is None:
raise ValueError('Must specify minimum, maximum, or both')
if (minimum is not None and maximum is not None and
isinstance(minimum, numbers.Number) and
isinstance(maximum, numbers.Number) and minimum > maximum):
raise ValueError('Minimum cannot be greater than maximum')
if marginal_minimum is not None and minimum is None:
raise ValueError('Marginal minimum was specified without a minimum')
if marginal_maximum is not None and maximum is None:
raise ValueError('Marginal maximum was specified without a maximum')
if (marginal_minimum is not None and isinstance(minimum, numbers.Number) and
isinstance(marginal_minimum, numbers.Number) and
minimum > marginal_minimum):
raise ValueError('Marginal minimum cannot be less than the minimum')
if (marginal_maximum is not None and isinstance(maximum, numbers.Number) and
isinstance(marginal_maximum, numbers.Number) and
maximum < marginal_maximum):
raise ValueError('Marginal maximum cannot be greater than the maximum')
if (marginal_minimum is not None and marginal_maximum is not None and
isinstance(marginal_minimum, numbers.Number) and
isinstance(marginal_maximum, numbers.Number) and
marginal_minimum > marginal_maximum):
raise ValueError(
'Marginal minimum cannot be greater than the marginal maximum')
self._minimum = minimum
self._maximum = maximum
self._marginal_minimum = marginal_minimum
self._marginal_maximum = marginal_maximum
@property
def minimum(self):
return self._minimum
@property
def maximum(self):
return self._maximum
@property
def marginal_minimum(self):
return self._marginal_minimum
@property
def marginal_maximum(self):
return self._marginal_maximum
def __call__(self, values) -> bool:
within_maximum = self._maximum is None or all(
value <= self.maximum for value in values)
within_minimum = self._minimum is None or all(
value >= self.minimum for value in values)
return within_minimum and within_maximum
def is_marginal(self, values) -> bool:
is_maximally_marginal = self._marginal_maximum is not None and any(
[self._marginal_maximum <= value <= self._maximum for value in values])
is_minimally_marginal = self._marginal_minimum is not None and any(
[self._minimum <= value <= self._marginal_minimum for value in values])
return is_maximally_marginal or is_minimally_marginal
def __str__(self):
assert self._minimum is not None or self._maximum is not None
if (self._minimum is not None and self._maximum is not None and
self._minimum == self._maximum):
return 'x == {}'.format(self._minimum)
string_repr = ''
if self._minimum is not None:
string_repr += '{} <= '.format(self._minimum)
if self._marginal_minimum is not None:
string_repr += 'Marginal:{} <= '.format(self._marginal_minimum)
string_repr += 'x'
if self._marginal_maximum is not None:
string_repr += ' <= Marginal:{}'.format(self._marginal_maximum)
if self._maximum is not None:
string_repr += ' <= {}'.format(self._maximum)
return string_repr
class AllEqualsValidator(ValidatorBase):
"""Validator to verify a list of values are equal to the expected value."""
def __init__(self, spec) -> None:
super(AllEqualsValidator, self).__init__()
self._spec = spec
@property
def spec(self):
return self._spec
def __call__(self, values) -> bool:
return all([value == self.spec for value in values])
def __str__(self) -> str:
return "'x' is equal to '%s'" % self._spec
register(AllInRangeValidator, name='all_in_range')
@register
def all_equals(value, type=None): # pylint: disable=redefined-builtin
if isinstance(value, numbers.Number):
return AllInRangeValidator(minimum=value, maximum=value)
elif isinstance(value, str):
assert type is None or issubclass(
type, str), ('Cannot use a non-string type when matching a string')
return matches_regex('^{}$'.format(re.escape(value)))
else:
return AllEqualsValidator(value)
class InRange(RangeValidatorBase):
"""Validator to verify a numeric value is within a range."""
def __init__(self,
minimum=None,
maximum=None,
marginal_minimum=None,
marginal_maximum=None,
type=None) -> None: # pylint: disable=redefined-builtin
super(InRange, self).__init__()
if minimum is None and maximum is None:
raise ValueError('Must specify minimum, maximum, or both')
if (minimum is not None and maximum is not None and
isinstance(minimum, numbers.Number) and
isinstance(maximum, numbers.Number) and minimum > maximum):
raise ValueError('Minimum cannot be greater than maximum')
if marginal_minimum is not None and minimum is None:
raise ValueError('Marginal minimum was specified without a minimum')
if marginal_maximum is not None and maximum is None:
raise ValueError('Marginal maximum was specified without a maximum')
if (marginal_minimum is not None and isinstance(minimum, numbers.Number) and
isinstance(marginal_minimum, numbers.Number) and
minimum > marginal_minimum):
raise ValueError('Marginal minimum cannot be less than the minimum')
if (marginal_maximum is not None and isinstance(maximum, numbers.Number) and
isinstance(marginal_maximum, numbers.Number) and
maximum < marginal_maximum):
raise ValueError('Marginal maximum cannot be greater than the maximum')
if (marginal_minimum is not None and marginal_maximum is not None and
isinstance(marginal_minimum, numbers.Number) and
isinstance(marginal_maximum, numbers.Number) and
marginal_minimum > marginal_maximum):
raise ValueError(
'Marginal minimum cannot be greater than the marginal maximum')
self._minimum = minimum
self._maximum = maximum
self._marginal_minimum = marginal_minimum
self._marginal_maximum = marginal_maximum
self._type = type
@property
def minimum(self):
converter = self._type if self._type is not None else _identity
return converter(self._minimum)
@property
def maximum(self):
converter = self._type if self._type is not None else _identity
return converter(self._maximum)
@property
def marginal_minimum(self):
converter = self._type if self._type is not None else _identity
return converter(self._marginal_minimum)
@property
def marginal_maximum(self) -> str:
converter = self._type if self._type is not None else _identity
return converter(self._marginal_maximum)
def with_args(self, **kwargs):
return type(self)(
minimum=util.format_string(self._minimum, kwargs),
maximum=util.format_string(self._maximum, kwargs),
marginal_minimum=util.format_string(self._marginal_minimum, kwargs),
marginal_maximum=util.format_string(self._marginal_maximum, kwargs),
type=self._type,
)
def __call__(self, value) -> bool:
if value is None:
return False
if math.isnan(value):
return False
if self._minimum is not None and value < self.minimum:
return False
if self._maximum is not None and value > self.maximum:
return False
return True
def is_marginal(self, value) -> bool:
if value is None:
return False
if math.isnan(value):
return False
if (self._marginal_minimum is not None and
self.minimum <= value <= self.marginal_minimum):
return True
if (self._marginal_maximum is not None and
self.maximum >= value >= self.marginal_maximum):
return True
return False
def __str__(self) -> str:
assert self._minimum is not None or self._maximum is not None
if (self._minimum is not None and self._maximum is not None and
self._minimum == self._maximum):
return 'x == {}'.format(self._minimum)
string_repr = ''
if self._minimum is not None:
string_repr += '{} <= '.format(self._minimum)
if self._marginal_minimum is not None:
string_repr += 'Marginal:{} <= '.format(self._marginal_minimum)
string_repr += 'x'
if self._marginal_maximum is not None:
string_repr += ' <= Marginal:{}'.format(self._marginal_maximum)
if self._maximum is not None:
string_repr += ' <= {}'.format(self._maximum)
return string_repr
def __eq__(self, other) -> bool:
return (isinstance(other, type(self)) and self.minimum == other.minimum and
self.maximum == other.maximum and
self.marginal_minimum == other.marginal_minimum and
self.marginal_maximum == other.marginal_maximum)
def __ne__(self, other) -> bool:
return not self == other
in_range = InRange # pylint: disable=invalid-name
register(in_range, name='in_range')
@register
def equals(value, type=None): # pylint: disable=redefined-builtin
if isinstance(value, numbers.Number):
return InRange(minimum=value, maximum=value, type=type)
elif isinstance(value, str):
assert type is None or issubclass(
type, str), ('Cannot use a non-string type when matching a string')
return matches_regex('^{}$'.format(re.escape(value)))
else:
return Equals(value, type=type)
class Equals(ValidatorBase):
"""Validator to verify an object is equal to the expected value."""
def __init__(self, expected, type=None) -> None: # pylint: disable=redefined-builtin
self._expected = expected
self._type = type
@property
def expected(self):
converter = self._type if self._type is not None else _identity
return converter(self._expected)
def __call__(self, value):
return value == self.expected
def __str__(self) -> str:
return f"'x' is equal to '{self._expected}'"
def __eq__(self, other) -> bool:
return isinstance(other, type(self)) and self.expected == other.expected
class RegexMatcher(ValidatorBase):
"""Validator to verify a string value matches a regex."""
def __init__(self, regex, compiled_regex) -> None:
self._compiled = compiled_regex
self.regex = regex
def __call__(self, value) -> bool:
return self._compiled.match(str(value)) is not None
def __deepcopy__(self, dummy_memo):
return type(self)(self.regex, self._compiled)
def __str__(self):
return "'x' matches /%s/" % self.regex
def __eq__(self, other):
return isinstance(other, type(self)) and self.regex == other.regex
def __ne__(self, other) -> bool:
return not self == other
@register
def matches_regex(regex):
return RegexMatcher(regex, re.compile(regex))
class WithinPercent(RangeValidatorBase):
"""Validates that a number is within percent of a value."""
def __init__(self, expected, percent, marginal_percent=None) -> None:
super(WithinPercent, self).__init__()
if percent < 0:
raise ValueError('percent argument is {}, must be >0'.format(percent))
if marginal_percent is not None and marginal_percent >= percent:
raise ValueError(
'marginal_percent argument is {}, must be < {} percent'.format(
marginal_percent, percent))
self.expected = expected
self.percent = percent
self.marginal_percent = marginal_percent
@property
def _applied_percent(self):
return abs(self.expected * self.percent / 100.0)
@property
def _applied_marginal_percent(self):
return (abs(self.expected * self.marginal_percent /
100.0) if self.marginal_percent else 0)
@property
def minimum(self):
return self.expected - self._applied_percent
@property
def maximum(self):
return self.expected + self._applied_percent
@property
def marginal_minimum(self):
return (self.expected -
self._applied_marginal_percent if self.marginal_percent else None)
@property
def marginal_maximum(self):
return (self.expected +
self._applied_marginal_percent if self.marginal_percent else None)
def __call__(self, value) -> bool:
return self.minimum <= value <= self.maximum
def is_marginal(self, value) -> bool:
if self.marginal_percent is None:
return False
else:
return (self.minimum < value <= self.marginal_minimum or
self.marginal_maximum <= value < self.maximum)
def __str__(self) -> str:
return "'x' is within {}% of {}. Marginal: {}% of {}".format(
self.percent, self.expected, self.marginal_percent, self.expected)
def __eq__(self, other) -> bool:
return (isinstance(other, type(self)) and
self.expected == other.expected and
self.percent == other.percent and
self.marginal_percent == other.marginal_percent)
def __ne__(self, other) -> bool:
return not self == other
@register
def within_percent(expected, percent):
return WithinPercent(expected, percent)
class DimensionPivot(ValidatorBase):
"""Runs a validator on each actual value of a dimensioned measurement."""
def __init__(self, sub_validator) -> None:
super(DimensionPivot, self).__init__()
self._sub_validator = sub_validator
def __call__(self, dimensioned_value) -> bool:
return all(self._sub_validator(row[-1]) for row in dimensioned_value)
def __str__(self) -> str:
return 'All values pass: {}'.format(str(self._sub_validator))
@register
def dimension_pivot_validate(sub_validator):
return DimensionPivot(sub_validator)
class ConsistentEndDimensionPivot(ValidatorBase):
"""If any rows validate, all following rows must also validate."""
def __init__(self, sub_validator) -> None:
super(ConsistentEndDimensionPivot, self).__init__()
self._sub_validator = sub_validator
def __call__(self, dimensioned_value) -> bool:
for index, row in enumerate(dimensioned_value):
if self._sub_validator(row[-1]):
i = index
break
else:
return False
return all(self._sub_validator(rest[-1]) for rest in dimensioned_value[i:])
def __str__(self) -> str:
return 'Once pass, rest must also pass: {}'.format(str(self._sub_validator))
@register
def consistent_end_dimension_pivot_validate(sub_validator):
return ConsistentEndDimensionPivot(sub_validator)