Lecture 11. Asserts and Error Handling

Similar documents
Lecture 11. Asserts and Error Handling

Lecture 9. Memory and Call Stacks

Review 3. Exceptions and Try-Except Blocks

CS Prelim 1 Review Fall 2012

PREPARING FOR PRELIM 1

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses

CS Prelim 2 Review Fall 2017

Lecture 12. Lists (& Sequences)

Lecture 18. Classes and Types

Lecture 21. Programming with Subclasses

CS Prelim 2 Review Fall 2012

CS Prelim 2 Review Fall 2015

CS Prelim 2 Review Fall 2014

CS Prelim 1 Review Fall 2018

Lecture 10. Memory in Python

CS 1110 Prelim 1 October 17th, 2013

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

CS Prelim 1 Review Fall 2013

CS Prelim 1 Review Fall 2017

CS Prelim 1 Review Fall 2013

CS 1110 Prelim 2 November 14th, 2013

CS 1110 Prelim 1 October 4th, 2012

CS 1110 Final, December 8th, Question Points Score Total: 100

Lecture 4. Defining Functions

PREPARING FOR PRELIM 2

Announcements for this Lecture

CS 1110 Prelim 1 October 15th, 2015

CS1110 Lab 6 (Mar 17-18, 2015)

CS 1110 Final, December 8th, Question Points Score Total: 100

Lecture 17: Classes (Chapter 15)

CS Prelim 1 Review Fall 2016

CS Lecture 26: Grab Bag. Announcements

CS 1110 Prelim 1 March 10, 2015

PREPARING FOR THE FINAL EXAM

Lecture 19. Operators and Abstraction

Lecture 5. Defining Functions

Errors. And How to Handle Them

CS Lecture 19: Loop invariants

Lecture 7. Memory in Python

Announcements for This Lecture

CS Prelim 2 Review Fall 2018

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Lecture 19. Using Classes Effectively

CS 1110 Prelim 2 November 6th, 2012

Fundamentals of Programming (Python) Getting Started with Programming

Getting Started with Python

a correct statement? You need to know what the statement is supposed to do.

Lecture 24. GUI Applications

Lecture 13. For-Loops

Lecture 22. While Loops

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

CSC148 Recipe for Designing Classes

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Lecture 13. Call Stacks & Debugging

Announcements for This Lecture

Lecture 9: Memory in Python

CS61A Lecture 9 Immutable Data Structures. Jom Magrotker UC Berkeley EECS July 2, 2012

CS 1110: Introduction to Computing Using Python Loop Invariants

Lecture 18: Using Classes Effectively (Chapter 16)

Outline. software testing: search bugs black-box and white-box testing static and dynamic testing

Lecture 18. Using Classes Effectively

Lecture 8. Conditionals & Control Flow

Lecture 1. Course Overview, Python Basics

CS 1110 Prelim 1 October 12th, 2017

CS1110 Lab 5: Practice for A4 (Mar 8-9, 2016) First Name: Last Name: NetID:

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

CISC 110 Week 1. An Introduction to Computer Graphics and Scripting

This guide helps you understand how operations available on the printer can be used to adjust and customize color output.

PREPARING FOR THE FINAL EXAM

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Graphics Overview ECE2893. Lecture 19. ECE2893 Graphics Overview Spring / 15

Assertions, pre/postconditions

CS 100: Computability, Python Lists

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Lecture 6: Specifications & Testing

Lecture Notes on Memory Layout

Control Structures 1 / 17

Conditionals & Control Flow

Lecture 17: Classes (Chapter 15)

Text Input and Conditionals

CS 1110 Prelim 1 March 15th, 2016

Expressions and Variables

CISC 1115 (Science Section) Brooklyn College Professor Langsam. Assignment #5

ECSE 321 Assignment 2

Lecture 24. GUI Applications

Readings for This Lecture

Lecture 20. Subclasses & Inheritance

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Iteration and For Loops

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

CSC Java Programming, Fall Java Data Types and Control Constructs

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Announcements for the Class

Lecture 1. Types, Expressions, & Variables

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lecture 1. Course Overview, Python Basics

Functions with Parameters and Return Values

Transcription:

Lecture 11 Asserts and Error Handling

Announcements for Today Reading Reread Chapter 3 10.0-10.2, 10.4-10.6 for Thu Prelim, Oct 12 th 7:30-9:00 Material up October 3rd Study guide next week Conflict with Prelim time? Submit to Prelim 1 Conflict assignment on CMS Do not submit if no conflict Assignments Assignment 1 should be done If not, revise ASAP Assignment 2 in progress Ready for pick-up Thurs Solutions posted in CMS Assignment 3 due next week Before you leave for break Same length as A1 Get help now if you need it 9/26/17 Asserts & Error Handling 2

Using Color Objects in A3 New classes in cornell RGB, CMYK, and HSV Each has its own attributes RGB: red, blue, green CMYK: cyan, magenta, yellow, black HSV: hue, saturation, value Attributes have invariants Limits the attribute values Example: red is int in 0..255 Get an error if you violate id1 c id1 RGB r 128 red 128 green 0 blue 0 >>> import cornell >>> c = cornell.rgb(128,0,0) >>> r = c.red >>> c.red = 500 # out of range AssertionError: 500 outside [0,255] 9/26/17 Asserts & Error Handling 3

