From e8f51079d7a8d829ee66d7a4d998fd1be7816fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdulkadir=20Furkan=20=C5=9Eanl=C4=B1?= Date: Sat, 5 Jan 2019 10:49:18 +0100 Subject: [PATCH] Databases now contain all classes' data --- TODO.md | 2 +- ib-clearance.py | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index a32acde..2f48b8e 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,7 @@ To-Do List ## v0.2 - [x] Error handling (when entering invalid data types) -- [ ] Make database contain data for all classes +- [x] Make database contain data for all classes - [ ] Flexible mandatory subjects ## v0.3 diff --git a/ib-clearance.py b/ib-clearance.py index 08592a9..1389636 100755 --- a/ib-clearance.py +++ b/ib-clearance.py @@ -28,35 +28,37 @@ def connect_database(file): return conn -def request_id(conn): +def get_data(conn): """ - Request student ID, validate, and return. + Requests student ID and returns integer tuple (id, class) with valid + student id and corresponding class no. :param conn: Connection object """ try: id = int(input("Please enter student ID: ")) cur = conn.cursor() - cur.execute("SELECT id FROM students") + cur.execute("SELECT id, student_class FROM students") ids = cur.fetchall() for x in ids: - # ids are single value tuples + # ids is list with single value tuples if id == x[0]: - return id + # return tuple x with valid id and corresponding class no. + return x else: raise ValueError except ValueError: print("Incorrect ID, try again.") - request_id(conn) + return get_data(conn) -def select_final_time(conn, day, id): +def select_final_time(conn, day, stud): """ Returns ending time of final lesson student must attend, or None. :param conn: Connection object :param day: current weekday, lowercase str - :param id: ID of student, int + :param stud: tuple with student ID no. and class no., int """ cur = conn.cursor() cur.execute(""" @@ -70,10 +72,12 @@ def select_final_time(conn, day, id): OR g6 = lesson_id OR lesson_id = 'gu' OR lesson_id = 'tok' - WHERE id = ? AND day = ? + WHERE id = ? + AND day = ? + AND lesson_class = ? ORDER BY end_time DESC LIMIT 1 - """, (id, day)) + """, (stud[0], day, stud[1])) finish = cur.fetchone() if str(type(finish)) == "": return finish[0] @@ -86,8 +90,8 @@ def parse_time_string(timestring): Parses given 24h time string of format "HH:MM" into struct_time. :param timestring: time string "HH:MM" """ - timestruct = time.strptime(timestring, "%H:%M") - return timestruct + structtime = time.strptime(timestring, "%H:%M") + return structtime def main(): @@ -97,7 +101,7 @@ def main(): conn = connect_database("database.db") while True: - id = request_id(conn) + id = get_data(conn) day = time.strftime("%A").lower() finish_time = select_final_time(conn, day, id) current_time = time.strftime("%H:%M")