PYTHON LANGUAGE Your assignment is to write the following ma

PYTHON LANGUAGE

Your assignment is to write the following magic methods for the Date class. Each method must be written in the order asked. Please complete each method to complete the Date class.

1. __eq__ returns True if two Date objects have the same month, day, and year.

2. __add__ is passed an int as a parameter, and adds that number of days to the Date. It is

nondestructive. For example:

3. __getitem__ is passed either an int between 0 and 2, or a str ‘month’, ‘day’, or ‘year’. It returns the appropriate instance variable from the Date object, or raises an exception otherwise. For example:

4. __setitem__ is passed an index (as described in problem 4) and an integer, and sets the month, day, or year of the Date object to be that integer. If the index is not of the correct type or value, then an exception is raised. For example:

Your will receive extra credit if you correctly implement the method below. A correctly implemented version is worth .5 points extra credit.

5. __radd__ is passed an integer, and modifies the Date object to be the specified number of days into the future. It is destructive. For example:

Solution

class Date:

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

self.month = month

self.day = day

self.year = year

self._dict = {}

self._dict[1] = self.day

self._dict[\'day\'] = self.day

  

self._dict[0] = self.month

self._dict[\'month\'] = self.month

  

self._dict[2] = self.year

self._dict[\'year\'] = self.year

def _is_leap_year(self, year):

if (year % 4) == 0:

if (year % 100) == 0:

if (year % 400) == 0:

return True

else:

return False

else:

return True

else:

return False

def _get_last_day(self, month, year):

if month == 2:

if self._is_leap_year(year):

return 29

else:

return 28

elif month in [1,3,5,7,8,10,12]:

return 31

else:

return 30

def _add_days(self, days, day, month, year):

last_date = self._get_last_day(month, year)

if day + days > last_date :

days_remaining = days -(last_date-day) - 1

if month == 12:

return self._add_days(days_remaining, 1, 1, year+1)

else:

return self._add_days(days_remaining, 1, month+1, year)

else:

return (day+days, month, year)

def __eq__(self, date):

return (self.month == date.month) and (self.day == date.day) and (self.year == date.year)

def __add__(self, days):

(day, month, year) = self._add_days(days, self.day, self.month, self.year)

return Date(month, day, year)

def __radd__(self, days):

(day, month, year) = self._add_days(days, self.day, self.month, self.year)

self._setItem(1, day)

self._setItem(0, month)

self._setItem(2, year)

def _setItem(self,item_type, item_value):

if item_type == 1 or item_type == \'day\':

self._dict[1] = item_value

self._dict[\'day\'] = item_value

self.day = item_value

elif item_type == 0 or item_type == \'month\':

self._dict[0] = item_value

self._dict[\'month\'] = item_value

self.month = item_value

elif item_type == 2 or item_type == \'year\':

self._dict[2] = item_value

self._dict[\'year\'] = item_value

self.year = item_value

def __getitem__(self, item_type):

if item_type in self._dict:

return self._dict[item_type]

else:

raise IndexError(item_type)

def __setitem__(self, item_type, item_value):

if item_type in self._dict:

self._setItem(item_type, item_value)

else:

raise IndexError(item_type)

def __repr__(self):

return \"Date(\"+str(self.month) + \",\" + str(self.day) + \",\" + str(self.year) + \")\"

# hope chegg maintain indentation.

# else use this link http://pastebin.com/pZKTjDm7

PYTHON LANGUAGE Your assignment is to write the following magic methods for the Date class. Each method must be written in the order asked. Please complete each
PYTHON LANGUAGE Your assignment is to write the following magic methods for the Date class. Each method must be written in the order asked. Please complete each
PYTHON LANGUAGE Your assignment is to write the following magic methods for the Date class. Each method must be written in the order asked. Please complete each

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site