Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON 1. (20 pts) Complete the class Date, which stores and manipulates dates.

ID: 3602632 • Letter: P

Question

PYTHON

1. (20 pts) Complete the class Date, which stores and manipulates dates. As specified below, write the required methods, including those needed for overloading operators. Exceptions messages should include the class and method names, and identify the error (including the value of all relevant arguments). Hint see the type_as_str function in the goody.py module. You may not import/use the datetime or other similar modules in Python. 1. The class is initialized with three int values (the year first, the month second, the day third.) If any parameter is not an int, or not in the correct range (the year must be 0 or greater, the month must be between 1 and 12 inclusive, and the day must be legal given the month and year: remember about leap years) raise an AssertionBrror with an appropriate string describing the problem/values. When initialized, the Date class should create exactly three self variables named year, month, and day (with these exact names and no others self variables).

Explanation / Answer

from goody import irange,type_as_str

class Date:

month_dict = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31,11:30, 12:31}

@staticmethod

def is_leap_year(year):

return (year%4 == 0 and year%100 != 0) or year%400 == 0

@staticmethod

def days_in(year,month):

return Date.month_dict[month] + (1 if month == 2 andDate.is_leap_year(year) else 0)

def __init__(self,year,month,day):

assert type(year) is int and year >= 0,'Date.__init__:year('+str(year)+') is not positive'

assert type(month) is int and 1 <= month <= 12, 'Date.__init__:month('+str(month)+') is not int in range [1,12]'

assert type(day) is int and 1 <= day <= Date.days_in(year,month),'Date.__init__: days('+str(day)+') is not correct for month('+str(month)+')/year('+str(year)+')'

self.year= year

self.month = month

self.day= day

def __getitem__(self,index):

if type(index) is not tuple:

index =(index,)

if not all(type(i) is str and i in ('y','m','d') for i in index):

raise IndexError('Date.__getitem__: illegal argument in'+str(index))

answer = tuple(self.year if i=='y' else self.month if i=='m' elseself.day for i in index)

if len(answer)==1:

answer = answer[0]

return answer

def __repr__(self):

return 'Date('+','.join(str(part) for part in self['y','m','d'])+')'