This repository has been archived on 2023-12-11. You can view files and clone it, but cannot push or open issues or pull requests.
ib-clearance/ib-clearance.py
Abdulkadir Furkan Şanlı 42e6042405 Intuitivize exit sequence.
2019-01-11 12:53:06 +01:00

167 lines
4.7 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# Copyright 2018 Abdulkadir Furkan Şanlı
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sqlite3
import time
from os import name
# Import getch from msvcrt if running on Windows.
if name == "nt":
from msvcrt import getch
else:
from getch import getch
def connect_database(file):
"""
Creates and returns Connection object.
:param file: database file
"""
conn = sqlite3.connect(file)
return conn
def get_data(conn):
"""
Requests student ID and returns integer tuple (id, class) with valid
student id, corresponding class no. (1 or 2) and extra subject IDs.
:param conn: Connection object
"""
try:
id = int(input("\nPlease enter student ID: "))
cur = conn.cursor()
cur.execute("SELECT id, student_class, other FROM students")
ids = cur.fetchall()
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("\nInvalid ID, try again.")
return get_data(conn)
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 stuple: tuple with student ID no. and class no., int
"""
cur = conn.cursor()
cur.execute("""
SELECT end_time
FROM timetable
INNER JOIN students ON g1 = lesson_id
OR g2 = lesson_id
OR g3 = lesson_id
OR g4 = lesson_id
OR g5 = lesson_id
OR g6 = lesson_id
WHERE id = ?
AND day = ?
AND lesson_class = ?
ORDER BY end_time DESC
LIMIT 1
""", (stuple[0], day, stuple[1]))
ib_finish = cur.fetchone()
if ib_finish is not 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_finish = cur.fetchone()
if other_finish is not None:
others.append(parse_time_string(other_finish[0]))
if not others:
others_finish = None
else:
others_finish = max(others)
if ib_finish is not None and others_finish is not None:
if ib_finish > others_finish:
finish = ib_finish
else:
finish = others_finish
elif ib_finish is not None:
finish = ib_finish
elif others_finish is not None:
finish = others_finish
else:
finish = None
return finish
def parse_time_string(timestring):
"""
Parses given 24h time string of format "HH:MM" into struct_time.
:param timestring: time string "HH:MM"
"""
structtime = time.strptime(timestring, "%H:%M")
return structtime
def main():
print("ib-clearance")
print("============")
conn = connect_database("database.db")
while True:
student_data = get_data(conn)
day = time.strftime("%A").lower()
finish_time = select_final_time(conn, day, student_data)
current_time = parse_time_string(time.strftime("%H:%M"))
if finish_time == None:
print("\nStudent has no lessons today, clear to leave.")
elif finish_time < current_time:
print("\nStudent has finished for today, clear to leave.")
else:
print("\nStudent still has lessons, clearance not granted.")
print("\nPress X key to exit, other key to run again.")
key = getch()
# msvcrt.getch returns type bytes, decode into str.
if isinstance(key, bytes):
key = key.decode()
if key.upper() == "X":
break
# Run main() if script is run standalone.
if __name__ == "__main__":
main()