Slide Set 15 (Complete)

Similar documents
Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Exceptions CS GMU

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

ENCM 335 Fall 2018 Tutorial for Week 13

Slide Set 8. for ENCM 501 in Winter Steve Norman, PhD, PEng

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

ENCM 339 Fall 2017 Tutorial for Week 8

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

Slide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slides for Lecture 6

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

University of Calgary Department of Electrical and Computer Engineering ENCM 339: Programming Fundamentals, Section 01 Instructor: Steve Norman

Integer Multiplication and Division

ENCM 501 Winter 2016 Assignment 1 for the Week of January 25

MEIN 50010: Python Flow Control

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Fundamentals of Programming Session 4

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Intro. Scheme Basics. scm> 5 5. scm>

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slides for Lecture 15

Functions and Decomposition

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 5. for ENCM 501 in Winter Term, Steve Norman, PhD, PEng

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

ENCM 501 Winter 2018 Assignment 2 for the Week of January 22 (with corrections)

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Python Intro GIS Week 1. Jake K. Carr

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

Contents Slide Set 9. Final Notes on Textbook Chapter 7. Outline of Slide Set 9. More about skipped sections in Chapter 7. Outline of Slide Set 9

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CIS192 Python Programming

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

CIS192 Python Programming

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

Chapter 9: Dealing with Errors

Slide Set 8. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

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

Slide Set 1 (corrected)

Introduction to python

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

CIS192 Python Programming

Loop structures and booleans

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Unit E Step-by-Step: Programming with Python

Lecture 9: July 14, How to Think About Debugging

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

A Problem. Loop-Body Returns. While-Loop Solution with a Loop-Body Return. 12. Logical Maneuvers. Typical While-Loop Solution 3/8/2016

Text Input and Conditionals

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

The Dynamic Typing Interlude

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

CS 115 Lecture 4. More Python; testing software. Neil Moore

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

Errors. And How to Handle Them

A Little Python Part 3

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Programming and Data Structure

APT Session 2: Python

COMP 102: Computers and Computing

Slides for Lecture 15

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Writing to and reading from files

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

CS 103 Lab The Files are *In* the Computer

Type Checking and Type Equality

PREPARING FOR PRELIM 1

A Little Python Part 3

Class extension and. Exception handling. Genome 559

cs1114 REVIEW of details test closed laptop period

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

ENCM 501 Winter 2015 Tutorial for Week 5

Introduction to Python

Chapter 5 Errors. Bjarne Stroustrup

CIS192 Python Programming

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

ENCM 501 Winter 2019 Assignment 9

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Extended Introduction to Computer Science CS1001.py. Lecture 5 Part A: Integer Representation (in Binary and other bases)

Python Programming: An Introduction to Computer Science

Lecture Transcript While and Do While Statements in C++

Chapter 1 Introduction

CSc 120. Introduction to Computer Programming II. 07: Excep*ons. Adapted from slides by Dr. Saumya Debray

Transcription:

Slide Set 15 (Complete) for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2017

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 2/46 Contents About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 3/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 4/46 About this complete version Slides 5 31 are the same as in the incomplete version that was posted earlier. Slides 32 46 are new.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 5/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 6/46 Dealing with failure Things can go wrong when programs run. For example: Users can enter letters when a program needs digits. Attempts to open files can fail. Operations with a file can fail after the file has successfully been opened. C programmers are expected to check return values of functions for error situations, as outlined in the sketch of C code on the next slide...

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 7/46 result = do_something( /* arguments */ ); if (result == BAD) { /* deal with the error somehow */ } else { } /* do the normal work of the program */ do_something is a generic representative of functions such as scanf, fopen, fread, fclose, malloc, and many, many others.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 8/46 There are at least two serious and valid criticisms of the C approach to failure checking: The code to manage failures makes programs hard for humans to read the normal program flow when failure doesn t happen can be hard to pick out. This is especially the case when multiple failures are possible, leading to if-statements within if-statements within if-statements. Programmers sometimes just don t write code needed to check for failure, because they believe (correctly or incorrectly) that some kinds of failure are very unlikely to happen.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 9/46 Exception handling Exception handling is an approach to failure management that is quite different from checking function return values for error indications. Python and Java are prominent examples of languages that use exception handling to manage failure events. (C++, an extremely powerful, flexible, and messy language, makes its programmers worry about both error-indicating return values and exception handling.)

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 10/46 Advantages of exception handling Here are two: Compared to looking for error-indicating return values, exception handling allows significantly better separation of deal with the unusual error code from deal with the normal no-error case code. If exceptions are not handled, mechanisms such as the Python traceback system can help developers understand the exact cause of a program failure.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 11/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 12/46 How Python exceptions work The next few slides are intended to illustrate the basic mechanisms of Python and are not provided as models for good software design! The program on the slide 13 has code that tries to add 120 to an int values encoded in a string, in a rather awkward way. Slide 14 shows what happens when it runs. Let s make some detailed notes about the sequence of events that lead to program termination with traceback.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 13/46 1 def add_20(s): 2 print( add_20 arrival ) 3 q = int(s) + 20 4 print( add_20 departure ) 5 return q 6 7 def add_120(t): 8 print( add_120 arrival ) 9 r = add_20(t) + 100 10 print( add_120 departure ) 11 return r 12 13 print( 1st result:, add_120( 4 )) 14 print( 2nd result, add_120( xyz ))

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 14/46 add_120 arrival add_20 arrival add_20 departure add_120 departure 1st result: 124 add_120 arrival add_20 arrival Traceback (most recent call last): File "program1.py", line 14, in <module> print( 2nd result, add_120( xyz )) File "program1.py", line 9, in add_120 r = add_20(t) + 100 File "program1.py", line 3, in add_20 q = int(s) + 20 ValueError: invalid literal for int() with base 10: xyz

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 15/46 The try statement The program on slide 16 is similar to the previous Python program, but the new program puts its calls to add_120 inside a thing called a try statemennt. The output of the new program is on slide 17.

