-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.py
28 lines (22 loc) · 1.01 KB
/
exceptions.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
SIGNS = ('<', '>', '<=', '>=', '=')
class InvalidSignError(Exception):
def __init__(self, sign, message=None):
if message is None:
message = f'Error: Your sign = {sign} must be one of {SIGNS}!'
super().__init__(message)
class NumberValueError(Exception):
def __init__(self, num, sign, value, message=None):
if sign not in SIGNS:
raise InvalidSignError(sign)
if message is None:
variable = [name for name, i in locals().items() if i == num][0]
message = f'Error: {variable} = {num} must be {sign} {value}!'
super().__init__(message)
class IterableLengthError(Exception):
def __init__(self, iterable, sign, value, message=None):
if sign not in SIGNS:
raise InvalidSignError(sign)
if message is None:
variable = [name for name, i in locals().items() if i == iterable][0]
message = f'Error: len({variable}) = {len(iterable)} must be {sign} {value}!'
super().__init__(message)