-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartException.py
More file actions
87 lines (75 loc) · 2.83 KB
/
SmartException.py
File metadata and controls
87 lines (75 loc) · 2.83 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
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
import re
class SmartException:
"""
Currently supports (if error appears in the file where the context manager is) TODO: include support for errors in imported modules
KeyError for dict and pandas DataFrame
IndexError
AttributeError
"""
def __init__(self):
pass
def __enter__(self):
pass
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is not None:
l = self.get_line(traceback.tb_lineno)
if exc_type == KeyError:
failkey = str(exc_value).strip()
variable = re.findall(r"([a-zA-Z0-9]+)\[", l)[0]
print()
print("Error trying to index the following variable")
print(eval(variable))
print("Which has the following keys")
print(eval(variable).keys())
if exc_type == IndexError:
variables = re.findall(r"([a-zA-Z0-9]+)\[", l)
const_indexes = re.findall(r"\[(\d)\]", l)
var_indexes = re.findall(r"\[([a-zA-Z0-9]+)\]", l)
print()
print("Error trying to index the following variable(s)")
for var in variables:
print(eval(var))
print("with the following constant index(es)")
for idx in const_indexes:
print(eval(idx))
print("and the following variable index(es)")
for idx in var_indexes:
print(eval(idx))
if exc_type == AttributeError:
objects = re.findall(r"([a-zA-Z0-9]+)\.", l)
print()
print("Error trying to dot-index the following object(s)")
for obj in objects:
print(obj)
print("Which is/are of type")
for obj in objects:
print(type(obj))
print("which has/have the following attributes")
for obj in objects:
print(obj.__dir__())
def get_line(self, ln):
with open(__file__) as fid:
for _ in range(ln-1):
next(fid)
return next(fid)
# examples below:
if __name__ == '__main__':
with SmartException():
data = {
't': [1, 2, 3],
'r': [5, 7, 9],
}
# the following works
print(data['t'])
# the following fails with KeyError
print(data['k'])
shortlist = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
a = 2
b = [1, 6]
# the following index works
print(shortlist[1])
# the following fails with IndexError
print(shortlist[a][b[1]])
k = 3
# the following fails with AttributeError
k.append(2)