-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_dos.py
101 lines (78 loc) · 2.51 KB
/
to_dos.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
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(self):
.........
@classmethod
def tearDownClass(self):
..........
"""
OOPs : Object-Oriented Programming
Since these three methods insert, update, delete have the same definition in every model class then what you can do is you can make a class inheritedClassName which can be inherited in other classes and so you can use the same code.
This will also make your code reusable and will increase code readability as well!
For Example:
'''
Extend the base Model class to add common methods
'''
class inheritedClassName(db.Model):
__abstract__ = True
def insert(self):
try:
db.session.add(self)
db.session.commit()
except:
db.session.rollback()
print(sys.exc_info())
def delete(self):
try:
db.session.delete(self)
db.session.commit()
except:
db.session.rollback()
print(sys.exc_info())
def update(self):
try:
db.session.commit()
except:
db.session.rollback()
print(sys.exc_info())
'''
Vehicle
'''
@dataclass
class Vehicle(inheritedClassName):
id: int
license_plate: String
model: String
make: String
__tablename__ = "vehicles"
id = Column(Integer, primary_key=True)
license_plate = Column(String, unique=True)
model = Column(String)
make = Column(String)
seats = Column(Integer)
This Vehicle(inheritedClassName) syntax tells that you are inheriting the class inheritedClassName in the class Vehicle.
You can group methods from the same module like so:
from flask import (
Flask,
request,
flash,
redirect,
render_template,
....,
other_packages_name ,
url_for,
flash
)
app.py line 336:
To see what error is being thrown, you may use:
print(sys.exc_info())
For debugging purposes you can use sys.exc_info().You can refer more here
Flash messages could also be useful here if you have a frontend to show.
You can also try using logging to get the best debugging results. I suggest you to please go through the Module-Level Functions here to broaden your knowledge.
SUGGESTION
You can make use of flask-blueprint to properly separating your endpoints. Like - You can arrange endpoints related to actors in one file and endpoints related to movies in another file!
SUGGESTION
You should document the endpoint by use of multiline docstring!
Check the best practices on Python Multiline docstring
"""