In Chapter 11, Inheritance, from our textbook, Starting Out with Python, you should read sections 11.1 and 11.2, Introduction to Inheritance and Polymorphism.
I have posted homework 10 here.
Let's look at the answers to Quiz 8.
Are there any questions before I begin?
if statement 
			if __name__ == "__main__":
#! /usr/bin/python3
chmod 755 MODULE_FILENAME
		if 
			statement
		x += 7 y = x + 8 x > y
diff = t1.difference(t2)
diff = t1 - t2
| Operator | Magic Method | 
|---|---|
| + | __add__(self, other) | 
| - | __sub__(self, other) | 
| * | __mul__(self, other) | 
| // | __floordiv__(self, other) | 
| / | __truediv__(self, other) | 
| % | __mod__(self, other) | 
| ** | __pow__(self, other) | 
# returns the difference in seconds between two times
def __sub__(self, other_time):
    return self.__seconds - other_time.__seconds
		
>>> t1 = Time("10:45:10")
>>> t2 = Time("10:50:00")
>>> t2 - t1
290
		| Operator | Magic Method | 
|---|---|
| == | __eq__(self, other) | 
| != | __ne__(self, other) | 
| < | __lt__(self, other) | 
| > | __gt__(self, other) | 
| <= | __le__(self, other) | 
| >= | __ge__(self, other) | 
def __eq__(self, other):
    return self.__seconds == other.__seconds
def __ne__(self, other):
    return self.__seconds != other.__seconds
def __lt__(self, other):
    return self.__seconds < other.__seconds
def __gt__(self, other):
    return self.__seconds > other.__seconds
def __le__(self, other):
    return self.__seconds <= other.__seconds
def __ge__(self,other):
    return self.__seconds >= other.__seconds
		
>>> t1 = Time("10:45:10")
>>> t2 = Time("10:50:00")
>>> t3 = Time("10:50:00")
>>> t1 == t2
False
t2 == t3
True
>>> t1 < t2
True
>>> t1 > t2
False
>>> t1 <= t2
True
>>> t2 <= t3
True
>>> t1 >= t2
False
>>> t2 >= t3
True
		intfloatboolstr| Operator | Magic Method | 
|---|---|
int | 
						__int__(self) | 
float | 
						__float__(self) | 
bool | 
						__bool__(self) | 
str | 
						__str__(self) | 
def __int__(self):
    return self.__seconds
		
def __float__(self):
    return self.__seconds * 1.0
		True
			>>> bool(0) False >>> bool(5) True >>> bool(-5) True
def __bool__(self):
    return self.__seconds != 0
		
>>> t1 = Time("10:15:00")
>>> t2 = Time("00:00:00")
>>> str(t1)
'10:15:0 AM'
>>> bool(t1)
True
>>> bool(t2)
False
>>> int(t1)
36900
>>> int(t2)
0
>>> float(t1)
36900.0
		
def __eq__(self, other):
    return self.__seconds == other.__seconds
		
    def __eq__(self, other):
        return self.__manufacturer == other.get_manufacturer() and self.__model == other.get_model()
    def __ne__(self, other):
        return self.__manufacturer != other.get_manufacturer() or self.__model != other.get_model() 
		>>> from car4 import Car Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
if statement
			
if __name__ == "__main__":
    c1 = Car("Honda", "CRV",   "1997", "Blue")
    c2 = Car("Honda", "CRV",   "2010", "Green")
    c3 = Car("Honda", "Civic", "2015", "Red")
    print(c1)
    print(c2)
    print(c3)
    print("c1 == c2 :" , c1 == c2 )
    print("c1 == c3 :" , c1 == c3 )
    print("c1 != c3 :" , c1 != c3 )
		$ ./car3.py Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
>>> from car3 import Car >>>
def __init__(self, collection_no, name, length, format):
    self.__collection_no = collection_no
    self.__name   = name
    self.__length = length
    self.__format = format
		for loop using the string method 
			isalnum
			
MIN_NAME_LENGTH = 3
class Video:
...
    def __check_name(name):
        if type(name) is not str:
            raise TypeError("Video constructor expects a string argument for name")
        char_count = 0
        for char in name:
            if char.isalnum():
                char_count += 1
        if char_count < MIN_NAME_LENGTH:
            raise ValueError("Name must have at least " + str(MIN_NAME_LENGTH) + \
                    " characters that are letters or digits")
		
