the python debug artifact PySnooper

I recommend this to my colleagues commissioning artifact, has no time to look, I looked at today.

Original link:

History of the most convenient Python Debug Tools | Tencent technology says

Experience the next, feeling the best usage: 1, line by line debugging priority; 2, bug complex process state or even now, you can use this library debugging; feeling not to overturn the traditional debugging tools, just let add more logs I facilitated it.

But from the perspective of my colleagues give back the development of his views,
slightly large python project only when development was progressive, they basically rely on the log server, but the client used to debug the debugger. Write desktop applications, debugger is easy, the service is not so simple, especially in multi-process services, general py debugger can only rely on remote debugging mode.

When complicated by multiple requests, each process hung on a different port debugger, very painful encounter timing issues, a debugger to intervene, even to reproduce is a problem

When logging accustomed to this way, just write your own program born with the same diagnosis function, then the debugger back and feel cumbersome. So I always felt good that we do log collection report

Snooper usage analysis, you will find that he used a python decorator syntax, what is it decorator?

Place it in a function defined at the beginning, it's like a hat worn on the head of this function. And the function to bind together. When we call this function, the first thing is not to perform this function, but this function as a parameter passed it the hat on his head, the hat we call 装饰函数 or 装饰器。

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 following article speaks quite complete.

An article decorators get to know all the uses (recommended collection)

1) interior trim function defines how to handle the specific needs of the incoming function:

# This is a decorative function 
DEF Logger (FUNC): 
    DEF wrapper (* args, ** kw): 
        Print ( 'I'm ready to start the calculation: {} function of:' format (FUNC .__ name__).) 

        # Real implementation is this Row. 
        FUNC (* args, ** kw) 

        Print ( 'Aha, I'm done for the calculation. give yourself a chicken !!') 
    return wrappe

2) before the service function to bring decorator function:

@logger
def add(x, y):
    print('{} + {} = {}'.format(x, y, x+y))

3) call

add(200, 50)

4) Output:

I'm ready to start the calculation: add a function:
 200 + 50 = 250 
Aha, I am done for the calculation. Give yourself a chicken!

 

Guess you like

Origin www.cnblogs.com/khacker/p/10992546.html