Debugging method of qi09 original Python program
This article discusses using the PDB to debug a python program when no convenient IDE tool is available
Source code Example
For example, there are procedures for simulating tax calculations:
#!/usr/bin/pythondefDebug_demo (val):ifVal <= 1600 : Print "Level 1" Print0elifVal <= 3500 : Print "Level 2" Print(val-1600) * 0.05elifVal <= 6500 : Print "Level 3" Print(val-3500) * 0.10 + (3500-1600) * 0.05Else: Print "Level 4" Print(val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05#~def Debug_demoif __name__=="__main__": Debug_demo (4500)
The Debug_demo function calculates the tax required for 4500 of the recorded entries.
How to debug?
1. Adding Breakpoints
Add a red part of the code where you want to insert a breakpoint: If the _DEBUG value is true, start debugging at that point (the reason for joining _DEBUG is to facilitate debugging on/off).
#!/usr/bin/python
_debug=true
Def Debug_demo (val):
if _debug = = True:
Import PDB
Pdb.set_trace ()
If Val <= 1600:
Print "Level 1"
Print 0
Elif Val <= 3500:
Print "Level 2"
Print (val-1600) * 0.05
Elif Val <= 6500:
Print "Level 3"
Print (val-3500) * 0.10 + (3500-1600) * 0.05
Else
Print "Level 4"
Print (val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
#~def Debug_demo
if __name__ = = "__main__":
Debug_demo (4500)
2. Start Running debugging
Run the program./debug_demo.py, get
>/usr/local/qspace/user_network/debug_demo.py (7) Debug_demo ()
If Val <= 1600:
(PDB)
Val <= 1600: Indicates the currently executing statement, (PDB) waits for your debug instruction. The PDB commands are rich, and the input h instruction allows you to see how the instructions are used.
The following is a brief introduction to common directives:
View Code context, L (lowercase l)
(PDB) L
2 _debug=true
3 def Debug_demo (val):
4 if _debug = = True:
5 Import PDB
6 Pdb.set_trace ()
7-If Val <= 1600:
8 print "Level 1"
9 Print 0
Ten elif val <= 3500:
Print "Level 2"
Print (val-1600) * 0.05
(PDB)
The left is the line number and the right is the code body.
monitor variable: P variable name
(PDB) P Val
4500
(PDB)
Single Step execution: N
-Elif Val <= 3500:
(PDB) L
5 Import PDB
6 Pdb.set_trace ()
7 if Val <= 1600:
8 print "Level 1"
9 Print 0
Elif Val <= 3500:
Print "Level 2"
Print (val-1600) * 0.05
Elif Val <= 6500:
Print "Level 3"
Print (val-3500) * 0.10 + (3500-1600) * 0.05
Add Breakpoint: B line number
(Pdb) b 14
Run to breakpoint: C
(PDB) C
>/*****
Print "Level 3"
(PDB) L
9 Print 0
Ten elif val <= 3500:
Print "Level 2"
Print (val-1600) * 0.05
Elif Val <= 6500:
b-> print "Level 3"
Print (val-3500) * 0.10 + (3500-1600) * 0.05
+ Else:
Print "Level 4"
Print (val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
19
before executing to function return: R
(PDB) R
Level 3
195.0
--return--
>/**** ()
->none
Print (val-3500) * 0.10 + (3500-1600) * 0.05
(PDB)
Description:
The PDB also has many other useful instructions that readers can explore on their own. Enter the H,h command. You can get the detailed help of the command.
However, I personally think that generally do not need to start this debugging method, generally use the log output for debugging, unless you encounter a very subtle error. At this point, the power of one-step debugging is displayed.
[goto] Python program debugging methods