Interactive Debugging in Python
Pages: 1, 2, 3, 4, 5, 6, 7
An Advanced Example
Now that you know what the debugger is, how to get it to run your code, and what the basic commands do, it's time to walk through a meatier example. The following code reads in a text file a line at a time, splits the line on white space, and converts the line to a dictionary with stringified word positions as its keys and the integer values of the words themselves as values for those keys:
#!/usr/bin/env python
import pdb
import string
import sys
class ConvertToDict:
def __init__(self):
self.tmp_dict = {}
self.return_dict = {}
def walk_string(self, some_string):
'''walk given text string and return a dictionary.
Maintain state in instance attributes in case we hit an exception'''
l = string.split(some_string)
for i in range(len(l)):
key = str(i)
self.tmp_dict[key] = int(l[i])
return_dict = self.tmp_dict
self.return_dict = self.tmp_dict
self.reset()
return return_dict
def reset(self):
'''clean up'''
self.tmp_dict = {}
self.return_dict = {}
def get_number_dict(self, some_string):
'''do super duper exception handling here'''
try:
return self.walk_string(some_string)
except:
#if we hit an exception, we can rely on tmp_dict
being a backup to the point of the exception
return self.tmp_dict
def main():
ctd = ConvertToDict()
for line in file(sys.argv[1]):
line = line.strip()
print "*" * 40
print "line>>", line
print ctd.get_number_dict(line)
print "*" * 40
if __name__ == "__main__":
#pdb.runcall(main)
main()
Note that I have pdb.runcall(main) commented out. This will
make it easy to drop into the debugger in a moment. Here is a simple example
input file:
jmjones@bean:~/debugger $ cat simple_example.data
1234 2345 3456 4567
9876 8765 7654 6543
Here is the output from running the script on that file:
jmjones@bean:~/debugger $ python example_debugger.py simple_example.data
****************************************
line>> 1234 2345 3456 4567
{'1': 2345, '0': 1234, '3': 4567, '2': 3456}
****************************************
****************************************
line>> 9876 8765 7654 6543
{'1': 8765, '0': 9876, '3': 6543, '2': 7654}
****************************************
Now, given the following input file:
jmjones@bean:~/debugger $ cat example_debugger.data
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4
1 2 3 4
the script produces this output:
jmjones@bean:~/debugger $ python example_debugger.py example_debugger.data
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{'1': 2, '0': 1, '3': 4, '2': 3, '5': 6, '4': 5, '7': 8, '6': 7, '9': \
10, '8': 9}
****************************************
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{'1': 2, '0': 1, '3': 4, '2': 3, '5': 6, '4': 5, '7': 8, '6': 7}
****************************************
****************************************
line>> 1 2 3 4
{'1': 2, '0': 1, '3': 4, '2': 3, '5': 6, '4': 5, '7': 8, '6': 7}
****************************************
****************************************
line>> 1 2 3 4
{'1': 2, '0': 1, '3': 4, '2': 3}
****************************************
Something is obviously amiss. The first and second lines should be the same, as should the third and fourth lines. What to do? Start randomly
inserting print statements throughout the source file or fire up
the debugger? How about fire up the debugger? Uncomment the debugger line,
comment out the main() line in the source file, and then:
jmjones@bean:~/debugger $ python example_debugger.py example_debugger.data
> /home/jmjones/debugger/example_debugger.py(35)main()
-> ctd = ConvertToDict()
(Pdb)