From 14a25827a09087dd101f7f98162572b5f2bfecd8 Mon Sep 17 00:00:00 2001 From: phowell Date: Tue, 27 Aug 2024 10:41:00 -0700 Subject: [PATCH] schedule manipulations --- schedule.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 schedule.py diff --git a/schedule.py b/schedule.py new file mode 100644 index 0000000..51dc8a6 --- /dev/null +++ b/schedule.py @@ -0,0 +1,103 @@ +# schedule.py +# +# experimenting with manipulating and querying the schedule of courses + + +import json, re +from datetime import datetime + +course_types = {'in-person':'IP','hybrid':'H','online':'O','online live':'OL'} + +def course_to_string(crs): + # crn type days start end cred code name teacher + lengths = [5, 3, 5, 6, 6, 4, 4, 13, 35, 25] + items = [ crs[x] for x in 'crn,type,days,time_start,time_end,cred,act,code,name,teacher'.split(',')] + items[1] = course_types[ items[1] ] + items[5] = items[5][0:3] + result = " ".join(f"{str(val):{width}}" for val, width in zip(items, lengths)) + return result + + +def parse_days(str_days): + # return a list with actual day of week names + days = [] + if 'M' in str_days: + days.append('monday') + if 'T' in str_days: + days.append('tuesday') + if 'W' in str_days: + days.append('wednesday') + if 'R' in str_days: + days.append('thursday') + if 'F' in str_days: + days.append('friday') + if 'S' in str_days: + days.append('saturday') + return days + +def parse_courses(filename): + with open(filename) as f: + courses = json.load(f) + + locations, teachers, days, now = {}, {}, {}, datetime.now() + weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] + + for course in courses: + #print(course) + + # Add course to teachers dict + course['teacher'] = re.sub(r'\s+', ' ', course['teacher']) + teachers.setdefault(course['teacher'], []).append(course) + + if course['type'] != 'in-person': continue + # Add course to locations dict + locations.setdefault(course['loc'], []).append(course) + + # Add course to days dict + for day in parse_days(course['days']): + days.setdefault(day, []).append(course) + + # Check if course is happening now + if course['time_start'] == '': continue + if course['time_end'] == '': continue + start_time = datetime.strptime(course['time_start'], '%H:%M') + end_time = datetime.strptime(course['time_end'], '%H:%M') + #if start_time.time() <= now.time() <= end_time.time(): + # print(f"{course['code']} is happening now in {course['loc']}") + + return locations, teachers, days + +# Example usage +locations, teachers, days = parse_courses('cache/sample_semester.json') + +#print(json.dumps(locations,indent=2)) +#print(json.dumps(teachers,indent=2)) +#print(json.dumps(days,indent=2)) + +for L,C in locations.items(): + print(f"\n{L}") + for crs in C: + print(" " + course_to_string(crs)) + +for T,C in teachers.items(): + print(f"\n{T}") + for crs in C: + print(" " + course_to_string(crs)) + +# EXAMPLES +# +print("\n\n") + +# Get schedule for a specific room on a specific day +room = 'HU 104' +day = 'monday' +print(f"\nSchedule for {room} on {day}:") +for course in locations[room]: + if day in parse_days(course['days']): + print(" " + course_to_string(course)) + +# Get weekly schedule for a specific teacher +teacher = 'Kimberly J Smith' +print(f"\nWeekly schedule for {teacher}:") +for course in teachers[teacher]: + print(" " + course_to_string(course)) \ No newline at end of file