Another Python debugging tool

985439a5de94f28b26c3684aea4945ee.png

Lost little book boy

Needed after reading

5

minute

Speed ​​reading only takes 2 minutes

1

   

Introduction

During the programming process, errors are almost inevitable. In fact, programmers spend a lot of time debugging code in order to eliminate errors. During debugging, using print() statements to understand program flow and discover unexpected behavior is undoubtedly the most common method.

However, there are many problems with using print() statements, such as

  • The print statement is typically used to display output to the user. If the programmer uses print() for debugging, after debugging is complete, the programmer must be careful to remove specific print() statements that are used only for debugging.

  • During debugging, multiple variables may be printed continuously. In this case, the programmer must manually format the output for readability.

  • As the number of variables increases, it may be necessary to look back and forth between the code and the output to determine which variable each output corresponds to.

Fortunately, python provides us with an excellent alternative, icecream. By using minimal code, icecream makes debugging easy and readable.

2

   

Install

Use the pip command to install the icecream library

pip install icecream

3

   

Basic usage

Introduce the ic module into the code

from icecream import ic

Using the IceCream library is similar to the print statement, just replace print with ic.

from icecream import ic


var1 = 0
var2 = 1.0


ic(var1)
ic(var2)

code output

ic| var1: 0
ic| var2: 1.0

Noticed? ic() not only prints the value of the variable, but also the name of the variable.

4

   

More applications

icecream is not limited to variables, but can also be used for functions, classes, etc.

from icecream import ic


def func(num):
    return num * 2


ic(func(3))

code output

ic| func(3): 6

so cool! It prints the name of the method (func), the passed arguments (3), and the output value (6).

9d8cb28afaea1c63bfc171e0f0e4b6d5.jpeg

icecream's debugging function also applies to common Python data structures, such as dictionaries, see the example below

from icecream import ic


sample_dict = {1:"A", 2:"B", 3:"C"}


ic(sample_dict[1])

code output

ic| sample_dict[1]: 'A'

Many times, programmers will use the print() statement to print some meaningful statements to determine the execution stage of the program, such as

def func(input_num):
    if input_num == 1:
        print("If Executed!")
        ...


    else:
        print("Else Executed!")
        ...

Using icecream can achieve the purpose more elegantly.

from icecream import ic


def func(input_num):
    if input_num == 1:
        ic()
        ...


    else:
        ic()
        ...


func(2)

Just call ic() and it will print the file name, line number and other details like function name and time. Pretty simple and clear, right?

Next, you may be thinking, does every Python file need to import this library? The answer, of course, doesn’t have to be this way!

The method is to import icecream's install module in the entry file to make all project files available, such as

from icecream import install
install()


from help_file import func
func(2)

If you notice, the output of the ic() statement starts with "ic|", which is the default prefix provided by icecream. If you have special needs, you can use the prefix parameter in the ic.configureOutput() method to replace the default prefix, see the example below

from icecream import ic
ic.configureOutput(prefix='ic debug| -> ')


ic("A custom prefix")

code output

ic debug| -> 'A custom prefix'

Once the project is debugged, generally we want to delete all unnecessary debugging statements. You can use ic.disable() to stop printing ic(). If you want to use it again, just use ic.enable().

5

   

in conclusion

icecream is a powerful debugging tool in Python, which can make code debugging and error location easier, improving development efficiency and code quality. icecream is one of the tools that every Python programmer should master and use.

6

   

References

  • https://github.com/gruns/icecream ( https://github.com/gruns/icecream )

7

   

free community

eb0d5b44d4db21490423809a423123b0.jpeg

30e986c4a2c994950322c32f5af63c21.gif

Guess you like

Origin blog.csdn.net/djstavaV/article/details/132930340