1+ from datetime import datetime , timedelta
2+
3+ def get_first_day_of_week_after_date (start_date , day_of_week ):
4+ #weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
5+ current_date = start_date
6+ while current_date .strftime ("%A" ).lower () != day_of_week .lower ():
7+ current_date += timedelta (days = 1 )
8+ return current_date
9+
10+ def get_all_dates (start_date , end_date , delta_days = 7 ):
11+ all_dates = []
12+ current_date = start_date
13+ while current_date <= end_date :
14+ all_dates .append (current_date )
15+ current_date += timedelta (days = delta_days )
16+ return all_dates
17+
18+ class OfficeHoursScheduler :
19+ def __init__ (self , semester_start_date , semester_end_date , ta_schedule ):
20+ self .semester_start_date = semester_start_date
21+ self .semester_end_date = semester_end_date
22+ self .office_hours = {} #self.office_hours[person_id][date] = []
23+ """
24+ Matt:{Jan 10th:[ {10am - 11am},{3pm - 4pm} ]
25+ max:{Jan 11th: [ {11am - 12pm}], Jan 18th: [ {11am - 12pm} ], Jan 25th: [ {11am - 12pm} ]
26+ """
27+
28+ #add_single_office_hour
29+ def add_single_office_hour (self , person_id , date , start_time , end_time , additional = False ):
30+ if person_id not in self .office_hours :
31+ self .office_hours [person_id ] = {}
32+ if date not in self .office_hours [person_id ]:
33+ self .office_hours [person_id ][date ] = []
34+ # Check for duplicates before adding
35+ current_office_hours = self .office_hours [person_id ][date ]
36+ if {'start_time' : start_time , 'end_time' : end_time } not in current_office_hours :
37+ self .office_hours [person_id ][date ].append ({'start_time' : start_time , 'end_time' : end_time })
38+ print (f"Added specific office hour for person { person_id } on { date } from { start_time } to { end_time } " )
39+ if additional :
40+ pass #add to additional_office_hours list
41+ else :
42+ print (f"Office hour for person { person_id } on { date } at { start_time } to { end_time } already exists" )
43+
44+ #add_remaining_office_hours
45+ def add_remaining_office_hours (self , person_id , start_date , day_of_week , start_time , end_time ):
46+ date_of_first_office_hours = get_first_day_of_week_after_date (start_date , day_of_week )
47+ for date in get_all_dates (date_of_first_office_hours , self .semester_end_date ):
48+ self .add_single_office_hour (person_id , date , start_time , end_time )
49+
50+ def add_office_hours (self , person_id , day_of_week , start_time , end_time ):
51+ self .add_remaining_office_hours (person_id , self .semester_start_date , day_of_week , start_time , end_time )
52+
53+ #modify_single_office_hour
54+ def modify_single_office_hour (self , person_id , old_date , old_start_time , old_end_time , new_date , new_start_time , new_end_time ):
55+ # Check if person_id exists and has office hours
56+ if person_id in self .office_hours and old_date in self .office_hours [person_id ]:
57+ current_office_hours = self .office_hours [person_id ][old_date ]
58+ for index , office_hour in enumerate (current_office_hours ):
59+ if office_hour ['start_time' ] == old_start_time and office_hour ['end_time' ] == old_end_time :
60+ # Modify the office hour to new_date, new_start_time, new_end_time
61+ self .office_hours [person_id ][old_date ][index ] = {'start_time' : new_start_time , 'end_time' : new_end_time }
62+ # Remove the old office hour if new date is different
63+ if old_date != new_date :
64+ self .office_hours [person_id ][new_date ] = self .office_hours [person_id ].get (new_date , [])
65+ self .office_hours [person_id ][new_date ].append ({'start_time' : new_start_time , 'end_time' : new_end_time })
66+ del self .office_hours [person_id ][old_date ][index ]
67+ if not self .office_hours [person_id ][old_date ]:
68+ del self .office_hours [person_id ][old_date ]
69+ return True # Successfully modified the office hour
70+ return False # Unable to modify the office hour
71+
72+ #modify_remaining_office_hours
73+ #
74+ """
75+ def modify_remaining_office_hours(modify_single_office_hour(self, person_id, start_date, old_start_time, old_end_time, new_day_of_week, new_start_time, new_end_time))
76+ date_of_first_office_hours = get_first_day_of_week_after_date(start_date, old_day_of_week)
77+ for date in get_all_dates(date_of_first_office_hours, self.semester_end_date):
78+ self.modify_single_office_hour(person_id, date, old_start_time, old_end_time, new_date, new_start_time, new_end_time):
79+ """
80+ #modify_office_hours
81+
82+ #remove_single_office_hour
83+ def remove_single_office_hour (self , person_id , date , start_time , end_time ):
84+ if person_id in self .office_hours and date in self .office_hours [person_id ]:
85+ current_office_hours = self .office_hours [person_id ][date ]
86+ for index , office_hour in enumerate (current_office_hours ):
87+ if office_hour ['start_time' ] == start_time and office_hour ['end_time' ] == end_time :
88+ del self .office_hours [person_id ][date ][index ]
89+ if not self .office_hours [person_id ][date ]:
90+ del self .office_hours [person_id ][date ]
91+ return True # Successfully removed the office hour
92+ return False # Unable to remove the office hour
93+
94+ #remove_remaining_office_hours
95+ def remove_remaining_office_hours (self , person_id , start_date , day_of_week , start_time , end_time ):
96+ current_date = start_date
97+ while current_date <= self .semester_end_date :
98+ if current_date .weekday () == day_of_week :
99+ self .remove_single_office_hour (person_id , current_date , start_time , end_time )
100+ current_date += timedelta (days = 1 )
101+ #remove_office_hours # self, person_id, date, start_time, end_time, additional=False):
102+ def remove_office_hours (self , person_id , day_of_week , start_time , end_time ):
103+ self .remove_remaining_office_hours ( person_id , self .semester_start , day_of_week , start_time , end_time )
104+ def pretty_print_office_hours (self ):
105+ for person_id , person_schedule in self .office_hours .items ():
106+ print (person_schedule )
107+ for date , office_hours in person_schedule .items ():
108+ print (f"\t Date: { date .strftime ('%Y-%m-%d' )} " )
109+ print ("\t Office Hours:" )
110+ for hour in office_hours :
111+ print (f"\t \t Start Time: { hour ['start_time' ]} - End Time: { hour ['end_time' ]} " )
112+
113+ # Example usage:
114+ semester_start = datetime (2024 , 1 , 3 )
115+ semester_end = datetime (2024 , 5 , 31 )
116+ ta_schedule = {'day_of_week' : 'Monday' , 'start_time' : '09:00 AM' , 'end_time' : '11:00 AM' }
117+
118+ scheduler = OfficeHoursScheduler (semester_start , semester_end , ta_schedule )
119+
120+ #Assignment Example:
121+ scheduler .add_office_hours ('Matt' , 'Thursday' , '10:00 AM' , '11:00 AM' )
122+
123+ try :
124+ scheduler .add_single_office_hour ('Max' , None , '11:00 AM' , '12:00 PM' )
125+ scheduler .add_single_office_hour ('Max' , None , '11:00 AM' , '12:00 PM' )
126+ scheduler .add_single_office_hour ('Max' , None , '11:00 AM' , '12:00 PM' )
127+ except :
128+ print ("Classic None" )
129+
130+ # try:
131+ # scheduler.add_single_office_hour('Max', datetime(0, 0, 0), '11:00 AM', '12:00 PM')
132+ # scheduler.add_single_office_hour('Max', datetime(0, 0, 0), '11:00 AM', '12:00 PM')
133+ # scheduler.add_single_office_hour('Max', datetime(0, 0, 0), '11:00 AM', '12:00 PM')
134+ # except:
135+ # print("Classic None")
136+
137+ # scheduler.modify_single_office_hour('Matt', datetime(2024, 1, 4), '10:00 AM', '11:00 AM', datetime(2024, 1, 5), '6:00 PM', '7:00 PM')
138+
139+
140+ print (scheduler .pretty_print_office_hours ())
141+ #scheduler.add_office_hour(1, datetime(2024, 1, 10), '09:00 AM', '11:00 AM') #next Wednesday
142+ #scheduler.add_office_hour(1, datetime(2024, 1, 12), '10:00 AM', '12:00 PM') #next Fridat
143+ #scheduler.modify_office_hour(1, datetime(2024, 1, 10), '10:00 AM', '12:00 PM')
144+ #scheduler.remove_office_hour(1, datetime(2024, 1, 10))
145+ #scheduler.remove_remaining_office_hours(1, datetime(2024, 1, 10))
146+ #scheduler.modify_remaining_office_hours(1, datetime(2024, 1, 10), '01:00 PM', '03:00 PM')
0 commit comments