asserts and error handling announcements for today
play

Asserts and Error Handling Announcements for Today Reading - PowerPoint PPT Presentation

Lecture 11 Asserts and Error Handling Announcements for Today Reading Assignments Reread Chapter 3 Assignment 1 now complete Unless we gave extension 10.0-10.2, 10.4-10.6 for Thu Assignment 2 in progress Prelim, Oct


  1. Lecture 11 Asserts and Error Handling

  2. Announcements for Today Reading Assignments • Reread Chapter 3 • Assignment 1 now complete § Unless we gave extension • 10.0-10.2, 10.4-10.6 for Thu • Assignment 2 in progress • Prelim, Oct 13 th 7:30-9:00 § Ready for pick-up Thurs § Material up October 4th § Solutions posted in CMS § 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/16 Asserts & Error Handling 2

  3. Using Color Objects in A3 • New classes in colormodel 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 colormodel >>> c = colormodel.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/16 Asserts & Error Handling 3

  4. Using Color Objects in A3 • New classes in colormodel 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. § CMYK : cyan , magenta , To make a new color. blue 0 yellow , black § HSV : hue , saturation , value >>> import colormodel >>> c = colormodel.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 AssertionError: 500 outside [0,255] Attribute § Get an error if you violate 9/27/16 Asserts & Error Handling 4

  5. 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/16 Asserts & Error Handling 5

  6. 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/16 Asserts & Error Handling 6

  7. 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/16 Asserts & Error Handling 7

  8. 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 Komodo. Preferences è Editor 9/27/16 Asserts & Error Handling 8

  9. Errors and the Call Stack # error.py Crashes produce the call stack: Script code. Traceback (most recent call last): Global space 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 Komodo. Preferences è Editor 9/27/16 Asserts & Error Handling 9

  10. 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) """Returns: amt from exchange § Why would you do this? Precondition: amt is a • Enforce preconditions! float…""" § Put precondition as assert. assert type(amt) == float § If violate precondition, … the program crashes • Provided code in A3 Will do yourself in A4 . uses asserts heavily 9/27/16 Asserts & Error Handling 10

  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, str(n)+' is not an int' assert 0 < n and n < 1000000, str(n)+' is out of range' # Implement method here… 9/27/16 Asserts & Error Handling 11

  12. 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, str(n)+' is not an int' assert 0 < n and n < 1000000, str(n)+' is out of range' # Implement method here… Check (part of) Error message the precondition when violated 9/27/16 Asserts & Error Handling 12

  13. 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/16 Asserts & Error Handling 13

  14. 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, str(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/16 Asserts & Error Handling 14

  15. 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 ?????? , str(curr_from) + ' not valid' assert ?????? , str(curr_from) + ' not valid' assert type(amt_from)==float, str(amt_from) + ' not a float' 9/27/16 Asserts & Error Handling 15

  16. 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), str(curr_from) + ' not valid' assert iscurrency (curr_to), str(curr_to) + ' not valid' assert type(amt_from)==float, str(amt_from) + ' not a float' 9/27/16 Asserts & Error Handling 16

  17. 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 = raw_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/16 Asserts & Error Handling 17

  18. 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 = raw_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/16 Asserts & Error Handling 18

  19. Try-Except is Very Versatile def isfloat(s): """Returns: True if string s represents a float""" Conversion to a try : float might fail x = float(s) If attempt succeeds, return True string s is a float except : return False Otherwise, it is not 9/27/16 Asserts & Error Handling 19

  20. Try-Except and the Call Stack # recover.py • Error “pops” frames off stack § Starts from the stack bottom def function_1(x,y): § Continues until it sees that try: current line is in a try -block return function_2(x,y) § Jumps to except , and then except: proceeds as if no error return float('inf') function_1 line in a try def function_2(x,y): pops return function_3(x,y) function_2 pops def function_3(x,y): function_3 return x/y # crash here 9/27/16 Asserts & Error Handling 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend