PySnooper # is a very convenient debugger. If you're trying to figure out why your Python code doesn't do what you expect, you'll want to use mature debugging tools with breakpoints and monitoring, but many debugging tools are cumbersome to configure.
Now, with PySnooper, you don't need to configure such a complex Debug tool to complete the analysis of the whole code. It tells you which code is running and what the value of the local variable is.
In fact, pysnooper replaces the repetitive work of printing line by line, and gives your code a pysnooper decorator, which can automatically identify statements and variables and print their values:
import pysnooper @pysnooper.snoop() def number_to_bits(number): if number: bits = [] while number: number, remainder = divmod(number, 2) bits.insert(0, remainder) return bits else: return [0] number_to_bits(6)
The effects are as follows:
Source path:... 1.py Starting var:.. number = 6 23:03:35.990701 call 4 def number_to_bits(number): 23:03:35.991699 line 5 if number: 23:03:35.991699 line 6 bits = [] New var:....... bits = [] 23:03:35.991699 line 7 while number: 23:03:35.991699 line 8 number, remainder = divmod(number, 2) Modified var:.. number = 3 New var:....... remainder = 0 23:03:35.991699 line 9 bits.insert(0, remainder) Modified var:.. bits = [0] 23:03:36.004664 line 7 while number: 23:03:36.005661 line 8 number, remainder = divmod(number, 2) Modified var:.. number = 1 Modified var:.. remainder = 1 23:03:36.005661 line 9 bits.insert(0, remainder) Modified var:.. bits = [1, 0] 23:03:36.007657 line 7 while number: 23:03:36.007657 line 8 number, remainder = divmod(number, 2) Modified var:.. number = 0 23:03:36.008655 line 9 bits.insert(0, remainder) Modified var:.. bits = [1, 1, 0] 23:03:36.008655 line 7 while number: 23:03:36.009651 line 10 return bits 23:03:36.009651 return 10 return bits Return value:.. [1, 1, 0] Elapsed time: 00:00:00.020945
As you can see, it outputs the value of each line of variables to the screen, which is convenient for you to debug the code.
You only need to write one line of code - this convenient debugging function can be realized by using decorator, which is much more convenient than writing print line by line.
0. Install the module
To use this module, you only need to use Pip to install PySnooper:
pip install pysnooper
Next, let's talk about other easy-to-use functions of this module:
1. Support log files
If you find it inconvenient to print to the screen, you can also output it to the log file. You just need to change the line of the decorator to:
@pysnooper.snoop('/my/log/file.log')
2. Read external variables or other expressions
If you want to read the value of a variable or expression outside the scope of the decorator, you can also use the watch parameter:
@pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))
3. If you don't want to use decorator, you can also debug in the form of context
Yes, the decorator has limited conditions for use, so pysnooper also supports the context form of with:
import pysnooper import random def foo(): lst = [] for i in range(10): lst.append(random.randrange(1, 1000)) with pysnooper.snoop(): lower = min(lst) upper = max(lst) mid = (lower + upper) / 2 print(lower, mid, upper) foo()
The effect is as follows: only the code in the context can be debugged:
New var:....... i = 9 New var:....... lst = [681, 267, 74, 832, 284, 678, ...] 09:37:35.881721 line 10 lower = min(lst) New var:....... lower = 74 09:37:35.882137 line 11 upper = max(lst) New var:....... upper = 832 09:37:35.882304 line 12 mid = (lower + upper) / 2 74 453.0 832 New var:....... mid = 453.0 09:37:35.882486 line 13 print(lower, mid, upper) Elapsed time: 00:00:00.000344
This contextual debugging method is very convenient when we only need to debug part of the code.
This is the end of our article. If you like today's Python practical tutorial, please keep following me.