Detailed explanation of DeBug Python God level tool PySnooper

  • 2021-07-09 08:36:39
  • OfStack

PySnooper laughs at itself on GitHub as a "beggar version" debugging tool (poor man 's debugger).

In general, when writing Python code, if you want to find out why Python code is not executing as expected, which code is running and which is not running, and what local variables are, you will use a debugger with functions such as breakpoints and observation modes, or print it out directly using print statements.

However, the above methods are troublesome, for example, using the debugger requires tedious settings, and printing with print should be very careful. In contrast to them, using PySnooper only requires adding a decorator to the function to be debugged, so that you can get the detailed log of the running function, including the line of code to execute and the execution time, as well as the exact time when local variables change.

The reason why it is called "beggar version" is believed that PySnooper is 10 points simple to use, and developers can use it in any huge code base without any setup. Simply add a decorator and specify a path for the log output address.

GitHub Project Address

Installation


pip3 install pysnooper

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 return log is as follows

Starting var:.. number = 6
21:14:32.099769 call 3 @pysnooper.snoop()
21:14:32.099769 line 5 if number:
21:14:32.099769 line 6 bits = []
New var:....... bits = []
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
New var:....... remainder = 0
Modified var:.. number = 3
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
Modified var:.. number = 0
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 10 return bits
21:14:32.099769 return 10 return bits

PySnooper feature

If the standard error output is difficult to obtain or is too long, you can navigate the output to a local file:


@pysnooper.snoop('/my/log/file.log')

View the values of 1 non-local variables:


@pysnooper.snoop(variables=('foo.bar', 'self.whatever'))

Show the snoop line of the calling function in our function:


@pysnooper.snoop(depth=2)

Start all snoop lines with a prefix to make it easier to locate and find:


@pysnooper.snoop(prefix='ZZZ ')

It can be used to obtain various tensor information of TensorFlow, and 10 points is powerful. Mom doesn't have to worry that I can't find bug anymore!
(Update on May 7, 2019: Sometimes it doesn't work, I don't know if it's my wrong posture or other reasons.)


Related articles: