-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers.py
More file actions
52 lines (38 loc) · 1.31 KB
/
numbers.py
File metadata and controls
52 lines (38 loc) · 1.31 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
try: # python 3.5+
# noinspection PyUnresolvedReferences
from numbers import Integral
except ImportError:
pass
from valid8.base import ValidationFailure
class IsNotEven(ValidationFailure, ValueError):
""" Custom ValidationFailure raised by is_even """
help_msg = 'Value should be even'
def is_even(x # type: Integral
):
""" Validates that x is even (`x % 2 == 0`) """
if x % 2 == 0:
return True
else:
raise IsNotEven(wrong_value=x)
class IsNotOdd(ValidationFailure, ValueError):
""" Custom ValidationFailure raised by is_odd """
help_msg = 'Value should be odd'
def is_odd(x # type: Integral
):
""" Validates that x is odd (`x % 2 != 0`) """
if x % 2 != 0:
return True
else:
raise IsNotOdd(wrong_value=x)
class IsNotMultipleOf(ValidationFailure, ValueError):
""" Custom ValidationFailure raised by is_multiple_of """
help_msg = 'Value should be a multiple of {ref}'
def is_multiple_of(ref):
""" Validates that x is a multiple of the reference (`x % ref == 0`) """
def is_multiple_of_ref(x):
if x % ref == 0:
return True
else:
raise IsNotMultipleOf(wrong_value=x, ref=ref)
is_multiple_of_ref.__name__ = 'is_multiple_of_%s' % ref
return is_multiple_of_ref