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

Python question: Date Clients for the previous problem (ps11pr2) please refer to

ID: 3865710 • Letter: P

Question

Python question: Date Clients

for the previous problem (ps11pr2) please refer to

https://www.chegg.com/homework-help/questions-and-answers/methods-python-class-date-class-stores-manipulates-dates-represented-day-month-year-constr-q13439755

Thank you!

Now that you have written a functional Date class, we will put it to use! Remember that the Date class is only a blueprint, or template, for how Date objects should behave. We can now create Date objects according to that template and use them in client code.

Getting started
To start, open a new file in IDLE and save it as ps11pr3.py. Put all of the code that you write for this problem in this file. Don’t forget to include appropriate comments at the top of the file, and a docstring for your function.

IMPORTANT: Since your clients will need to construct Date objects, you need to import the Date class. Therefore, make sure that ps11pr3.py is in the same directory as ps11pr2.py, and include the following statement at the top of ps11pr3.py:

Your tasks

Write a function named get_age_on(birthday, other) that accepts two Date objects as parameters: one to represent a person’s birthday, and one to represent an arbitrary date. The function should then return the person’s age on that date as an integer.

Notes:

You can assume that the other parameter will represent a date on or after the birthday date.

It may be helpful to construct a new Date object that represents the person’s birthday in the year of other. That way, you can determine whether the person’s birthday has already passed in the year of other, and use that information to calculate the age.

Example:

Write a function print_birthdays(filename) that accepts a string filename as a parameter. The function should then open the file that corresponds to that filename, read through the file, and print some information derived from that file.

More specifically, the function should assume that the file in question contains information about birthdays in lines of the following format:

In other words, each line of the file contains comma-separated birthday data.

The function should read this file line-by-line, and print the person’s name, birthday, and the day of the week on which the person was born in the following format:

For example, the file birthdays.txt contains the following data:

Therefore, calling print_birthdays with this filename should print the following information:

Notes:

For full-credit, the format of the printed text should exactly match the formatting scheme described above, including spaces and parentheses.

You should process the text file using the line-by-line technique shown in lecture.

For every line of the file, you will need to create a Date object and invoke the appropriate methods on the object to get the information needed.

Originally, the components of the date that you obtain from the file will be in string form. You will need to convert them to integers before you pass them into the Dateconstructor, and you can use the int function for this purpose.

You can get a string representation of a Date object named d using the expression str(d).

Remember that you can concatenate strings together using the string concatentation operator (+). This operator will be helpful when you try to wrap parts of the output in parentheses. For example:

Explanation / Answer

import Date

def get_age_on(birthday, other):
    """takes two Date objects as parameters: one to represent
    a person’s birthday, and one to represent an arbitrary date
    then return the person’s age on that date as an integer
    """

    if birthday.__eq__(other) == True:
        return 1
    else:
        years = birthday.diff(other) // 365
        return years

def print_birthdays(filename):
    """takes a string filename as a parameter. The function should then open
    the file that corresponds to that filename, read through the file,
    and print some information derived from that file
    """
    file = open(filename, 'r')
    for line in file:
        line = line[:-1]
        elems = line.split(',')
        bday = int(elems[1])
        bmonth= int(elems[2])
        byear = int(elems[3])
        bdate = Date(bday, bmonth, byear)
        print(elems[0], '(' + str(bdate) + ')', '(' + str(bdate.day_of_week()) + ')', end=' ')

        file.close()

--------------------------------------------
Date.py
------------------------
class Date:
    """ A class that stores and manipulates dates that are
        represented by a day, month, and year.
    """

    # The constructor for the Date class.
    def __init__(self, new_month, new_day, new_year):
        """ The constructor for objects of type Date. """
        self.month = new_month
        self.day = new_day
        self.year = new_year

    # The function for the Date class that returns a Date
    # object in a string representation.
    def __repr__(self):
        """ This method returns a string representation for the
            object of type Date that it is called on (named self).
            ** Note that this _can_ be called explicitly, but
              it more often is used implicitly via printing or evaluating.
        """
        s = '%02d/%02d/%04d' % (self.month, self.day, self.year)
        return s

    def is_leap_year(self):
        """ Returns True if the called object is
            in a leap year. Otherwise, returns False.
        """
        if self.year % 400 == 0:
            return True
        elif self.year % 100 == 0:
            return False
        elif self.year % 4 == 0:
            return True
        return False

    def copy(self):
        """ Returns a new object with the same month, day, year
            as the called object (self).
        """
        new_date = Date(self.month, self.day, self.year)
        return new_date

    #### Put your code for problem 2 below. ####

    def tomorrow(self):
        """changes the called object so that it represents one calendar day after
        the date that it originally represented.
        """

        days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if self.is_leap_year() == True:
            days_in_month = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

        if self.month == 12 and self.day == 31:
            self.month = (self.month - self.month) + 1
            self.day = (self.day - self.day) + 1
            self.year += 1
        elif days_in_month[self.month] == self.day and self.month < 12:
            self.month = self.month + 1
            self.day = (self.day - self.day) + 1
            self.year = self.year
        else:
            self.month = self.month
            self.day += +1
            self.year = self.year

    def add_n_days(self, n):
        """ changes the calling object so that it represents n calendar days after the date it originally
        represented. Additionally, the method should print all of the dates from the starting date to the finishing date,
        inclusive of both endpoints.
        """

        if n == 0:
            print(self)

        else:
            print(self)
            for i in range(n):
                self.tomorrow()
                print(self)

    def __eq__(self, other):
        """returns True if the called object (self) and the argument (other) represent the same calendar date
        (i.e., if the have the same values for their day, month, and year attributes).
        Otherwise, this method should return False.
        """

        if self.day == other.day and self.month == other.month and self.year == other.year:
            return True
        else:
            return False

    def is_before(self, other):
        """returns True if the called object represents a calendar date that occurs before the calendar date
        that is represented by other. If self and other represent the same day, or if self occurs after other,
        the method should return False.
        """

        if self.__eq__(other) == True:
            return False
        elif self.year < other.year:
            return True
        elif self.year == other.year and self.month < other.month:
            return True
        elif self.year == other.year and self.month == other.month and self.day < other.day:
            return True
        else:
            return False

    def is_after(self, other):
        """returns True if the calling object represents a calendar date that occurs after the calendar date that
        is represented by other. If self and other represent the same day, or if self occurs before other,
        the method should return False.
        """

        if self.__eq__(other) == True:
            return False
        elif self.is_before(other) == True:
            return False
        elif self.is_before(other) == False:
            return True

    def diff(self, other):
        """returns an integer that represents the number of days between self and other.
        """

        d1 = self.copy()
        d2 = other.copy()
        day_diff = 0

        if self.__eq__(other) == True:
            return 0
        else:
            if self.is_before(other) == True:
                while (d1.__eq__(other) != True):
                    day_diff = day_diff + 1
                    d1.tomorrow()
                if self.is_before(other) == True:
                    return day_diff
                else:
                    return day_diff * -1

            elif self.is_after(other) == True:
                while (d2.__eq__(self) != True):
                    day_diff = day_diff + 1
                    d2.tomorrow()

                if self.is_before(other) == True:
                    return day_diff
                else:
                    return day_diff * -1

    def day_of_week(self):
        """eturns a string that indicates the day of the week of the Date object that
        calls it. In other words, the method should return one of the following
        strings: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'.
        """

        day_of_week_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
                             'Friday', 'Saturday', 'Sunday']
        reference = Date(11, 14, 2016)
        day = day_of_week_names[reference.diff(self) % 7]
        return day

--------------------------------------------

birthdays.txt

-------------------------------