Title: CSC1018F: Object Orientation, Exceptions and File Handling (Tutorial)
1CSC1018F Object Orientation, Exceptions and
File Handling(Tutorial)
- Diving into Python Ch. 56
- Think Like a Comp. Scientist Ch. 11-13
2Mini-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 - from abra import
- from abra import function1, function2,
- import abra
- Which of the following statements concerning
classes is NOT correct - 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
3Mini-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
- 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
4Mini-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 - 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 - True
- False
5Mini-Test Answers (4)
- The following python code executed on a windows
system - gtgtgt import os
- gtgtgt os.path.join("c\\documents", "photo.jpg")
- will output
- "c\\documentsphoto.jpg"
- "c\\documents\\photo.jpg"
- "c\documents\photo.jpg"
6Revision 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
lt length l or truncates rows gt length l - Threshold(t) - set all pixels lt 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
7Revision 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(255x) 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) lt length and
row.extend(255(le
ngth-len(row))) or
rowlength) for row in self.__image
8Revision 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 lt self.__height
self.__image self.__imagey elif y gt
self.__height for i in
range(y-self.__height)
self.__image.append(255self.__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)