Today we'll cover dealing with errors in your Python code, an important aspect of writing software.
According to Wikipedia (accessed 16 Oct 2018), a software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or behave in unintended ways.
Engineers have used the term well before electronic computers and software. Sometimes Thomas Edison is credited with the first recorded use of bug in that fashion. [Wikipedia]
Let's discuss three major types of bugs in your code, from easiest to most difficult to diagnose:
In [1]:
import numpy as np
In [2]:
print( "This should only work in Python 2.x, not 3.x used in this class.")
In [3]:
x = 1; y = 2
b = x == y # Boolean variable that is true when x & y have the same value
b = 1 = 2
In [ ]:
# invalid operation
a = 0
5/a # Division by zero
In [ ]:
# invalid operation
input = '40'
input/11 # Incompatiable types for the operation
In [ ]:
str(21).index("1")
In [ ]:
import math
import numpy as np
'''Checks that Pythagorean identity holds for one input, theta'''
def check_pythagorean_identity(theta):
return math.sin(theta)**2 + math.cos(theta)**2 == 1
In [ ]:
check_pythagorean_identity(0)
In [ ]:
check_pythagorean_identity(np.pi)
Is our code correct?
Debugging has the following steps:
The detection of bugs is too often done by chance. While running your Python code, you encounter unexpected functionality, exceptions, or syntax errors. While we'll focus on this in today's lecture, you should never leave this up to chance in the future.
Software testing practices allow for thoughtful detection of bugs in software. We'll discuss more in the lecture on testing.
There are three main methods commonly used for bug isolation:
print
statements (or other logging techniques)pdb
.Typically, all three are used in combination, often repeatedly.
In [ ]:
import numpy as np
def entropy(p):
"""
arg p: list of float
"""
items = p * np.log(p)
return -np.sum(items)
In [ ]:
entropy([0.5, 0.5])
In [ ]:
import numpy as np
def improved_entropy(p):
"""
arg p: list of float
"""
items = p * np.log(p)
new_items = []
print("1 " + str(new_items))
for item in items:
if np.isnan(item):
pass
else:
new_items.append(item)
print("2 " + str(new_items))
return -np.sum(new_items)
In [ ]:
# Detailed examination of codes
p = [1, 0.0]
p * np.log(p)
In [ ]:
improved_entropy([1, 0.0])
Next steps:
pdb
Python comes with a built-in debugger called pdb. It allows you to step line-by-line through a computation and examine what's happening at each step. Note that this should probably be your last resort in tracing down a bug. I've probably used it a dozen times or so in five years of coding. But it can be a useful tool to have in your toolbelt.
You can use the debugger by inserting the line
import pdb; pdb.set_trace()
within your script. To leave the debugger, type "exit()". To see the commands you can use, type "help".
Let's try this out:
In [4]:
def debugging_entropy(p):
items = p * np.log(p)
if any([np.isnan(v) for v in items]):
import pdb; pdb.set_trace()
return -np.sum(items)
In [5]:
debugging_entropy([0.5, 0.5])
Out[5]:
This can be a more convenient way to debug programs and step through the actual execution.
In [6]:
p = [.1, -.2, .3]
debugging_entropy(p)