>>> v1 = Video(0, 0, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/glenn/Documents/workspace-neon/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 11, in __init__
    self.__check_name(name)
  File "/Users/glenn/Documents/workspace-neon/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 21, in __check_name
    raise TypeError('Video constructor expects a string argument for name')
TypeError: Video constructor expects a string argument for name
>>> 
>>> v1 = Video("he", 0, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/glenn/Documents/workspace-neon/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 11, in __init__
    self.__check_name(name)
  File "/Users/glenn/Documents/workspace-neon/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 28, in __check_name
    ' characters that are letters or digits')
ValueError: Name must have at least 3 characters that are letters or digits
>>> 
>>> v1 = Video("he o", 0, "foo")
>>> 
		
MIN_LENGTH      = 15
MAX_LENGTH      = 1000
class Video:
...
    def __check_length(length): 
        if type(length) is not int:
            raise TypeError("Video constructor expects a integer argument for length")
        if length < MIN_NAME_LENGTH or length > MAX_LENGTH:
            raise ValueError("Length must be at least " + str(MIN_LENGTH) + 
                " and no more than " + str(MAX_LENGTH))
		
>>> v1 = Video("Forbidden Planet", "foo", "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 13, in __init__
    self.__check_length(length)
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 30, in __check_length
    raise TypeError('Video constructor expects a integer argument for length')
TypeError: Video constructor expects a integer argument for length
>>> 
>>> v1 = Video("Forbidden Planet", 0, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 13, in __init__
    self.__check_length(length)
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 33, in __check_length
    " and no more than " + str(MAX_LENGTH))
ValueError: Length must be at least 15 and no more than 1000
>>> 
>>> v1 = Video("Forbidden Planet", 2000, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 13, in __init__
    self.__check_length(length)
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 33, in __check_length
    ' and no more than ' + str(MAX_LENGTH))
ValueError: Length must be at least 15 and no more than 1000
>>> 
>>> v1 = Video("Forbidden Planet", 100, "foo")
>>> 
		
FORMATS         = ("DVD", "Blue Ray")
class Video:
...
    def __check_format(format):
        if type(format) is not str:
            raise TypeError("Video constructor expects a string argument for format")
        if format not in FORMATS:
            raise ValueError("Format must be one of " + str(FORMATS))
		
>>> v1 = Video("Forbidden Planet", 100, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 15, in __init__
    self.__check_format(format)
  File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/video.py", line 39, in __check_format
    raise ValueError('Format must be one of ' + str(FORMATS))
ValueError: Format must be one of ('DVD', 'Blue Ray')
>>> 
>>> v1 = Video("Forbidden Planet", 100, "DVD")
>>> 
		open function inside a try block
# reads the last collection number used from a file,
# increments that number and writes it back to the file
# and returns the incremented number
def __next_collection_no():
    try:
        file = open(FILENAME, 'r')
        collection_no = int(file.readline())
    except FileNotFoundError:
        collection_no = 1
    else: 
        collection_no += 1
        file.close()
    finally:
        file = open(FILENAME, 'w')
        file.write(str(collection_no))
        file.close()
        return collection_no
		
def __init__(self, name, length, format):
    self.__check_name(name)
    self.__check_length(length)
    self.__check_format(format)
    self.__collection_no = self.__next_collection_no()
    self.__name   = name
    self.__length = length
    self.__format = format
		
>>> from video import Video
>>> v1 = Video("Forbidden Planet", 100, "DVD")
>>> v1.get_collection_no()
1
>>> v2 = Video("The Day the Earth Stood Stil", 90, "DVD")
>>> v2.get_collection_no()
2
		
def get_collection_no(self):
    return self.__collection_no
def get_name(self):
    return self.__name
def get_length(self):
    return self.__length
def get_format(self):
    return self.__format
		
>>> v1 = Video("Forbidden Planet", 100, "DVD")
>>> v1.get_collection_no()
1
>>> v1.get_name()
'Forbidden Planet'
>>> v1.get_length()
100
>>> v1.get_format()
'DVD'
		
def __str__(self):
    return str(self.__collection_no) + ": " + self.__name + ", " + \
        str(self.__length) + " minutes, " + self.__format
		
>>> from video import Video
>>> v1 = Video("Forbidden Planet", 100, "DVD")
>>> print(v1)
4: Forbidden Planet, 100 minutes, DVD
>>> str(v1)
4: Forbidden Planet, 100 minutes, DVD