l14 exceptions modules sp20 - university of illinois · lecture 13: exceptions and modules craig...
TRANSCRIPT
![Page 1: L14 Exceptions Modules SP20 - University Of Illinois · Lecture 13: Exceptions and Modules Craig Zilles (Computer Science) May 1, 2020 CS 105](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052923/5f042d057e708231d40cafbc/html5/thumbnails/16.jpg)
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