From b7611be5eea9553cb4f4fa05a95bfce838214034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdulkadir=20Furkan=20=C5=9Eanl=C4=B1?= Date: Sun, 30 Dec 2018 09:28:16 +0100 Subject: [PATCH] Handle ValueErrors and rewrite code --- TODO.md | 2 +- ib-clearance.py | 80 +++++++++++++++++++++---------------------------- 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/TODO.md b/TODO.md index 0e407ad..a32acde 100644 --- a/TODO.md +++ b/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) diff --git a/ib-clearance.py b/ib-clearance.py index 3469c12..08592a9 100755 --- a/ib-clearance.py +++ b/ib-clearance.py @@ -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()