-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrangeforce.py
678 lines (536 loc) · 25.3 KB
/
rangeforce.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright © 2019-2020, Matjaž Guštin <[email protected]> <https://matjaz.it>.
# Released under the BSD 3-Clause License
"""Rangeforce, a developer-friendly check for "is this value
within the allowed range?" with user-friendly error messages.
Rangeforce aims to simplify the input validation process where the error
message of the exception is shown directly to the user and has to be
understandable, which may happened in a command line interface or when using
a Python program interactively from a Python shell.
For example:
value = int(input('How many hours per day do you sleep? '))
value = rangeforce.limited(value, 0, 24, name='Hours of sleep')
# Now value is valid. Otherwise an error message like this appears:
# rangeforce.RangeError: Hours of sleep must be in range [0, 24]. 25
found instead.
# Especially useful for values that need to fit within an integer type:
value = rangeforce.uint16(int(input('Type a 16-bit value: ')))
rangeforce.RangeError: Value must be in range [0, 65535]. 70000
found instead.
"""
import math
__VERSION__ = '1.1.0'
class RangeError(Exception):
"""Value outside of the allowed range.
A custom exception type raised by Rangeforce functions when the validation
of the values fails, that is when the values are not within the acceptable
bounds.
"""
pass
def clip(value, min, max):
"""Clips (limits) the value to the given limits.
The output is at least `min`, at most `max` and `value` if that value
is between the `min` and `max`.
Args:
value: to be limited to [min, max]
min: smallest acceptable value
max: greatest acceptable value
Returns:
the given value if within [min, max] or min if value is smaller than
min or max if value is greater than max.
"""
if value < min:
return min
elif value > max:
return max
else:
return value
def exactly(value, expected, name='Value', dtype=None, ex=RangeError):
"""Validates that value is exactly equal to another one.
If the value is valid, it returns the value itself: this is also the case
for comparing NaN to NaN, to avoid having a separate function for it.
In other words: exactly(NaN, NaN) is valid, does not raise exceptions.
If the value is not valid, it raises an exception of type ex with an
understandable error message that includes expected and failing value.
The name of the value can be altered for a customized error message.
The data type can be enforced if specified.
The exception class can be altered to a custom one.
Args:
value: the value to be validated to be within [min, max]
expected: only acceptable value. Not None. Can be NaN.
name: customizable name of the value that appears in the error message
dtype: optional data type the value has to be
ex: exception type to throw in case the value is out of range
Returns:
the given value if matching the expected value and, optionally, if of
the correct data type
Raises:
RangeError or type(ex): if the value is not matching the expected one.
TypeError: if the value is not of the acceptable data type, if
specified.
Examples:
>>> exactly(0.5, 0.5) # Valid value
0.5
>>> exactly(500, 30) # Incorrect value
rangeforce.RangeError: Value must be exactly 30. 500
found instead.
>>> exactly(50, 7, name='Days in a week')
rangeforce.RangeError: Days in a week must be exactly 7. 50 found
instead.
>>> exactly(42.0, math.nan)
rangeforce.RangeError: Value must be exactly NaN. 42.0
found instead..
>>> exactly(math.nan, math.nan) # Valid equality, also for NaN
nan
>>> exactly(7.0, 7, name='Days in a week', dtype=int)
TypeError: Days in a week must be of type int. float found
instead.
>>> exactly(7.0, 7, name='Days in a week', ex=ValueError)
ValueError: Days in a week must be exactly 7. 50 found instead.
"""
_validate_type(name, value, dtype)
try:
if math.isnan(expected):
if math.isnan(value):
# NaN is EQUAL to NaN for this function.
return value
else:
raise ex(
'{:} must be exactly NaN. '
'{:} found instead.'.format(name, value)
)
except TypeError:
# Suppress math.isnan() applied to non-floats and just go on.
pass
if value != expected:
raise ex(
'{:} must be exactly {:}. '
'{:} found instead.'.format(name, expected, value)
)
else:
return value
def limited(value, min, max, name='Value', dtype=None, ex=RangeError):
"""Validates that value is within the [min, max] interval.
If the value is valid, it returns the value itself.
If the value is not valid, it raises an exeption with an understandable
error message that includes expected range and failing value.
Either min or max can be set to None for an unbound validity interval, i.e.
the value only has to be smaller or greater than something, not within a
closed interval.
The name of the value can be altered for a customized error message.
The data type can be enforced if specified.
The exception class can be altered to a custom one.
Args:
value: the value to be validated to be within [min, max]
min: smallest acceptable value. Can be None if max is not None.
Can be +inf, -inf. Cannot be NaN. Must be <= max.
max: greatest acceptable value. Can be None if min is not None.
Can be +inf, -inf. Cannot be NaN. Must be >= min.
name: customizable name of the value that appears in the error message
dtype: optional data type the value has to be
ex: exception type to throw in case the value is out of range
Returns:
the given value if within [min, max] and, optionally, of the correct
data type
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not of the acceptable data type, if
specified.
ValueError: if the min, max extremes are not valid (e.g. both None,
min greater than max, NaN etc.)
Examples:
>>> limited(0.5, 0, 1) # Valid value
0.5
>>> limited(500, 0.1, 42) # Value out of range
rangeforce.RangeError: Value must be in range [0.1, -42]. 500
found instead.
>>> limited(50, 0, 24, name='Hours in a day')
rangeforce.RangeError: Hours in a day must be in range [0, 24]. 50
found instead.
>>> limited(-1, 0, None, name='Earth satellites')
rangeforce.RangeError: Earth satellites must be in range [0, +inf[.
-1 found instead.
>>> limited(1.1, 0, None, name='Earth satellites', dtype=int)
TypeError: Earth satellites must be of type int. float found
instead.
>>> limited(1.1, 0, None, name='Earth satellites', ex=ValueError)
ValueError: Earth satellites must be in range [0, +inf[. -1 found
instead.
"""
_validate_interval(min, max)
_validate_type(name, value, dtype)
if min is None and max is not None and (value > max or math.isnan(value)):
raise ex(
'{:} must be in range ]-inf, {:}]. '
'{:} found instead.'.format(name, max, value)
)
elif max is None and min is not None and (
value < min or math.isnan(value)):
raise ex(
'{:} must be in range [{:}, +inf[. '
'{:} found instead.'.format(name, min, value)
)
elif min is not None and max is not None and (
value < min or value > max or math.isnan(value)):
raise ex(
'{:} must be in range [{:}, {:}]. '
'{:} found instead.'.format(name, min, max, value)
)
else:
return value
def _validate_interval(min, max):
if min is None and max is None:
raise ValueError(
'[min, max] interval must be closed on at least one extreme.')
elif min is not None and math.isnan(min):
raise ValueError('NaN is not a valid interval lower bound.')
elif max is not None and math.isnan(max):
raise ValueError('NaN is not a valid interval upper bound.')
elif min is not None and max is not None and min > max:
raise ValueError(
'Interval extremes [{:}, {:}] not in order.'.format(min, max))
def _validate_type(name, value, dtype):
if dtype is not None and not isinstance(value, dtype):
raise TypeError(
'{:} must be of type {:}. '
'{:} found instead.'.format(name, dtype.__name__,
type(value).__name__)
)
def negative_int(value, name='Value', ex=RangeError):
"""Validates that value is negative (< 0) and of type int.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within ]-inf, 0[
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value if < 0
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, None, -1, name, dtype=int, ex=ex)
def nonpositive_int(value, name='Value', ex=RangeError):
"""Validates that value is non-positive (<= 0) and of type int.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within ]-inf, 0]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value if <= 0
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, None, 0, name, dtype=int, ex=ex)
def positive_int(value, name='Value', ex=RangeError):
"""Validates that value is positive (> 0) and of type int.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within ]0, +inf[
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value if > 0
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 1, None, name, dtype=int, ex=ex)
def nonnegative_int(value, name='Value', ex=RangeError):
"""Validates that value is non-negative (>= 0) and of type int.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [0, +inf[
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value if >= 0
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 0, None, name, dtype=int, ex=ex)
def uint8(value, name='Value', ex=RangeError):
"""Validates that value fits in an 8-bit unsigned integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [0, 255]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 8 bits (unsigned)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 0, 0xFF, name, dtype=int, ex=ex)
def uint16(value, name='Value', ex=RangeError):
"""Validates that value fits in a 16-bit unsigned integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [0, 65535]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 16 bits (unsigned)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 0, 0xFFFF, name, dtype=int, ex=ex)
def uint32(value, name='Value', ex=RangeError):
"""Validates that value fits in a 32-bit unsigned integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [0, 4294967295]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 32 bits (unsigned)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 0, 0xFFFFFFFF, name, dtype=int, ex=ex)
def uint64(value, name='Value', ex=RangeError):
"""Validates that value fits in a 64-bit unsigned integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [0, 18446744073709551615]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 64 bits (unsigned)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 0, 0xFFFFFFFFFFFFFFFF, name, dtype=int, ex=ex)
def uint_bits(value, bits, name='Value', ex=RangeError):
"""Validates that value fits in an unsigned integer of specified bitlength.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [0, 2**bits-1]
bits: integer, positive number of bits determining the size and thus
the range of the value
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in the given amount of bits
(unsigned)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, 0, (1 << bits) - 1, name, dtype=int, ex=ex)
def int8(value, name='Value', ex=RangeError):
"""Validates that value fits in an 8-bit signed integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [-128, 127]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 8 bits (signed)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, -0x80, 0x7F, name, dtype=int, ex=ex)
def int16(value, name='Value', ex=RangeError):
"""Validates that value fits in a 16-bit signed integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [-32768, 32767]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 16 bits (signed)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, -0x8000, 0x7FFF, name, dtype=int, ex=ex)
def int32(value, name='Value', ex=RangeError):
"""Validates that value fits in a 32-bit signed integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [-2147483648, 2147483647]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 32 bits (signed)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, -0x80000000, 0x7FFFFFFF, name, dtype=int, ex=ex)
def int64(value, name='Value', ex=RangeError):
"""Validates that value fits in a 16-bit signed integer.
If the value is valid, it returns the value itself.
If the value is not valid, it raises a RangeError with an understandable
error message that includes expected range and failing value.
Args:
value: the value to be validated to be within [-9223372036854775808,
9223372036854775807]
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given value can be expressed in 64 bits (signed)
Raises:
RangeError or type(ex): if the value is not within the acceptable
range.
TypeError: if the value is not an integer.
"""
return limited(value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF, name,
dtype=int, ex=ex)
def limited_len(sized, min, max, name='value', ex=RangeError):
"""Validates that value has a length within the [min, max] interval.
If the sized value is valid, it returns the value itself.
If the sized value is not valid, it raises an exception with an
understandable error message that includes expected length range and
failing sized value.
Either min or max can be set to None for an unbound validity
interval, i.e. the value only has to have a length smaller or greater than
something, not within a closed interval.
The name of the sized value can be altered for a customized error message.
The exception class can be altered to a custom one.
Args:
sized: the value whose length is to be validated to be within
[min, max]
min: smallest acceptable length. Can be None if max is not None.
Can be +inf, -inf. Cannot be NaN. Must be <= max and >= 0.
max: greatest acceptable length. Can be None if min is not None.
Can be +inf, -inf. Cannot be NaN. Must be >= min and >= 0.
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given sized value if has length within [min, max]
Raises:
RangeError or type(ex): if the value does not have a length within the
acceptable range.
ValueError: if the min, max extremes are not valid (e.g. negative,
both None, min greater than max, NaN etc.)
Examples:
>>> limited_len([1, 2, 3], 0, 10) # Valid value
[1, 2, 3]
>>> limited_len([1, 2, 3], 0, 2) # Value out of range
rangeforce.RangeError: Length of value must be in range [0,
2]. 3 found instead.
>>> limited_len([1, 2, 3, 4], 0, 3, name='groups')
rangeforce.RangeError: Length of groups must be in range [0,
3]. 4 found instead.
>>> limited_len([1, 2, 3], 10, None)
rangeforce.RangeError: Length of value must be in range [10,
+inf[. 3 found instead.
>>> limited_len([1, 2, 3], 10, 20, ex=ValueError)
ValueError: Length of value must be in range [10, 20]. 3 found
instead.
"""
_validate_non_negative_interval_extremes(min, max)
limited(len(sized), min, max, name='Length of ' + name, ex=ex)
return sized
def _validate_non_negative_interval_extremes(min, max):
if min is not None and min < 0:
raise ValueError(
'Length lower bound must be non-negative. '
'{:} found instead.'.format(min)
)
elif max is not None and max < 0:
raise ValueError(
'Length upper bound must be non-negative. '
'{:} found instead.'.format(max)
)
def exact_len(sized, expected, name='value', ex=RangeError):
"""Validates that value has an exact length.
If the sized value is valid, it returns the value itself.
If the sized value is not valid, it raises an exception with an
understandable error message that includes expected length and failing
sized value.
The name of the sized value can be altered for a customized error message.
The exception class can be altered to a custom one.
Args:
sized: the value whose length is to be validated to be exactly as
expected.
expected: only acceptable length. Must be an integer >= 0.
name: customizable name of the value that appears in the error message
ex: exception type to throw in case the value is out of range
Returns:
the given sized value if has length matching the expected
Raises:
RangeError or type(ex): if the value does not have a length matching
the expected.
TypeError: if the expected length is not an integer
ValueError: if the expected length is negative
Examples:
>>> exact_len([1, 2, 3], 3) # Valid value
[1, 2, 3]
>>> exact_len([1, 2, 3], 2)
rangeforce.RangeError: Length of value must be exactly 2. 3
found instead.
>>> exact_len([1], 2, name='pairs')
rangeforce.RangeError: Length of pairs must be exactly 2. 1
found instead.
>>> exact_len([1], 2, name='pairs', ex=ValueError)
ValueError: Length of pairs must be exactly 2. 1 found instead.
"""
length = len(sized)
_validate_expected_length(expected)
if length != expected:
raise ex(
'Length of {:} must be exactly {:}. '
'{:} found instead.'.format(name, expected, length)
)
return sized
def _validate_expected_length(expected):
if not isinstance(expected, int):
raise TypeError(
'Expected length must be an integer. '
'{:} found instead.'.format(type(expected).__name__)
)
elif expected < 0:
raise ValueError(
'Expected length must be non-negative. '
'{:} found instead.'.format(expected)
)