Class Notes (837,317)
Canada (510,221)
CSC148H1 (92)
Paul Gries (18)
Lecture

jan23c.docx

3 Pages
145 Views
Unlock Document

Department
Computer Science
Course
CSC148H1
Professor
Paul Gries
Semester
Winter

Description
 exceptions: try and except conditions o if the code under try statements works, skips the except condition o if error occurs, python compares error raised to exception typed specified by the except  a match = execute except block o ex. 1. try: 2. x = 1/0 3. except Exception as error: # except Exception catches all exceptions 4. print error # error message raised in the try block  exceptions don’t have to be handled immediately 1. try: 2. params = read_params(param_file) # these functions may catch 3. grid = read_grid(grid_file) # and handle exceptions themselves 4. entropy = lee_entropy(params, grid) # if one isn’t handled internally 5. write_entropy(entropy_file, entropy) # then it is handled below 6. except IOError as err: 7. print err 8. except ArithemticError as err: 9. print err o exception handling propagates up to where the highest function called if not handled internally o throw low, catch high  error may occur in many places but only few places where they can be sensibly handled  throw very specific exception in lower levels of program, catch only at higher level  Raise exceptions 1. if x == 0: 2. raise Exception(‘X is zero’) o more specific exception 1. def divide(top, bottom): 2. if bottom == 0: 3. raise ValueError(‘divisor is zero’) 4. else: 5. return top / bottom  to define an exception write it as a child class of Exception class o encouraged to write own unique Exceptions to distinguish different errors 1. class InvalidTimeError(Exception): 2. '''An exception raised if a ClockTime object is initialized with nonsense values.''' 3. 4. def __init__(self, b, v): 5. '''(InvalidTimeError, str, str) -> InvalidTimeError 6. Construct an InvalidTimeError for b and v, indicating the 7. field (hour or minute) and value for that field.''' 8. self.field = b 9. self.value = v 10. 11.class ClockTime(object): 12. '''A clock.''' 13. 14. def __init__(self, hour, minute): 15. '''(ClockTime, int, int) -> ClockTime''' 16. if hour < 0 or hour > 23 or not isinstance(hour, int): 17. raise InvalidTimeError('hour', str(hour)) 18. self.hour = hour 19. self.minute = minute 20. 21. def __str__(self): 22. '''(ClockTime) -> str''' 23. if self.hour < 12: 24. return '%d:%.2d am' % (self.hour, self.minute) # %.2d = 2 digits, 9 = 09 25. else: 26. return str(self.hour - 12) + ':' + str(self.minute) + ' pm' 27. 28. def __repr__(self): 29. return 'ClockTime(' + str(self.hour) + ', ' + str(self.minute) + ')'  only one error rasied at a time o ex. 1. assert 3 == 4 2. assert 1 == 5 o one failure, assert error in the first line. Raise called  exits current program and gives error TESTING  options o nose (separate install, but doesn’t use classes) o PyUnit/unittest (comes with Python!) o doctest (comes with Python!)  PyUnit o import unittest  and modules that are being tested o created classes that inherit from unittest.TestCase o create methods in these classes  just like nose, test metods names start with ‘test’ o special methods:  setUp, tearDown  assertTrue, assertFalse, assertEqual 1. import unittest 2. import clock_time 3. 4. class ClockTimeTestCaseAM(unittest.TestCase): 5. 6. def setUp(self)
More Less

Related notes for CSC148H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit