import calendar
import matplotlib.pyplot as plt
from enum import Enum
# enums for months and days
class Months(Enum):
January = 1
February = 2
March = 3
April = 4
May = 5
June = 6
July = 7
August = 8
September = 9
October = 10
November = 11
December = 12
class Days(Enum):
Sun = 0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
calendar.setfirstweekday(6)
class MyCalendar():
def __init__(self,year,month):
# initiate calendar with years
self.year = year
self.month = month
self.cal = calendar.monthcalendar(year,month)
# store events in the calendar in same format as calendar
self.events = [[[] for day in week] for week in self.cal]
def index_monthday(self,day):
# index the day in the list of lists
for week_n, week in enumerate(self.cal):
try:
return week_n, week.index(day)
except ValueError:
pass
raise ValueError('There are not {} days in this month'.format(day))
def add_event(self, day, event_str):
# add event to the calendar
week, w_day = self.index_monthday(day)
self.events[week][w_day].append(event_str)
def show(self):
#display the calendar
f, axs = plt.subplots(len(self.cal), 7, sharex = True, sharey = True, figsize = (20,15))
for week, row in enumerate(axs):
for week_day, ax in enumerate(row):
ax.set_xticks([])
ax.set_yticks([])
if self.cal[week][week_day] != 0:
ax.text(0.02, 0.98, str(self.cal[week][week_day]),va = 'top', ha = 'left')
events = "\n".join(self.events[week][week_day])
ax.text(0.03, 0.85, events,va = 'top', ha = 'left', fontsize = 10)
for day in Days:
axs[0][day.value].set_title(day.name, fontsize = 10)
f.subplots_adjust(hspace=0)
f.subplots_adjust(wspace=0)
f.suptitle(Months(self.month).name + ' ' + str(self.year),fontsize=20, fontweight='bold')
plt.show()
yearcalendar = []
year = int(input('What year would you like to use for your calendar? '))
for x in range(1,13):
cal = MyCalendar(year, x)
yearcalendar.append(cal)
active = True
# functions for actions to perform on the calculator
# schedule recurring events
def schedulerecur(yearcalendar, month, event, is_time, time = None):
freq = input('Would you like to schedule this event daily(enter d) or weekly(enter w)? ')
if freq == 'd':
for week in yearcalendar[month-1].cal:
for day in week:
if day == 0:
pass
else:
if is_time == 'y':
yearcalendar[month - 1].add_event(day, event + ' @ ' + time)
else:
yearcalendar[month - 1].add_event(day, event)
elif freq == 'w':
fweek = ['s','m','t','w','th','f','sa']
fday = input('What day of the week would you like to schedule this event for(su,m,t,w,th,f,sa)?')
numday = fweek.index(fday)
for week in yearcalendar[month-1].cal:
if week[numday] == 0:
pass
else:
if is_time == 'y':
yearcalendar[month - 1].add_event(week[numday], event + ' @ ' + time)
else:
yearcalendar[month - 1].add_event(week[numday], event)
# schedule events
def schedule(yearcalendar):
scheduling = True
while scheduling:
recur = input('Would you like to schedule this as a recurring event (y or n)? ')
month = int(input('What month would you like to schedule an event for? '))
if recur == 'n':
day = int(input('What day would you like to schedule and event for? '))
is_time = input('Would you like to add a time to your event (y or n)? ')
event = input('What is the event? ')
# schedule timed events
if is_time == 'y':
time = input('What time is the event? ')
# schedule recurring timed events
if recur == 'y':
schedulerecur(yearcalendar, month, event, is_time, time)
else:
yearcalendar[month - 1].add_event(day, event + ' @ ' + time)
# schedule untimed events
else:
# schedule recurring untimed events
if recur == 'y':
schedulerecur(yearcalendar, month, event, is_time)
else:
yearcalendar[month - 1].add_event(day, event)
cont_sched = input('Would you like to continue scheduling (y or n)? ')
if cont_sched == 'n':
scheduling = False
else:
pass
# search for recurring events
def searchrecur(yearcalendar, searchevent):
freq = input('At what frequency does this event occur? Enter d(daily) or w(weekly) ')
if freq == 'd':
for calend in yearcalendar:
for week in calend.events:
for event in week:
if searchevent == ''.join(event):
found = True
dmonth = calend.month
if found:
print('Event Found everyday in ' + Months(dmonth).name)
elif not found:
print('Could not find event in calendar')
elif freq == 'w':
for calend in yearcalendar:
for week in calend.events:
for event in week:
if searchevent == ''.join(event):
found = True
dmonth = calend.month
if found:
print('Event Found weekly in ' + Months(dmonth).name)
elif not found:
print('Could not find event in calendar')
# search for events in the calendar
def search(yearcalendar):
searching = True
while searching:
found = False
search_is_time = input('Is there a time associated with the event that your are searching for (y or n)? ')
recur_search = input('Are you searching for a recurring event (y or n)?')
searchevent = input('What event would you like to search for? ')
if search_is_time == 'y':
stime = input('What time is the event? ')
searchevent = searchevent + ' @ ' + stime
#search for recurring events
if recur_search == 'y':
searchrecur(yearcalendar, searchevent)
#search for the events
else:
for calend in yearcalendar:
for week in calend.events:
weeknum = calend.events.index(week)
for event in week:
daynum = week.index(event)
if searchevent == ''.join(event):
month = calend.month
found = True
print('Event Found on ' + Months(month).name + ' ' + str(calend.cal[weeknum][daynum]))
if not found:
print('Could not find event in calendar')
cont_sear = input('Would you like to continue searching (y or n)? ')
if cont_sear == 'n':
searching = False
else:
pass
# browse the calendar
def browse(yearcalendar):
for cal in yearcalendar:
cal.show()
while active:
action = input('What would you like to do with your calender? Enter s for schedule, se for search, or b for browse ')
if action == 's':
schedule(yearcalendar)
elif action == 'se':
search(yearcalendar)
elif action == 'b':
browse(yearcalendar)
cont_act = input('Would you like to perform another action on your calendar (y or n)? ')
if cont_act == 'n':
active = False
else:
pass
What year would you like to use for your calendar? 2022 What would you like to do with your calender? Enter s for schedule, se for search, or b for browse s Would you like to schedule this as a recurring event (y or n)? y What month would you like to schedule an event for? 3 Would you like to add a time to your event (y or n)? n What is the event? Workout Would you like to schedule this event daily(enter d) or weekly(enter w)? d Would you like to continue scheduling (y or n)? y Would you like to schedule this as a recurring event (y or n)? y What month would you like to schedule an event for? 5 Would you like to add a time to your event (y or n)? y What is the event? Python Class What time is the event? 3 Would you like to schedule this event daily(enter d) or weekly(enter w)? w What day of the week would you like to schedule this event for(su,m,t,w,th,f,sa)?w Would you like to continue scheduling (y or n)? y Would you like to schedule this as a recurring event (y or n)? n What month would you like to schedule an event for? 2 What day would you like to schedule and event for? 28 Would you like to add a time to your event (y or n)? n What is the event? My Birthday Would you like to continue scheduling (y or n)? y Would you like to schedule this as a recurring event (y or n)? n What month would you like to schedule an event for? 12 What day would you like to schedule and event for? 25 Would you like to add a time to your event (y or n)? n What is the event? Christmas Would you like to continue scheduling (y or n)? n Would you like to perform another action on your calendar (y or n)? y What would you like to do with your calender? Enter s for schedule, se for search, or b for browse se Is there a time associated with the event that your are searching for (y or n)? y Are you searching for a recurring event (y or n)?y What event would you like to search for? Python Class What time is the event? 3 At what frequency does this event occur? Enter d(daily) or w(weekly) w Event Found weekly in May Would you like to continue searching (y or n)? y Is there a time associated with the event that your are searching for (y or n)? n Are you searching for a recurring event (y or n)?n What event would you like to search for? Christmas Event Found on December 25 Would you like to continue searching (y or n)? n Would you like to perform another action on your calendar (y or n)? y What would you like to do with your calender? Enter s for schedule, se for search, or b for browse b