Today’s open source project is a debug tool for Python developers if you’re trying to figure out why your Python code isn’t doing what you think it’s doing. You might want to use a full-fledged debugger with breakpoints and monitoring capabilities, but you don’t have to worry about setting one up right now.

You want to know which rows are running and which are not, and what the values of local variables are.

Most people print at strategic locations using lines, some of which show the values of variables.

PySnooper allows you to do the same thing, except that print simply adds a decorator line to the function of interest, rather than elaborating the correct line. You’ll get a play-by-play log of the function, including which lines were run and when and exactly when local variables were changed.

What makes PySnooper stand out from all the other code intelligence tools? You can use it in a cumbersome enterprise code base without any setup. Simply use the decorator, as shown below, and redirect the output to the dedicated log file by specifying its path as the first parameter.

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)
Copy the code
Source path:... /my_code/foo.py Starting var:.. Call 4 def number_to_bits(number): 15:29:11.327032 Line 5ifNumber: 15:29:11.327032 Line 6 bits = [] New var:....... Bits = [] 15:29:11.327032 line 7whileNumber: 15:29:11.327032 Line 8 number, remainder = divmod(number, 2) New var:....... remainder = 0 Modified var:.. Number = 3 15:29:11.327032 line 9 bits. Insert (0, REMAINDER) Bits = [0] 15:29:11.327032 line 7whileNumber: 15:29:11.327032 LINE 8 number, remainder = divmod(number, 2) number = 1 Modified var:.. Insert (0, REMAINDER) Modified var:.. Bits = [1, 0] 15:29:11.327032 line 7whileNumber: 15:29:11.327032 LINE 8 number, remainder = divmod(number, 2) Number = 0 15:29:11.327032 line 9 bits. Insert (0, REMAINDER) Bits = [1, 1, 0] 15:29:11.327032 line 7whileNumber: 15:29:11. 327032 line 10returnBits 15:29:11. 327032return      10         return bits
Return value:.. [1, 1, 0]
Copy the code

Or, if you don’t want to trace the entire function, wrap the relevant parts in a with block:

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()
Copy the code
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 13print(lower, mid, upper)
Copy the code

Installation method

pip install pysnooper

Project Address:Github.com/cool-RR/PyS…

Do you like today’s recommendation? If you like the words, please leave a message at the bottom of the article or like, to express my support, your message, like, forward attention is I continue to update the power oh!

Pay attention to my public account reply: “1024”, free to receive a large wave of learning resources, first come, first served oh!