If you have the textbook read Chapter 10, Classes and Object-Oriented Programming sections 10.3 and 10.4, Working with Instances and Techniques for Designing Classes.
I have posted homework 8 here.
It is due this coming Sunday at 11:59 PM.
Let's review the answers to Quiz 6.
YYYY-MM-DD
MONTH_NAME DAY_OF_MONTH, YEAR
#! /usr/bin/python3 # code to convert a date in the format # YYYY-MM-DD # stored as the first line in a file whose name # is stored in the variable DATA_FILENAME # into a string in the format # MONTH_NAME DAY_OF_MONTH, YEAR DATA_FILENAME = "data.txt" data_file = open(DATA_FILENAME, "r") # open the file for reading date_string = data_file.readline() year, month, day = date_string.strip().split("-") year, month, day = int(year), int(month), int(day) month_names = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"November", 12:"December"} new_date_string = month_names[month] + " " + str(day) + ", " + str(year) print(new_date_string)
# opens a file and returns the first line in # that file without the newline def get_first_line(filename): data_file = open(filename, "r") date_string = data_file.readline() data_file.close() return date_string.strip()
# turns a string with a date in the format # YYYY-MM-DD # into integer values for year, month and date def get_date(date_string): year, month, day = date_string.split("-") return int(year), int(month), int(day) # converts a month number into a month name def get_month_name(month_number): month_names = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"November", 12:"December"} return month_names[month_number] # returns a date in standard format def format_date(year, month, day): return get_month_name(month) + " " + str(day) + ", " + str(year)
#! /usr/bin/python3
import date_functions
# code to convert a date in the format
# YYYY-MM-DD
# stored as the first line in a file whose name
# is stored in the variable DATA_FILENAME
# into a string in the format
# MONTH_NAME DAY_OF_MONTH, YEAR
# opens a file and returns the first line in
# that file without the newline
def get_first_line(filename):
data_file = open(filename, "r")
date_string = data_file.readline()
data_file.close()
return date_string.strip()
DATA_FILENAME = "data.txt"
date_string = get_first_line(DATA_FILENAME)
year, month, day = date_functions.py.get_date(date_string)
print(date_functions.py.format_date(year, month, day))
def __init__(self, date_string): self.year, self.month, self.day = date_string.split("-") self.year = int(self.year) self.month = int(self.month) self.day = int(self.day)
# converts a month number into a month name def get_month_name(self): month_names = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"November", 12:"December"} return month_names[self.month]
# returns a date in standard format def format_date(self): return self.get_month_name() + " " + str(self.day) + ", " + str(self.year)
date = Date(date_string)
#! /usr/bin/python3 # code to convert a date in the format # YYYY-MM-DD # stored as the first line in a file whose name # is stored in the variable DATA_FILENAME # into a string in the format # MONTH_NAME DAY_OF_MONTH, YEAR from date import Date # opens a file and returns the first line in # that file without the newline def get_first_line(filename): data_file = open(filename, "r") date_string = data_file.readline() data_file.close() return date_string.strip() DATA_FILENAME = "data.txt" date_string = get_first_line(DATA_FILENAME) date = Date(date_string) print(date.format_date())
return
statement# represents a calendar date class Date: def __init__(self, date_string): self.year, self.month, self.day = date_string.split("-") self.year = int(self.year) self.month = int(self.month) self.day = int(self.day) # converts a month number into a month name def get_month_name(self): month_names = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 7:"July", 8:"August", x 9:"September", 10:"October", 11:"November", 12:"December"} return month_names[self.month] # returns a date in standard format def format_date(self): return self.get_month_name() + " " + str(self.day) + ", " + str(self.year)
class CLASS_NAME:
def __init__(self[, PARAMETER, ...]):
...
def METHOD_NAME(self[, PARAMETER, ...]):
...
OBJECT_VARIABLE = CLASS_NAME([PARAMETER, ...])
date = Date(date_string)
print(date.format_date())
>>> date_1 = Date("2015-10-03") >>> date_2 = Date("2016-05-14")
id
>>> id(date_1) 4325278944 >>> id(date_2) 4325279280
>>> date_1.year 2015 >>> date_2.year 2016
>>> date_1.format_date() 'October 3, 2015'
YYYY-MM-DD
MONTH_NAME DAY_NUMBER, YEAR
MM/DD/YYYY
# returns a date in the short format M[M]/D[D]/YYYY def short_format_date(self): return str(self.month) + "/" + str(self.day) + "/" + str(self.year)
>>> date_1.short_format_date() '10/3/2015'
# returns a date in the long format D[D] MONTH_NAME YYYY def format_date_international(self): return str(self.day) + " " + self.get_month_name() + " " + str(self.year) # returns a date in the short format D[D]/M[M]/YYYY def short_format_date_international(self): return str(self.day) + "/" + str(self.month) + "/" + str(self.year)
>>> date_1.format_date_international() '3 October 2015' >>> date_1.short_format_date_international() '3/10/2015'
# returns a date in the long format # MONTH_NAME D[D], YYYY # if the region parameter is US, otherwise # D[D] MONTH_NAME YYYY def long_format(self, region="US"): if region == "US": return self.get_month_name() + " " + str(self.day) + ", " + str(self.year) else: return str(self.day) + " " + self.get_month_name() + " " + str(self.year)
>>> date_1.long_format() 'October 3, 2015'
>>> date_1.long_format("UK") '3 October 2015'
# returns a date in the short format # M[M]/D[D]/YYYY # if the region parameter is US, otherwise # D[D]/M[M]/YYYY def short_format(self, region="US"): if region == "US": return str(self.month) + "/" + str(self.day) + "/" + str(self.year) else: return str(self.day) + "/" + str(self.month) + "/" + str(self.year)
>>> date_1.short_format() '10/3/2015' >>> date_1.short_format("UK") '3/10/2015'
HH:MM:SS
# expects a string of the form HH:MM:SS # where hours are expressed in numbers from 0 to 23 def __init__(self, time_string): self.hours, self.minutes, self.seconds = time_string.split(":") self.hours = int(self.hours) self.minutes = int(self.minutes) self.seconds = int(self.seconds)
# returns a string in the form HH:MM:SS AM/PM def format_time(self): hours = self.hours am_pm = "AM" if hours > 12: hours -= 12 am_pm = "PM" return str(hours) + ":" + str(self.minutes) + ":" + str(self.seconds) + " " + am_pm
>>> t1 = Time("10:45:30") >>> t1.format_time() '10:45:30 AM'
# returns the difference in seconds between two times def difference(self, other_time): seconds = (self.hours - other_time.hours) * 60 * 60 seconds += (self.minutes - other_time.minutes) * 60 seconds += self.seconds - other_time.seconds return seconds
>>> midnight = Time("00:00:00") >>> t2 = Time("03:04:13") >>> t2.difference(midnight) 11053
# returns a string formated as follows # H[H] hours M[M] minutes S[S] seconds def format_seconds(seconds): hours = seconds // (60 * 60) remainder = seconds % (60 * 60) minutes = remainder // 60 seconds = remainder % 60 return str(hours) + " hours " + str(minutes)+ " minutes " + str(seconds) + " seconds"
>>> midnight = Time("00:00:00")
>>> t2 = Time("03:04:13")
>>> Time.format_seconds(t2.difference(midnight))
'3 hours 4 minutes 13 seconds'
# expects a string of the form HH:MM:SS # where hours are expressed in numbers from 0 to 23 def __init__(self, time_string): hours, minutes, seconds = time_string.split(":") hours = int(hours) minutes = int(minutes) seconds = int(seconds) self.seconds = hours * 60 * 60 + minutes * 60 + seconds
# returns a string in the form HH:MM:SS AM/PM
def format_time(self):
hours = self.seconds // (60 * 60)
remainder = self.seconds % (60 * 60)
minutes = remainder // 60
seconds = remainder % 60
am_pm = "AM"
if hours > 12:
hours -= 12
am_pm = "PM"
return str(hours) + ":" + str(minutes) + ":" + str(seconds) + " " + am_pm
# returns the difference in seconds between two times def difference(self, other_time): return self.seconds - other_time.seconds
>>> from time_2 import Time >>> midnight = Time("00:00:00") >>> t2 = Time("03:04:13") >>> t2.format_time() '3:4:13 AM' >>> Time.format_seconds(t2.difference(midnight)) '3 hours 4 minutes 13 seconds'