16
Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020 https://go.illinois.edu/cs105fa19 CS 105

L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020 CS 105

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Lecture 13: Exceptions and Modules

Craig Zilles (Computer Science)

May 1, 2020

https://go.illinois.edu/cs105fa19

CS 105

Page 2: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Today1. Exceptions• try/except, multiple exceptions, across functions

2. Modules• scripts, importing, paths

3. Larger Software Development• Stubs• Unit Testing

2

Page 3: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

What does this code do?prompt = 'type a number or q to quit\n'items = []while True:

item = input(prompt)if item == 'q':

breakitems.append(float(item))

value = sum(items) / len(items)print(value)

3

Page 4: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Where can an exception occur?prompt = 'type a number or q to quit\n'items = []while True:

item = input(prompt)if item == 'q':

breakitems.append(float(item))

value = sum(items) / len(items)print(value)

4

A

B

C

D

E

Page 5: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Exception Handling: Big Idea• Don't crash the program; gracefully handle user errors• Do it in a way that doesn't encumber the normal path• Exception handling (like objects) is complexity

management

try:# do potentially bad things

except:# handle bad things if they happen

5

Page 6: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Can differentiate between exceptions try:

# do potentially bad things

except ValueException as e:# handle ValueExceptions# e is bound to exception object

except ZeroDivisionError:# ...

except:# handle any other exceptions

6

Page 7: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Exceptions handed up "call chain"• Unhandled exceptions passed to caller

def f():x = 1/0print('hi')

def g():f()print('hello')

g()7

What gets printed?

A) Just hiB) Just helloC) Both hi and helloD) Nothing

Page 8: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Annoucements• 5% curve on Exam 2, 10% curve on Exam 3• Clicker points capped at 150• Lab after break: Data Analysis• Quiz 4: 12/5 - 12/7• Mix of Excel and Python content

• Final Exam• Almost all Python (can't run Excel in CBTF)

• Hiring Course Assistants for Spring: (see mass mail)• https://forms.gle/XqHiiphAYYJRXUHr8

8

Page 9: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Modules• One of the main uses of computing is automation• Often you want to do similar things repeatedly

• Write a piece of code once and use it again and again• Put the code into a file (e.g., useful.py) • Import that file as a module• import useful• useful.py must be in Python's path

• Path is set of directories where Python will look for file• Most easily done by running Python where the file is

9

Page 10: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

useful.py defines useful_function

After:import usefulHow to call useful_function with "hi" as argument

A) function("hi")B) useful_function("hi")C) useful.function("hi")D) useful.useful_function("hi")E) None of the above

10

Page 11: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

If execute.py includes:useful_function("during loading")

if __name__ == "__main__":

useful_function("WELCOME TO THE MACHINE")

What is printed if I run: import execute

A) NothingB) Just *** during loading ***C) Just *** WELCOME TO THE MACHINE ***D) Both *** during loading *** and

*** WELCOME TO THE MACHINE ***E) An error occurs

11

Page 12: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

If execute.py includes:useful_function("during loading")if __name__ == "__main__":

useful_function("WELCOME TO THE MACHINE")

What is printed if I run: python execute.py

A) NothingB) Just *** during loading ***C) Just *** WELCOME TO THE MACHINE ***D) *** during loading *** then

*** WELCOME TO THE MACHINE ***E) *** WELCOME TO THE MACHINE *** then

*** during loading ***

12

Page 13: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Larger software development• When building larger programs …

• Can't build it all at once: use modular design• Break it up into logical pieces • Those become modules, objects• Manages complexity

• Test each piece individually• Using unit tests

• Assemble pieces into more and more complete systems

13

Page 14: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Function Stubs• Writing a larger programs, can't write everything at once• Outline!

def solve_climate_change(world_status):raise NotImplementedError

14

Page 15: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Unit testing• "If it hasn't been tested, then it doesn't work"• Very hard to prove code works in every circumstance• Pick cases that are representative of all cases

• What inputs should be tested for this code?def compare(x, y):

"return True if x is at least 5 greater than y, False otherwise"

15

Page 16: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020  CS 105

Actual test cases for PL questionsclass Test(unittest.TestCase):

@points(1)

@name("Testing with 'hi' and 'there'")

def test_one(self):r = concatenate_two_strings('hi',

'there')

self.assertEqual(r, 'hithere')

16