Answer To: COMP2152 Assignment 2 (Group Assignment) Due: April 18, 2021, 11:59 PM Late penalty: 10% per day of...
Pulkit answered on Apr 18 2021
81015_python/Calendar.py
import os
'''
IMPORTANT NOTE: Do NOT change any of the function names or their signatures
(the parameters they take).
Your functions must behave exactly as described. Please check correctness by
running DocTests included in function headers. You may not use any print or
input statements in your code.
Manage a calendar database.
A calendar is a dictionary keyed by date ("YYYY-MM-DD") with value being a list
of strings, the events on the specified date.
'''
# -----------------------------------------------------------------------------
# Please implement the following calendar commands
# -----------------------------------------------------------------------------
def command_help():
"""
() -> str
This function is already implemented. Please do not change it.
Returns a help message for the system. That is...
"""
help_me = """
Help for Calendar. The calendar commands are
add DATE START END DETAILS add the event DETAILS at the specified DATE with specific START and END time
show show all events in the calendar
delete DATE NUMBER delete the specified event (by NUMBER) from
the calendar
quit quit this program
help display this help message
Examples: user data follows command:
command: add 2018-10-12 18 19 dinner with jane
success
command: show
2018-10-12 :
start : 08:00,
end : 09:00,
title : Eye doctor
start : 12:30,
end : 13:00,
title : lunch with sid
start : 18:00,
end : 19:00,
title : dinner with jane
2018-10-29 :
start : 10:00,
end : 11:00,
title : Change oil in blue car
start : 12:00,
end : 14:00,
title : Fix tree near front walkway
start : 18:00,
end : 19:00,
title : Get salad stuff, leuttice, red peppers, green peppers
2018-11-06 :
start : 18:00,
end : 22:00,
title : Sid's birthday
command: delete 2018-10-29 10
deleted
A DATE has the form YYYY-MM-DD, for example
2018-12-21
2016-01-02
START and END has a format HH where HH is an hour in 24h format, for example
09
21
Event DETAILS consist of alphabetic characters,
no tabs or newlines allowed.
"""
return help_me
def command_add(date, start_time, end_time, title, calendar):
"""
(str, int, int, str, dict) -> boolean
Add title to the list at calendar[date]
Create date if it was not there
Adds the date if start_time is less or equal to the end_time
date: A string date formatted as "YYYY-MM-DD"
start_time: An integer from 0-23 representing the start time
end_time: An integer from 0-23 representing the start time
title: A string describing the event
calendar: The calendar database
return: boolean of whether the even was successfully added
>>> calendar = {}
>>> command_add("2018-02-28", 11, 12, "Python class", calendar)
True
>>> calendar == {"2018-02-28": [{"start": 11, "end": 12, "title": "Python class"}]}
True
>>> command_add("2018-03-11", 14, 16, "CSCA08 test 2", calendar)
True
>>> calendar == {"2018-03-11": [{"start": 14, "end": 16, "title": "CSCA08 test 2"}], \
"2018-02-28": [{"start": 11, "end": 12, "title": "Python class"}]}
True
>>> command_add("2018-03-11", 10, 9, "go out with friends after test", calendar)
False
"""
if start_time > end_time:
return False
# Add the date with an empty list as its value if it is not already in the database and start time is less than end time
if date not in calendar and start_time <= end_time:
calendar[date] = []
# Add the title to the list at calendar[date] with the start and end time
calendar[date].append({"start":start_time, "end": end_time, "title": title})
return True
def command_show(calendar):
r"""
(dict) -> str
Returns the list of events for calendar sorted in decreasing date order
and increasing time order within the date
as a string, see examples below for a sample formatting
calendar: the database of events
Example:
>>> calendar = {}
>>> command_add("2018-01-15", 11, 13, "Eye doctor", calendar)
True
>>> command_add("2018-01-15", 8, 9, "lunch with sid", calendar)
True
>>> command_add("2018-02-10", 12, 23, "Change oil in blue car", calendar)
True
>>> command_add("2018-02-10", 20, 22, "dinner with Jane", calendar)
True
>>> command_add("2017-12-22", 5, 8, "Fix tree near front walkway", calendar)
True
>>> command_add("2017-12-22", 13, 15, "Get salad stuff", calendar)
True
>>> command_add("2018-05-06", 19, 23, "Sid's birthday", calendar)
True
>>> command_show(calendar)
"\n2018-05-06 : \n start : 19:00,\n end : 23:00,\n title : Sid's birthday\n2018-02-10 : \n start : 12:00,\n end : 23:00,\n title : Change oil in blue car\n\n start : 20:00,\n end : 22:00,\n title : dinner with Jane\n2018-01-15 : \n start : 08:00,\n end : 09:00,\n title : lunch with sid\n\n start : 11:00,\n end : 13:00,\n title : Eye doctor\n2017-12-22 : \n start : 05:00,\n end : 08:00,\n title : Fix tree near front walkway\n\n start : 13:00,\n end : 15:00,\n title : Get salad stuff"
"""
for dt in sorted (calendar, reverse = True):
print (dt)
lst = calendar[dt]
lst = sorted (lst, key = lambda x:x["start"])
for appt in lst:
s = " start: " + str (appt["start"]) + ":00 "+"\n" + " end: " + str (appt["end"]) + ":00,\n"
s += " title: " + appt["title"] + "\n"
return s
def command_delete(date, start_time, calendar):
"""
(str, int, dict) -> str
Delete the entry at calendar[date][start_time]
If calendar[date] is empty, remove this date from the calendar.
If the entry does not exist, do nothing
date: A string date formatted as "YYYY-MM-DD"
start_time: An...