Transcript PythonOOTut

CSC1018F:
Object Orientation, Exceptions
and File Handling
(Tutorial)
Diving into Python Ch. 5&6
Think Like a Comp. Scientist Ch. 11-13
Mini-Test Answers (1)
If you want access to the contents of a module “abra” but do
not want to bring it into the current namespace (i.e., all
functions of the module must be qualified with
“module.function”) then you would call:
a. from abra import *
b. from abra import function1, function2, …
c. import abra
Which of the following statements concerning classes is
NOT correct:
a.
b.
c.
d.
e.
Data attributes of a class must be declared in the __init__ method
__init__ methods are optional
Class attributes can be accessed from both a class and instance as
“class.attrib” or “instance.attrib”
Python does not support multiple inheritance
The “__data__” notation indicates a private method or attribute
Mini-Test Answers (2)
The following python code:
try:
fsock = open("/program.py")
except IOError:
print "The file does not exist"
except AttributeError:
print "open() not available"
print "Program opened"
will:
a.
b.
c.
d.
Always print “Program opened”
Print “Program opened” only if no IOError occurs
Print “Program opened” if no IOError and no AttributeError occur
Fail to execute because it contains a syntax error
Mini-Test Answers (3)
Assuming that a file called “f” of length 256 has been newly
opened, you could move the current read position to 128 by
calling:
a.
b.
c.
d.
e.
f.seek(-128, 2)
f.seek(-128, 1)
f.seek(128, 0)
f.seek(128, 1)
f.seek(-128, 0)
Calling “from module import *” twice in succession
will cause a syntax error because of namespace conflicts:
a.
b.
True
False
Mini-Test Answers (4)
The following python code executed on a windows
system:
>>> import os
>>> os.path.join("c:\\documents",
"photo.jpg")
will output:
a. "c:\\documentsphoto.jpg"
b. "c:\\documents\\photo.jpg"
c. "c:\documents\photo.jpg"
Revision Exercise
Create an image class with the following
methods:
__init__ - creates a white image with a user
specified x, y resolution
CheckValidity() - which checks if all pixel rows
have the same length
PadTrunc(l) - which appends white pixels to
rows < length l or truncates rows > length l
Threshold(t) - set all pixels <= t to 0 and the
rest to 255
Resize(x, y) - alters the resolution of an image
by making calls to PadTrunc and/or dropping
rows
Revision Solution (1)
class Image:
"""A greyscale image class consisting of a rectangular matrix
of integer pixels where 0 represents black and 255 white """
def __init__(self, x = 100, y = 100):
self.__image = []
self.__width = x
self.__height = y
for i in range(y):
self.__image.append([255]*x)
def CheckValidity(self):
for row in self.__image:
if(len(row) != self.__width):
return False
return True
def PadTrunc(self, length):
self.__image = [(len(row) < length and
row.extend([255]*(length-len(row)))
or row[:length]) for row in self.__image]
Revision Solution (2)
def Resize(self, x, y):
if not self.CheckValidity():
print "Warning: Correcting Invalid Image"
self.PadTrunc(self.__width)
# why do we resize y before x?
if y < self.__height:
self.__image = self.__image[:y]
elif y > self.__height:
for i in range(y-self.__height):
self.__image.append([255]*self.__width)
self.__height = y
if
x != self.__width:
self.PadTrunc(x)
self.__width = x
def __str__(self):
return "resolution (" + str(self.__width) + "," +
str(self.__height) + "):\n" + str(self.__image)