Often before the lecture starts, and occasionally in the break I like to display a timer with a countdown until the lectures starts/ resumes while playing some lofi-hip hop in the background. While the university students seems to enjoy this, I am ashamed of the code running the timer.
So my lecture schedules is as follows
Monday: 10:15 -- 12:00 Wednesday: 12:15 -- 14:00 Thursday: 08:15 -- 10:00
I am interested in finding when my next lecture is in iso format. I did this using the code below and it works fine. E.g if the current time is
16:00 on a Thursday, the output should be Monday and 10:15. I commented out the actual timer, as it is not important.
- Is there a better way / cleaner python code to obtain the date and time for my next lecture?
My attempt (which works, albeit a bit ugly) is as follows:
from datetime import datetime, timedelta from subprocess import call # Format (day, hour, minute) Lectures = [(0, 10, 15), (), (2, 12, 15), (3, 8, 15)] def get_next_lecture(now=datetime.now()): next_lecture = now.replace(minute=15, second=0, microsecond=0) # monday = 0, tuesday = 1, ... current_day = datetime.today().weekday() current_hour = now.hour lecture_day = current_day correct_day = False while not correct_day: # If the day is tuesday, increment to wednesday if current_day == 1: lecture_day = current_day + 1 lecture_hour = Lectures[lecture_day] lecture_minute = Lectures[lecture_day] now += timedelta(days=1) correct_day = True # if the day is friday, increment to monday elif current_day == 4: lecture_day = 0 lecture_hour = Lectures[lecture_day] lecture_minute = Lectures[lecture_day] now += datetime.today() + timedelta(days=3) correct_day = True else: # If it is not monday or friday, I have a lecture # checks if the lecture is in the future, if else increment day and try again if now.hour < Lectures[lecture_day]: if now.minute < Lectures[lecture_day]: lecture_hour = Lectures[lecture_day] lecture_minute = Lectures[lecture_day] correct_day = True else: current_day += 1 now += timedelta(days=1) else: current_day += 1 now += timedelta(days=1) next_lecture = now.replace( hour=lecture_hour, minute=lecture_minute, second=0, microsecond=0 ) return next_lecture def launch_timer(time): call(["termdown", time.isoformat()]) if __name__ == "__main__": next_lecture = get_next_lecture() print(lecture) # launch_timer(next_lecture)