Using Color Objects in A3 New classes in cornell RGB, CMYK, and HSV Each has its own attributes RGB: red, blue, green CMYK: cyan, magenta, yellow, black HSV: hue, saturation, value Attributes have invariants Limits the attribute values Example: red is int in 0..255 Get an error if you violate id1 c id1 RGB r 128 red 128 green 0 Constructor function. To make blue a new color. 0 >>> import cornell >>> c = cornell.rgb(128,0,0) >>> r = c.red >>> c.red = 500 # out Accessing of range AssertionError: 500 outside Attribute [0,255] 9/26/17 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 # 5. Return the newly constructed object Only time you will ever call a constructor 9/26/17 Asserts & Error Handling 5

Recall: The Call Stack Functions are stacked Cannot remove one above w/o removing one below Sometimes draw bottom up (better fits the metaphor) Stack represents memory as a high water mark Must have enough to keep the entire stack in memory Error if cannot hold stack Frame 1 Frame 2 Frame 3 Frame 4 Frame 6 calls calls calls calls 9/26/17 Asserts & Error Handling 6

Errors and the Call Stack # error.py def function_1(x,y): return function_2(x,y) def function_2(x,y): return function_3(x,y) def function_3(x,y): calls calls return x/y # crash here calls if name == ' main ': print(function_1(1,0)) 9/26/17 Asserts & Error Handling 7

Errors and the Call Stack # error.py def function_1(x,y): return function_2(x,y) def function_2(x,y): return function_3(x,y) def function_3(x,y): return x/y # crash here if name == ' main ': print(function_1(1,0)) Crashes produce the call stack: Traceback (most recent call last): File "error.py", line 20, in <module> print(function_1(1,0)) File "error.py", line 8, in function_1 return function_2(x,y) File "error.py", line 12, in function_2 return function_3(x,y) File "error.py", line 16, in function_3 return x/y Make sure you can see line numbers in Komodo. Preferences è Editor 9/26/17 Asserts & Error Handling 8

Errors and the Call Stack # error.py Script code. Global space def function_1(x,y): return function_2(x,y) def function_2(x,y): return function_3(x,y) def function_3(x,y): return x/y # crash here Where error occurred (or print where function_1(1,0) was found) if name == ' main ': Crashes produce the call stack: Traceback (most recent call last): File "error.py", line 20, in <module> print(function_1(1,0)) File "error.py", line 8, in function_1 return function_2(x,y) File "error.py", line 12, in function_2 return function_3(x,y) File "error.py", line 16, in function_3 return x/y Make sure you can see line numbers in Komodo. Preferences è Editor 9/26/17 Asserts & Error Handling 9

Assert Statements assert <boolean> assert <boolean>, <string> # Creates error if <boolean> false # As above, but displays <String> Way to force an error Why would you do this? Enforce preconditions! Put precondition as assert. If violate precondition, the program crashes Provided code in A3 uses asserts heavily def exchange(from_c, to_c, amt) """Returns: amt from exchange Precondition: amt a float """ assert type(amt) == float Will do yourself in A4. 9/26/17 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/26/17 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) the precondition Error message when violated 9/26/17 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/26/17 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/26/17 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) '2' is invalid Clear that var is really a string 9/26/17 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????? Assert use expressions only. Cannot use if-statements. Each one must fit on one line. Sometimes we will only enforce part of the precondition 9/26/17 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 only letters, numbers. Does this catch all violations? 9/26/17 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/26/17 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/26/17 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: input = input() # get number from user x = float(input) # convert string to float print('the next number is '+str(x+1)) except: print('hey! That is not a number!') might have an error executes if error happens 9/26/17 Asserts & Error Handling 20

Recovering from Errors try-except blocks allow us to recover Similar from to if-else errors 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 all of the try block Example: try: except: input = input() x = float(input) # get number from user # convert string to float print('the next number is '+str(x+1)) print('hey! That is not a number!') might have an error executes if error happens 9/26/17 Asserts & Error Handling 21

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

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

# recover.py def function_1(x,y): try: except: Try-Except and the Call Stack return function_2(x,y) return float('inf') def function_2(x,y): return function_3(x,y) def function_3(x,y): return x/y # crash here Error pops frames off stack How to return Starts from the stack bottom as a float. Continues until it sees that current line is in a try-block Jumps to except, and then proceeds as if no error Example: >>> print function_1(1,0) inf >>> No traceback! 9/26/17 Asserts & Error Handling 24

Tracing Control Flow def first(x): print('starting first.') try: second(x) except: print('caught at first') print('ending first') def third(x): print('starting third.') assert x < 1 print('ending third.') What is the output of first(2)? def second(x): print('starting second.') try: third(x) except: print('caught at second') print('ending second') 9/26/17 Asserts & Error Handling 25

Tracing Control Flow def first(x): print('starting first.') try: second(x) except: print('caught at first') print('ending first') def third(x): print('starting third.') assert x < 1 print('ending third.') What is the output of first(2)? def second(x): print('starting second.') try: third(x) except: print('caught at second') print('ending second') 'Starting first.' 'Starting second.' 'Starting third.' 'Caught at second' 'Ending second' 'Ending first' 9/26/17 Asserts & Error Handling 26

Tracing Control Flow def first(x): print('starting first.') try: second(x) except: print('caught at first') print('ending first') def third(x): print('starting third.') assert x < 1 print('ending third.') What is the output of first(0)? def second(x): print('starting second.') try: third(x) except: print('caught at second') print('ending second') 9/26/17 Asserts & Error Handling 27

Tracing Control Flow def first(x): print('starting first.') try: second(x) except: print('caught at first') print('ending first') def third(x): print('starting third.') assert x < 1 print('ending third.') What is the output of first(0)? def second(x): print('starting second.') try: third(x) except: print('caught at second') print('ending second') 'Starting first.' 'Starting second.' 'Starting third.' 'Ending third' 'Ending second' 'Ending first' 9/26/17 Asserts & Error Handling 28