The Use of python Debugging Artifact PySnooper

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

I believe that many small partners need to debug the program when writing python at ordinary times. If something goes wrong, they need to know how the function runs inside. At this time, many people will think of using print function to print 1 parameter to debug. Although using print is also a method, sometimes if there are many doubts, it is necessary to add print to every place, which is more troublesome.

Today, I found an open source artifact in Github, which can clearly let you know the internal operation of the function and the change of parameter values. PySnooper, project address: https://github.com/cool-RR/PySnooper

Simple and powerful to use, whoever uses it knows its good! Let's briefly talk about the usage of PySnooper under 1:

This tool is very simple to use. First, you can install it directly with pip: When you use pip install pysnooper, you only need to add a decorator in front of each function.
Just give a simple example,


import pysnooper
@pysnooper.snoop()
def removeDuplicates(nums):
  """
  :type nums: List[int]
  :rtype: int
  """
  flag = 0
  i=1
  while i<len(nums):
    if nums[i]==nums[i-1]:
      flag+=1
      i+=1
      if flag>=2:
        del nums[i-1]
        i-=1
    else:
      i+=1
      flag=0
  return len(nums)

nums = [1,1,1,2]
print(removeDuplicates(nums))

After adding the decorator, the running code will output the execution data of the corresponding function

Starting var:.. nums = [1, 1, 1, 2]
13:03:44.990194 call 11 def removeDuplicates(nums):
13:03:44.990695 line 16 flag = 0
New var:....... flag = 0
13:03:44.990695 line 17 i=1
New var:....... i = 1
13:03:44.990695 line 18 while i < len(nums):
13:03:44.990695 line 19 if nums[i]==nums[i-1]:
13:03:44.990695 line 20 flag+=1
Modified var:.. flag = 1
13:03:44.990695 line 21 i+=1
Modified var:.. i = 2
13:03:44.991193 line 22 if flag > =2:
13:03:44.991193 line 18 while i < len(nums):
13:03:44.991193 line 19 if nums[i]==nums[i-1]:
13:03:44.991193 line 20 flag+=1
Modified var:.. flag = 2
13:03:44.991193 line 21 i+=1
Modified var:.. i = 3
13:03:44.991193 line 22 if flag > =2:
13:03:44.991193 line 23 del nums[i-1]
Modified var:.. nums = [1, 1, 2]
13:03:44.991193 line 24 i-=1
Modified var:.. i = 2
13:03:44.991193 line 18 while i < len(nums):
13:03:44.991193 line 19 if nums[i]==nums[i-1]:
13:03:44.991193 line 26 i+=1
Modified var:.. i = 3
13:03:44.991693 line 27 flag=0
Modified var:.. flag = 0
13:03:44.991693 line 18 while i < len(nums):
13:03:44.991693 line 28 return len(nums)
13:03:44.991693 return 28 return len(nums)
Return value:.. 3

Of course, if you dislike this direct output and want to save log records, this decorator also has several optional parameters, such as:


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

Create the log directory first, and then output the log to the file file.


@pysnooper.snoop(prefix='removeDuplicates: ')

Adding a prefix name to the debugging line is easy to identify and locate. This parameter is applicable if multiple functions are debugged at the same time. My example here uses the function name as the prefix name. Excerpt 1 point log record as follows:


removeDuplicates: Starting var:.. nums = [1, 1, 1, 2]
removeDuplicates: 13:53:14.322036 call    11 def removeDuplicates(nums):
removeDuplicates: 13:53:14.323037 line    16   flag = 0

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

View non-local variables


@pysnooper.snoop(depth=2)

Display function call function snoop line, depth parameter range is greater than or equal to a positive integer, in the source code has such a judgment: assert self. depth > = 1, an exception is thrown when a value less than 1 is taken


Related articles: