Why
- Like a print statement but better
- Doesn’t accidentally get left in the code
- Contains a complete snapshot of the status of the program
- Let’s you walk through the code dynamically
Break points
- Insert breakpoint
- Press Debug
- Program runs until breakpoint and stops (before executing the line of code)
- Stack Data gives the current values for all variables (Local vs. Global)
- Pressing Debug again continues to the next breakpoint
Stepping
- Step Into – goes line by line through the code including entering functions
- Step Over – does not enter functions
- Step Out – leaves the current function and return to whatever called it
Conditional breakpoints (Pro and other debuggers)
- Breakpoints can be made conditional
- Only stop the program if certain conditions are met
- Right click the breakpoint and add condition
Example - Pleistocene Overkill
Mistakes
- Looping over all continents instead of unique
- Missing underscores in results.append()
- Missing dtype=None in import
Process
- Run, get error
- Run debugger, highlights line with error, show Stack Data
- Fix missing underscores
- Restart
- Lots of nan’s, likely a problem with the data so look at it
- Fix missing dtype=None and rerun
- That fixed one of our problems, but now too many lines, not easy to see, so
- Set a break point at start of loop
- Demo step in/over/out
- Use unique not actual
Code
import numpy as np
def mean_mass(masses):
"""Return the mean of a list of masses"""
mean_mass = np.mean(masses)
return masses
all_data = np.genfromtxt('MOMv3.3.txt', delimiter='\t', dtype=None,
names=['continent', 'status', 'order', 'family',
'genus', 'species', 'log10mass', 'mass', 'ref'])
continents = all_data['continent']
status = all_data['status']
masses = all_data['mass']
results = []
for continent in continents:
extinct_masses = masses[(status=='extinct') & (continents==continent)]
extant_masses = masses[(status=='extant') & (continents==continent)]
avg_extinct_mass = np.mean(extinct_masses)
avg_extant_mass = np.mean(extant_masses)
diff = avg_extant_mass - avg_extinct_mass
results.append([continent, avg_extantmass, avg_extinctmass, diff])
for line in results:
print line