0. 作用
代码调试工具,在部分场景下,提升代码调试效率
1. 安装
pip install PySnooper
2. 简单使用
以函数为单位进行调试的,它默认只会跟踪函数体内的局部变量

3. 详细使用
3.1 重定向到日志文件
添加传参output=filename

3.2 跟踪非局部变量
添加传参watch="参数名"


-
watch
接收一个可迭代对象,如list、tuple -
watch
接收的单个对象,建议使用字符串,否则会报错:TypeError: compile() arg 1 must be a string, bytes or AST object
3.3 跟踪列表或字典的所有项/属性
添加传参watch_explode=("a",)
# func()添加装饰器
@pysnooper.snoop(output=filename, watch_explode=("a", "dict2"))

3.4 跟踪函数深度
添加传参depth=2
,默认1

3.5 设置调试日志的前缀
添加传参prefix
方便为不同的函数添加不同的标志,以便区分
@pysnooper.snoop(output=filename, prefix="百草:")

3.6 设置最大输出长度
添加传参max_variable_length
,默认100
默认情况下,PySnooper 输出的变量和异常信息,如果超过 100 个字符,被会截断为 100 个字符
设置None,则表示从不拦截

3.7 支持多线程调试模式
添加传参thread_info=True
,默认False
@pysnooper.snoop(output=filename, thread_info=True)
def func():
...

3.8 支持自定义对象的输出
添加传参custom_repr
,接收元组对象,默认空元组
元组对象中,可以定义:指定特定类型的对象以特定格式进行输出
方法一:(XX类型,custom_repr_func自定义函数)
PySnooper 在打印对象的调试信息时,会逐个判断它是否是 XX类型的对象,若是,就将该对象传入custom_repr_func函数中,由该函数来决定如何显示这个对象的信息。
注:
XX类型,可以是类即自定义数据类型,也可以是基本数据类型,如list


方法二:(判断条件,custom_repr_func自定义函数)

注:
网友评论