1 of 15

Itemgetter, Sorting, and Debugging/Testing

Winter 2025

1

Adrian Salguero

2 of 15

Announcements

  • Homework 5, Part 1 due this Friday at 11:59pm
  • Coding Practice 6 due this Wednesday at 11:59pm
  • John (your TA) will be leading lecture on Wednesday and Friday
  • Final Exam will be similar style to midterm
    • More information coming soon!

2

3 of 15

Review: Itemgetter and Sorting

Review solutions to the additional practice problems here

3

4 of 15

Situation: Your code does not work!

  • What tools do we have available to fix things?
  • How do we know it’s not working?
  • What is it supposed to do?
  • What is it doing instead?
  • Where in the code is the error?
  • What kind of error is it?

4

5 of 15

Three classifications of errors

  1. Syntax errors
    1. Example: for in range 2:
  2. Runtime and arithmetic errors
  3. Logical errors

5

6 of 15

Options on how to debug

  • Testing with assert
  • Error messages from Python
  • Print statements
  • Python interpreter
  • Python Tutor
  • Python debugger

6

7 of 15

Tips on how to debug

  • Be systematic
    • Never do anything if you don’t have a reason to
    • Don’t just flail or “throw things at the wall”
      • Randomly guessing is likely to make the problem worse
  • Don’t make assumptions
    • Think carefully about why your code is not working
  • Read the error messages

7

8 of 15

Read the error message

Traceback (most recent call last):

File "nx_error.py", line 41, in <module>

print(friends_of_friends(rj, myval))

File "nx_error.py", line 30, in friends_of_friends

f = friends(graph, user)

File "nx_error.py", line 25, in friends

return set(graph.neighbors(user))#

File "/Library/Frameworks/…/graph.py", line 978, in neighbors

return list(self.adj[n])

TypeError: unhashable type: 'list'

List of all exceptions (errors):

http://docs.python.org/3/library/exceptions.html#bltin-exceptions

Two other resources, with more details about a few of the errors:

http://inventwithpython.com/appendixd.html

http://www.cs.arizona.edu/people/mccann/errors-python

First function that was called (<module> means the interpreter)

Second function that was called

Last function that was called (this one suffered an error)

The error message:�daunting but useful.�You need to understand:

  • the literal meaning of the error
  • the underlying problems certain errors tend to suggest

9 of 15

Common Error Types

  • AssertionError
    • Raised when an assert statement fails.
  • IndexError
    • Raised when a sequence (e.g. list, string, tuple) subscript is out of range.
  • KeyError
    • Raised when a dictionary key is not found in the set of existing keys.
  • KeyboardInterrupt
    • Raised when the user hits the interrupt key (normally Control-C or Delete).
  • NameError
    • Raised when a local or global name is not found.
  • SyntaxError
    • Raised when the parser encounters a syntax error. (e.g. missing closing parenthesis)
  • IndentationError
    • Syntax errors related to incorrect indentation.
  • TypeError
    • Raised when an operation or function is applied to an object of inappropriate type.

9

10 of 15

What's the most important aspect of a program?

  1. Correctness
    1. (Does it solve the right problem in the right way?)
  2. Speed
    • (Does it solve it fast enough?)
  3. Style
    • (Does it look good along the way?)

10

11 of 15

Correctness

Pros

  • What good is a program that claims to solve for x but doesn't?
  • … or one that never comes up with the right x?

Cons

  • What good is a program that claims to solve for x but takes 10 years to do so?
  • How long does it take to get a program to 100% correctness?
    • (What does that even mean for some problems?)

11

12 of 15

Correctness

  • Correctness is the most important property of a program!

  • We’ve talked about:
    • Testing
    • Debugging

12

13 of 15

Speed

Pros

  • If a program takes a really long time to run, how can you test if it's correct?
    • … let alone get its results?
  • Who are the users of this program? Their own work is likely impacted by the speed of your program.

Cons

  • Does speeding up the program make it extremely hard to debug?
  • If all your effort is put towards engineering the program to be faster, how much effort is being put towards correctness?

13

14 of 15

Style

Pros

  • If the program needs updates or bug fixes (all of them do eventually), without good style this would take all of your energy.
  • "Time is money" and making it hard for other people to understand the program costs significant time.

Cons

  • Does your style "look good" to others? Does it matter?
  • How much time is being put towards style vs. correctness or speed?
  • What tools exist to do this for you and to what degree?

14

15 of 15

Style

  • Programs are read by humans
    • Good style is important so human can understand what you are doing
      • to modify your code and re-use it
      • to debug your code
    • Sometimes this human is you
    • Sometimes it is another person
    • Sometimes it is you a year+ later
    • Sometimes it is another person a year+ later

15