Implement extra (non-IB) subject support, update README.md and TODO.md

This commit is contained in:
Abdulkadir Furkan Şanlı
2019-01-06 10:04:44 +01:00
parent e8f51079d7
commit 1a309834af
3 changed files with 77 additions and 51 deletions

View File

@@ -31,34 +31,36 @@ def connect_database(file):
def get_data(conn):
"""
Requests student ID and returns integer tuple (id, class) with valid
student id and corresponding class no.
student id, corresponding class no. (1 or 2) and extra subject IDs.
:param conn: Connection object
"""
try:
id = int(input("Please enter student ID: "))
cur = conn.cursor()
cur.execute("SELECT id, student_class FROM students")
cur.execute("SELECT id, student_class, other FROM students")
ids = cur.fetchall()
for x in ids:
# ids is list with single value tuples
if id == x[0]:
# return tuple x with valid id and corresponding class no.
return x
for tup in ids:
# ids is list of tuples
if id == tup[0]:
# return tuple x with valid id, class and extra class IDs
lis = list(tup)
lis[2] = lis[2].split()
return lis
else:
raise ValueError
except ValueError:
print("Incorrect ID, try again.")
print("Invalid ID, try again.")
return get_data(conn)
def select_final_time(conn, day, stud):
def select_final_time(conn, day, stuple):
"""
Returns ending time of final lesson student must attend, or None.
:param conn: Connection object
:param day: current weekday, lowercase str
:param stud: tuple with student ID no. and class no., int
:param stuple: tuple with student ID no. and class no., int
"""
cur = conn.cursor()
cur.execute("""
@@ -70,19 +72,45 @@ def select_final_time(conn, day, stud):
OR g4 = lesson_id
OR g5 = lesson_id
OR g6 = lesson_id
OR lesson_id = 'gu'
OR lesson_id = 'tok'
WHERE id = ?
AND day = ?
AND lesson_class = ?
ORDER BY end_time DESC
LIMIT 1
""", (stud[0], day, stud[1]))
finish = cur.fetchone()
if str(type(finish)) == "<class 'tuple'>":
return finish[0]
""", (stuple[0], day, stuple[1]))
ib_finish = cur.fetchone()
if ib_finish != None:
ib_finish = parse_time_string(ib_finish[0])
others = []
for sub in stuple[2]:
cur.execute("""
SELECT end_time
FROM timetable
INNER JOIN students ON ? = lesson_id
WHERE id = ?
AND day = ?
AND lesson_class = ?
ORDER BY end_time DESC
LIMIT 1
""", (sub, stuple[0], day, stuple[1]))
other_final = cur.fetchone()
if other_final != None:
others.append(parse_time_string(other_final[0]))
if not others:
other_finish = None
else:
return finish
other_finish = max(others)
if ib_finish != None and other_finish != None:
if ib_finish > other_finish:
finish = ib_finish
else:
finish = other_finish
else:
finish = None
return finish
def parse_time_string(timestring):
@@ -101,19 +129,19 @@ def main():
conn = connect_database("database.db")
while True:
id = get_data(conn)
student_data = get_data(conn)
day = time.strftime("%A").lower()
finish_time = select_final_time(conn, day, id)
current_time = time.strftime("%H:%M")
finish_time = select_final_time(conn, day, student_data)
current_time = parse_time_string(time.strftime("%H:%M"))
if finish_time == None:
print("Student has no lessons today, clear to leave.")
elif parse_time_string(finish_time) < parse_time_string(current_time):
elif finish_time < 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":
if input("Enter y to run again.") not in ["y", "Y"]:
break