Lecture 11 Asserts and Error Handling
Announcements for Today Reading Assignments • Reread Chapter 3 • Assignment 1 should be done § If not, you got an e-mail • 10.0-10.2, 10.4-10.6 for Tue • Assignment 2 in progress • Prelim, Oct 11 th 7:30-9:00 § Solutions posted in CMS § Material up October 2nd § Look for Gradescope e-mail § Study guide next week • Assignment 3 due next week • Conflict with Prelim time? § Before you leave for break § Submit to Prelim 1 Conflict § Same “length” as A1 assignment on CMS § Get help now if you need it § Do not submit if no conflict 9/27/18 Asserts & Error Handling 2
Using Color Objects in A3 • New classes in introcs id1 c id1 § RGB, CMYK, and HSV RGB r 128 red 128 • Each has its own attributes green 0 § RGB : red , blue , green § CMYK : cyan , magenta , blue 0 yellow , black § HSV : hue , saturation , value >>> import introcs >>> c = introcs .RGB(128,0,0) • Attributes have invariants >>> r = c.red § Limits the attribute values >>> c.red = 500 # out of range § Example: red is int in 0..255 AssertionError: 500 outside [0,255] § Get an error if you violate 9/27/18 Asserts & Error Handling 3
Using Color Objects in A3 • New classes in introcs id1 c id1 § RGB, CMYK, and HSV RGB r 128 red 128 • Each has its own attributes green 0 § RGB : red , blue , green Constructor function. To make a new color. § CMYK : cyan , magenta , blue 0 yellow , black § HSV : hue , saturation , value >>> import introcs >>> c = introcs .RGB(128,0,0) • Attributes have invariants >>> r = c.red § Limits the attribute values >>> c.red = 500 # out of range Accessing § Example: red is int in 0..255 Attribute AssertionError: 500 outside [0,255] § Get an error if you violate 9/27/18 Asserts & Error Handling 4
How to Do the Conversion Functions def rgb_to_cmyk(rgb): """Returns: color rgb in space CMYK Precondition: rgb is an RGB object""" # DO NOT CONSTRUCT AN RGB OBJECT # Variable rgb already has RGB object # 1. Access attributes from rgb folder # 2. Plug into formula provided # 3. Compute the new cyan, magenta, etc. values # 4. Construct a new CMYK object Only time you will ever call a # 5. Return the newly constructed object constructor 9/27/18 Asserts & Error Handling 5
Recall: The Call Stack • Functions are “stacked” § Cannot remove one above Frame 1 calls w/o removing one below § Sometimes draw bottom up Frame 2 calls (better fits the metaphor) Frame 3 • Stack represents memory calls as a “high water mark” Frame 4 calls § Must have enough to keep the Frame 6 entire stack in memory § Error if cannot hold stack 9/27/18 Asserts & Error Handling 6
Errors and the Call Stack # error.py def function_1(x,y): return function_2(x,y) calls def function_2(x,y): return function_3(x,y) calls def function_3(x,y): return x/y # crash here calls if __name__ == '__main__': print(function_1(1,0)) 9/27/18 Asserts & Error Handling 7
Errors and the Call Stack # error.py Crashes produce the call stack: Traceback (most recent call last): def function_1(x,y): File "error.py", line 20, in <module> return function_2(x,y) print(function_1(1,0)) File "error.py", line 8, in function_1 def function_2(x,y): return function_2(x,y) return function_3(x,y) File "error.py", line 12, in function_2 return function_3(x,y) def function_3(x,y): File "error.py", line 16, in function_3 return x/y # crash here return x/y if __name__ == '__main__': Make sure you can see print(function_1(1,0)) line numbers in Atom. 9/27/18 Asserts & Error Handling 8
Errors and the Call Stack # error.py Crashes produce the call stack: Script code. Global space Traceback (most recent call last): def function_1(x,y): File "error.py", line 20, in <module> return function_2(x,y) print(function_1(1,0)) File "error.py", line 8, in function_1 def function_2(x,y): return function_2(x,y) return function_3(x,y) File "error.py", line 12, in function_2 return function_3(x,y) def function_3(x,y): File "error.py", line 16, in function_3 return x/y # crash here return x/y if __name__ == '__main__': Where error occurred Make sure you can see print function_1(1,0) (or where was found) line numbers in Atom. 9/27/18 Asserts & Error Handling 9
Assert Statements assert <boolean> # Creates error if <boolean> false assert <boolean>, <string> # As above, but displays <String> • Way to force an error def exchange(from_c, to_c, amt) § Why would you do this? """Returns: amt from exchange Precondition: amt a float…""" • Enforce preconditions! assert type(amt) == float § Put precondition as assert. … § If violate precondition, the program crashes • Provided code in A3 Will do yourself in A4 . uses asserts heavily 9/27/18 Asserts & Error Handling 10
Example: Anglicizing an Integer def anglicize(n): """Returns: the anglicization of int n. Precondition: n an int, 0 < n < 1,000,000""" assert type(n) == int, repr(n)+' is not an int' assert 0 < n and n < 1000000, repr(n)+' is out of range' # Implement method here… 9/27/18 Asserts & Error Handling 11
Example: Anglicizing an Integer def anglicize(n): """Returns: the anglicization of int n. Precondition: n an int, 0 < n < 1,000,000""" assert type(n) == int, repr(n)+' is not an int' assert 0 < n and n < 1000000, repr(n)+' is out of range' # Implement method here… Check (part of) Error message the precondition when violated 9/27/18 Asserts & Error Handling 12
Aside: Using repr Instead of str >>> msg = str(var)+' is invalid' >>> print(msg) 2 is invalid • Looking at this output, what is the type of var ? A: int B: float C: str D: Impossible to tell 9/27/18 Asserts & Error Handling 13
Aside: Using repr Instead of str >>> msg = str(var)+' is invalid' >>> print(msg) 2 is invalid • Looking at this output, what is the type of var ? A: int B: float C: str D: Impossible to tell CORRECT 9/27/18 Asserts & Error Handling 14
Aside: Using repr Instead of str >>> msg = str(var)+' is invalid' >>> print(msg) 2 is invalid >>> msg = repr(var)+' is invalid' >>> print(msg) Clear that var is really a string '2' is invalid 9/27/18 Asserts & Error Handling 15
Enforcing Preconditions is Tricky! def lookup_netid(nid): """Returns: name of student with netid nid. Precondition: nid is a string, which consists of 2 or 3 letters and a number""" assert ????? Sometimes we will Assert use expressions only. only enforce part of Cannot use if-statements. the precondition Each one must fit on one line. 9/27/18 Asserts & Error Handling 16
Enforcing Preconditions is Tricky! def lookup_netid(nid): """Returns: name of student with netid nid. Precondition: nid is a string, which consists of 2 or 3 letters and a number""" assert type(nid) == str, repr(nid) + ' is not a string' assert nid.isalnum(), nid+' is not just letters/digits' Returns True if s contains Does this catch only letters, numbers. all violations? 9/27/18 Asserts & Error Handling 17
Using Function to Enforce Preconditions def exchange(curr_from, curr_to, amt_from): """Returns: amount of curr_to received. Precondition: curr_from is a valid currency code Precondition: curr_to is a valid currency code Precondition: amt_from is a float""" assert ?????? , repr(curr_from) + ' not valid' assert ?????? , repr(curr_from) + ' not valid' assert type(amt_from)==float, repr(amt_from)+' not a float' 9/27/18 Asserts & Error Handling 18
Using Function to Enforce Preconditions def exchange(curr_from, curr_to, amt_from): """Returns: amount of curr_to received. Precondition: curr_from is a valid currency code Precondition: curr_to is a valid currency code Precondition: amt_from is a float""" assert iscurrency (curr_from), repr(curr_from) + ' not valid' assert iscurrency (curr_to), repr(curr_to) + ' not valid' assert type(amt_from)==float, repr(amt_from)+' not a float' 9/27/18 Asserts & Error Handling 19
Recovering from Errors • try-except blocks allow us to recover from errors § Do the code that is in the try-block § Once an error occurs, jump to the catch • Example : try: might have an error input = input() # get number from user x = float(input) # convert string to float print('The next number is '+str(x+1)) except: executes if error happens print('Hey! That is not a number!') 9/27/18 Asserts & Error Handling 20
Recovering from Errors • try-except blocks allow us to recover from errors Similar to if-else § Do the code that is in the try-block § But always does try § Once an error occurs, jump to the catch § Just might not do • Example : all of the try block try: might have an error input = input() # get number from user x = float(input) # convert string to float print('The next number is '+str(x+1)) except: executes if error happens print('Hey! That is not a number!') 9/27/18 Asserts & Error Handling 21
Recommend
More recommend