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