Are there any questions before I begin?
I have posted a solution to homework 7 here.
Let's take a look.
I have posted homework 9 here.
It is due this coming Sunday at 11:59 PM.
section = Section("it117", "s17", 1)
TypeError: object() takes no parameters
return statement
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= Date("2015-10-03")
>>> date.year 2015
>>> date.format_date() 'October 3, 2015'
self.get_month_name()
self.year
self comes before the dot
(.)
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
# 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
>>> t1.second 63924
>>> t1.seconds = 7000 >>> t1.format_time() '1:56:40 AM'
>>> t1 = Time("14:35:12")
>>> t1.hours
14
>>> t1.minutes
35
>>> t1.seconds
12
>>> t1.hours = 15 >>> t1.minutes = 14 >>> t1.seconds = 13 >>> t1.format_time() '3:14:13 PM'
>>> t1 = Time("14:35:12")
>>> t1.seconds
52512
t1.format_time()
'2:35:12 PM'
>>> t1.seconds = 1000000000 >>> t1.format_time() '277765:46:40 PM'
# 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
>>> t1 = Time("14:35:12")
>>> t1.__seconds
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Time' object has no attribute '__seconds '
self.secondsto
self.__seconds
>>> t1.format_time() '2:35:12 PM'
t1 = Time("11:54:12")
>>> t = Time("09:30:00")
>>> print(t)
<time_3.Time object at 0x1013e8358>
# 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) + ':' + str(minutes) + ':' + str(seconds )
>>> print("Here is the time object we just created: ", t)
Here is the time object we just created: 9:30:00
def __init__(self, manufacturer, model, serial_number, processor, ram, hostname, disk_size):
self.__manufacturer = manufacturer
self.__model = model
self.__serial_number = serial_number
self.__processor = processor
self.__ram = ram
self.__hostname = hostname
self.__disk_size = disk_size
def get_manufacturer(self):
return self.__manufacturer
def get_model(self):
return self.__model
def get_serial_number(self):
return self.__serial_number
def get_processor(self):
return self.__processor
def get_ram(self):
return self.__ram
def get_hostname(self):
return self.__hostname
def get_disk_size(self):
return self.__disk_size
def set_ram(self, ram):
self.__ram = ram
def set_hostname(self, hostname):
self.__hostname = hostname
def set_disk_size(self, disk_size):
self.__disk_size = disk_siz
>>> t1 = Time("500:3000:120000")
>>> t1.format_time() '571:20:0 PM'
isinstanceisinstance takes two arguments
True if the object is an instance of the
class
>>> isinstance("14:35:12", str)
True
if not isinstance(time_string, str):
raise TypeError("Time constructor expects a string argument")
>>> t1 = Time(55)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/10_chapter_example_code/time_3.py", line 13, in __init__
raise TypeError('Time constructor expects a string argument')
TypeError: Time constructor expects a string argument
# returns true if the string has the form HH:MM:SS
def has_valid_format(s):
pattern = re.compile("\d\d:\d\d:\d\d")
match = pattern.match(s)
return match
TrueNoneNone is interpreted as False in an if statement
# returns true if the numbers for hours, minutes and seconds
# are within the proper range
def time_numbers_in_range(hours, minutes, seconds ):
if not 0 <= hours <= 23:
return False
if not 0 <= minutes <= 59:
return False
if not 0 <= seconds <= 59:
return False
return True
# expects a string of the form HH:MM:SS
# where hours are expressed in numbers from 0 to 23
def __init__(self, time_string):
if not isinstance(time_string, str):
raise TypeError("Time constructor expects a string argument")
if not self.has_valid_format(time_string):
raise ValueError("String must have the format HH:MM:SS")
hours, minutes, seconds = time_string.split(":")
hours = int(hours)
minutes = int(minutes)
seconds = int(seconds )
if not self.time_numbers_in_range(hours, minutes, seconds ):
raise ValueError("One of the time values is not in the proper range")
self.__seconds = hours * 60 * 60 + minutes * 60 + seconds