Handle ValueErrors and rewrite code
This commit is contained in:
parent
be5ff7177c
commit
b7611be5ee
2
TODO.md
2
TODO.md
@ -7,5 +7,5 @@ To-Do List
|
|||||||
- [ ] Flexible mandatory subjects
|
- [ ] Flexible mandatory subjects
|
||||||
|
|
||||||
## v0.3
|
## v0.3
|
||||||
- [ ] make script functional
|
- [x] make script functional (sorta)
|
||||||
- [ ] timetable editor (add & remove lessons, view timetable)
|
- [ ] timetable editor (add & remove lessons, view timetable)
|
||||||
|
@ -28,30 +28,27 @@ def connect_database(file):
|
|||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
def get_lower_weekday():
|
def request_id(conn):
|
||||||
"""
|
"""
|
||||||
Returns current weekday's name as lowercase string.
|
Request student ID, validate, and return.
|
||||||
"""
|
|
||||||
day = time.strftime("%A")
|
|
||||||
lower_day = day.lower()
|
|
||||||
return lower_day
|
|
||||||
|
|
||||||
|
|
||||||
def check_id(conn, id):
|
|
||||||
"""
|
|
||||||
Returns True if existing ID, False if not.
|
|
||||||
:param conn: Connection object
|
:param conn: Connection object
|
||||||
:param id: ID of student, int
|
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
id = int(input("Please enter student ID: "))
|
||||||
|
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute("SELECT id FROM students")
|
cur.execute("SELECT id FROM students")
|
||||||
ids = cur.fetchall()
|
ids = cur.fetchall()
|
||||||
|
|
||||||
for x in ids:
|
for x in ids:
|
||||||
|
# ids are single value tuples
|
||||||
if id == x[0]:
|
if id == x[0]:
|
||||||
return True
|
return id
|
||||||
else:
|
else:
|
||||||
return False
|
raise ValueError
|
||||||
|
except ValueError:
|
||||||
|
print("Incorrect ID, try again.")
|
||||||
|
request_id(conn)
|
||||||
|
|
||||||
|
|
||||||
def select_final_time(conn, day, id):
|
def select_final_time(conn, day, id):
|
||||||
@ -86,7 +83,7 @@ def select_final_time(conn, day, id):
|
|||||||
|
|
||||||
def parse_time_string(timestring):
|
def parse_time_string(timestring):
|
||||||
"""
|
"""
|
||||||
Parses given 24h time string of format "HH:MM" into time_struct.
|
Parses given 24h time string of format "HH:MM" into struct_time.
|
||||||
:param timestring: time string "HH:MM"
|
:param timestring: time string "HH:MM"
|
||||||
"""
|
"""
|
||||||
timestruct = time.strptime(timestring, "%H:%M")
|
timestruct = time.strptime(timestring, "%H:%M")
|
||||||
@ -96,35 +93,26 @@ def parse_time_string(timestring):
|
|||||||
def main():
|
def main():
|
||||||
print("ib-clearance")
|
print("ib-clearance")
|
||||||
print("============")
|
print("============")
|
||||||
db = input(
|
|
||||||
"Please enter name of database (in same folder as program): ")
|
|
||||||
conn = connect_database(db)
|
|
||||||
|
|
||||||
y = "y"
|
conn = connect_database("database.db")
|
||||||
while y == "y":
|
|
||||||
try:
|
|
||||||
id = int(input("Please input the student ID number: "))
|
|
||||||
|
|
||||||
if check_id(conn, id):
|
while True:
|
||||||
day = get_lower_weekday()
|
id = request_id(conn)
|
||||||
raw_time = select_final_time(conn, day, id)
|
day = time.strftime("%A").lower()
|
||||||
# if raw_time is None, student has no lessons today
|
finish_time = select_final_time(conn, day, id)
|
||||||
if raw_time != None:
|
current_time = time.strftime("%H:%M")
|
||||||
final_time = parse_time_string(raw_time)
|
|
||||||
current_time = parse_time_string(time.strftime("%H:%M"))
|
if finish_time == None:
|
||||||
if current_time > final_time:
|
print("Student has no lessons today, clear to leave.")
|
||||||
print("Clear to leave.")
|
elif parse_time_string(finish_time) < parse_time_string(current_time):
|
||||||
|
print("Student has finished for today, clear to leave.")
|
||||||
else:
|
else:
|
||||||
print("Student does not have clearance.")
|
print("Student still has lessons, clearance not granted.")
|
||||||
elif raw_time == None:
|
|
||||||
print("Clear to leave.")
|
|
||||||
else:
|
|
||||||
print("Student does not exist in the database. Please try again.")
|
|
||||||
except ValueError:
|
|
||||||
print("That was not an integer, please try again.")
|
|
||||||
|
|
||||||
y = input("Press y key followed by enter to run again.")
|
if input("Enter y to run again.") != "y":
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
# Run main() if script is run standalone.
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user