1 def add_20(s): 2 print( add_20 arrival ) 3 q = int(s) + 20 4 print( add_20 departure ) 5 return q 6 7 def add_120(t): 8 print( add_120 arrival ) 9 r = add_20(t) + 100 10 print( add_120 departure ) 11 return r 12 13 try: 14 print( 1st result:, add_120( 4 )) 15 print( 2nd result, add_120( xyz )) 16 except ValueError as value_error: 17 print( something went wrong... ) 18 print(value_error)

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 17/46 add_120 arrival add_20 arrival add_20 departure add_120 departure 1st result: 124 add_120 arrival add_20 arrival something went wrong... invalid literal for int() with base 10: xyz Let s make some notes about how the new program differs in behaviour from the older program.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 18/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 19/46 try statement syntax and rules for execution There are several different variations of try statement syntax. Let s just look at two simple ones instead of trying to cover all of the possibilities. Let s make some notes about the following syntax, which is the syntax used in the most recent example program. try: statements for usual program activity except exception class type as object name : statements to deal with the exception

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 20/46 If you are not interested in having an object to print or otherwise get information from, you may use this slightly simpler syntax... try: statements for usual program activity except exception class type : statements to deal with the exception In either kind of try statement, the part of the statement that begins with except is called an except clause.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 21/46 Rules for execution of except clauses Will an except clause handle an exception of a specific type, or will the clause choose not to handle the exception? The essential rules are most easily presented by example. What will happen when this tiny program runs, and why? try: x = int( pqr ) except ValueError: print( There was a ValueError exception. )

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 22/46 Behaviour with this next program will be quite different. Why? try: y = 19 + two except ValueError: print( There was a ValueError exception. ) The complete rules for matching the type of an exception against the type named in an except clause involve class inheritance in a major way. We ll look at the details of that later, if time permits.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 23/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 24/46 A simple practical example of exception handling A program that will add three integers typed in by a user is very much the opposite of an exciting application! But it shows how two programming goals can be accomplished... Users who are not Python programmers would like understandable error messages, not tracebacks. As much as possible, error-handling code should be separated from normal case code. Let s make some notes about the program on the next slide, then write out some example dialogs with the program.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 25/46 try: a = int(input( enter 1st int: )) b = int(input( enter 2rd int: )) c = int(input( enter 3rd int: )) f = {} + {} + {} = {}. print(f.format(a, b, c, a + b + c)) except ValueError: print( Problem trying get an int from input. )

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 26/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 27/46 Introduction to text file input/output with Python Let s look at text file input in Python first, then look at text file output later. Before we get to Python details, let s review some material from a slide set much earlier in the term...

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 28/46 The basic set of steps to read input from a file is common to most programming languages and libraries, and is not specific to C: 1. Obtain a file name. (In C, that means get a character string.) 2. Open the file for input make a connection between the program and the file. The connection will allow the program to read character codes from the file. 3. Read some characters from the file. 4. Close the file break the connection between the program and the file.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 29/46 Here s an overview of those steps in Python: 1. Obtain a file name. In Python, that means get a str object. 2. Open the file for input by calling the builtin open function. If this call succeeds, it will return a reference to a file object. (If the call fails, there will be an exception.) 3. Read some characters from the file, using method calls such as read, readline, and/or readlines on the file object. 4. Close the file by calling the close method on the file object.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 30/46 An old text file input example Suppose that we have some text files that contain nothing but representations of numbers separated by whitespace. Here are a few examples of these files... f1.txt 100 100 99 100 f2.txt 0.05 4.0e-2 0.07 f3.txt 9.0 9.5 8.5 7.5 10.0 By the way, what does 4.0e-2 mean?

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 31/46 Let s write a Python program that can read this kind of file, and tell the user how many numbers were in the file, what the sum of the numbers is, and what the average of the numbers is. The program should detect invalid input files, such as f4.txt 11 22 Hello! 33 In contrast to the C solution to this problem, let s do Step 1 (obtain a file name), by getting a string from the command line used to start the Python interpreter.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 32/46 The solution just given (in doc cam notes) to the problem described on slides 30 and 31 read the entire contents of an input file into program memory with a single method call. What are the advantages of taking that approach? What is potentially bad about reading all of a file into program memory?

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 33/46 Reading lines one at a time from a text file The readline method (which does not have s in its name) attempts to read a single line from an input file. What will be the program output, and why? def process_line(line): print(len(line)) f_object = open( foo.txt, r ) while True: line = f_object.readline() if len(line) == 0: break process_line(line) f_object.close() foo.txt contents: this is the file called foo.txt

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 34/46 The code on the previous slide works, but Python offers a much nicer way to do the job a text file open for reading can be processed as a sequence of lines... def process_line(line): print(len(line)) f_object = open( foo.txt, r ) for line in f_object: process_line(line) f_object.close() foo.txt contents: this is the file called foo.txt The output of the above program will be exactly the same as the output of the slide 33 program.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 35/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 36/46 A brief digression on class inheritance We ll soon see that this digression is quite relevant to the problem of designing exception-handling for text file operations. Recall the dog classes from Slide Set 14: SmartDog was the original base class; PlusDog and TimesDog each inherited directly from SmartDog; SmartestDog inherited directly from both PlusDog and TimesDog.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 37/46 Those sorts of relationships are often shown in inheritance diagrams, in which one or more arrows point from a class to the class(es) that it inherits from directly... SmartDog PlusDog TimesDog SmartestDog It s important to note that SmartestDog inherits indirectly from SmartDog. A SmartestDog object is a kind of SmartDog object.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 38/46 An object X is said to be an instance of class C if either of the following is true: the actual class of X is C; the class of X inherits directly or indirectly from C. In Python, it s easy to write code to ask if X is an instance of C using the builtin isinstance function. Assuming class definitions as given in Slide Set 14, what is the output of the program on the next slide?

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 39/46 def instance_check(dog): print( SmartDog?, isinstance(dog, SmartDog)) print( PlusDog?, isinstance(dog, PlusDog)) print( TimesDog?, isinstance(dog, TimesDog)) print( SmartestDog?, isinstance(dog, SmartestDog)) alice = SmartestDog( arf!, 3) print( facts about alice... ) instance_check(alice) bob = TimesDog( ruff!, 2) print( facts about bob... ) instance_check(bob) alice.say_age()

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 40/46 Inheritance and exception handling We re now in a position to tersely and precisely answer the question, When will the except clause handle an exception that may come from scary_function, and when will it not? def scary_function(a): result = 0 #... do things that might fail... return result try: x = scary_function(7) print(x) except FooError: print( A FooError has been handled. ) What s the answer?

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 41/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 42/46 Exceptions and file operations The general form of a call to open is variable = open( filename, mode ) where both arguments have type str. Here are some of the available modes: mode meaning r open text file for input w open text file for output, either creating a new file or replacing an existing one a open text file for output, either creating a new file or adding text at the end of an existing one x open text file for output, but only if the file does not already exist

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 43/46 Given the information on the previous slide, let s make an incomplete list of the ways open could fail and raise an exception. Then let s make some notes relating that list to this (very incomplete) inheritance diagram for Python Standard Library exception types... Exception OSError FileNotFoundError PermissionError FileExistsError

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 44/46 Let s sketch a general outline of Python code that can gracefully recover from a failure of a call to open. What is a flaw in this approach? Slide Set 16 will show how use of a with statement can avoid such flawed program behaviour.

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 45/46 Outline of Slide Set 15 (Complete) About this complete version Dealing with failure How Python exceptions work try statement syntax A simple practical example of exception handling Introduction to text file input/output with Python A brief digression on class inheritance Exceptions and file operations Coming up in Slide Set 16

ENCM 339 Fall 2017 Section 01 Slide Set 15 (Complete) slide 46/46 Coming up in Slide Set 16 For sure... Python text file output example(s) with statements and file operations Maybe... Python modules introduction to recursive problem-solving