Use Python third-party library "Icecream" for debugging and printing

The article is reprinted to the public account: Python developer, for personal learning only.

Icecream is a Python third-party library, which can make printing and debugging more clear and clear with the minimum code. Use pip to install the Icecream library

pip install icecream

Import module icecream

from icecream import ic
from datetime import datetime
import time

1. View function output print information

# 1、查看函数输出打印信息
def plus_five(num):
    return num + 5
def plus_five(num):
    return num + 5
# 通过使用icecream,不仅可以看到函数输出,还可以看到函数及其参数情况。
ic(plus_five(5))
ic(plus_five(10))

Output result:

ic| plus_five(5): 10
ic| plus_five(10): 15

2. Check the implementation

# 如果想要执行代码的位置,可以通过执行如下所示的操作,来查找执行了哪个语句
def hello(user:bool):
    if user:
        print("I'm user")
    else:
        print("i'm not user")

hello(user=True)

Output result:

I'm user

Using icecream, you can easily complete the above actions without unnecessary text information:

def hello(user:bool):
    if user:
        ic()
    else:
        ic()
# 使用icecream无须多余的文本信息,就可以轻松完成上述动作
hello(user=True)
hello(user=False)

Output result:

ic| icecream_demo.py:32 in hello() at 07:30:29.492
ic| icecream_demo.py:34 in hello() at 07:30:29.494

3. Custom prefix

def time_flag():
    return f'{datetime.now()}|>'

ic.configureOutput(prefix=time_flag)
for i in range(3):
    time.sleep(2)
    ic("歪比巴卜")

You can see the execution time of the code, which is displayed in front of the output:

2021-02-03 15:09:58.104343|>'歪比巴卜'
2021-02-03 15:10:00.120383|>'歪比巴卜'
2021-02-03 15:10:02.121633|>'歪比巴卜'

4. Get more information.
In some cases, in addition to knowing the output-related code, you may also want to know the code execution line and code file, then in ic.configureOutput(), set the parameter value of includecontext to True That's it.

def ic_hi(hi):
    return hi + "!"
ic.configureOutput(includeContext=True)
ic(ic_hi("hi"))
ic(ic_hi("hello"))

The output is as follows:

ic| icecream_demo.py:57 in <module>- ic_hi("hi"): 'hi!'
ic| icecream_demo.py:58 in <module>- ic_hi("hello"): 'hello!'

From the output results, we can know that the first output is executed by the function ic_hi on line 57 of the file icecream_demo.py, and the second output is executed by the function ic_hi on line 58 of the code file.
The above two operations both use the ic.configureOutput() function. The following is the source code of the configureOutput function:

def configureOutput(self, prefix=_absent, outputFunction=_absent,
                    argToStringFunction=_absent, includeContext=_absent):
    if prefix is not _absent:
        self.prefix = prefix

    if outputFunction is not _absent:
        self.outputFunction = outputFunction

    if argToStringFunction is not _absent:
        self.argToStringFunction = argToStringFunction

    if includeContext is not _absent:
        self.includeContext = includeContext

By looking at the source code, we know that there are four parameters that can be set:

  • prefix, custom output prefix
  • outputFunction, change the output function
  • argToStringFunction, custom parameter serialization string
  • includeContext, display file name, code line, function information

5. Delete the Icecream code.
Finally, you can use icecream only for debugging, and print for other purposes (such as beautiful printing)

def ic_hi(hi):
    return hi + "!"
ic.configureOutput(includeContext=True)
ic(ic_hi("hi"))
ic(ic_hi("hello"))
for i in range(10):
    print(f"--**--<{i}>--**--")

Output result:

ic| icecream_demo.py:89 in <module>- ic_hi("hi"): 'hi!'
ic| icecream_demo.py:90 in <module>- ic_hi("hello"): 'hello!'
--**--<0>--**--
--**--<1>--**--
--**--<2>--**--
--**--<3>--**--
--**--<4>--**--
--**--<5>--**--
--**--<6>--**--
--**--<7>--**--
--**--<8>--**--
--**--<9>--**--

Because debugging printing and print printing can be distinguished, it is very easy to search and delete all ic debugging statements:
you can use ctrl+f to search globally: ic
or use one key to select and modify multiple identical variables, symbols, and values: ctrl+shift+alt +j, select the corresponding ic print code globally and delete it.

Guess you like

Origin blog.csdn.net/weixin_52385863/article/details/113607845
Recommended