7

2个有用的Python命令行下可视化、调试、分析工具

 3 years ago
source link: https://blog.csdn.net/yanxiangtianji/article/details/116287735
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

loguru - 打印日志(特别是异常的详细信息)

loguru本身是一个日志库,跟logging库差不多,但是它还支持更丰富的一些功能,其中一项就是它可以显示抛出异常时的调用栈和各参数值。
使用方法很简单,我们只用使用loguru.logger.catch装饰我们要显示详细异常信息的函数即可。

比如下面的例子里,我建立了两个函数,一个fun_raw不用装饰器,fun_catch使用装饰器。它们的作用都是调用basic函数。我这里之所以多搞出一层basic函数,是为了展示调用栈的返回效果。

from loguru import logger
my_list = [1, 2, 3]
my_dict = { 'a': 'b' }

def basic(a,b):
	return a+b

def fun_raw(a, b):
    t = basic(a,b)
    return t

@logger.catch
def fun_catch(a, b):
    t = basic(a,b)
    return t

下面我们调用基本的函数,会得到普通的异常信息。

fun_raw(my_list, my_dict)
# 输出的异常信息如下:
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
<ipython-input-5-339452031bfc> in <module>()
----> 1 fun_raw(my_list, my_dict)

<ipython-input-4-b6eabadbdba9> in fun_raw(a, b)
      7 
      8 def fun_raw(a, b):
----> 9     t = basic(a,b)
     10     return t
     11 

<ipython-input-4-b6eabadbdba9> in basic(a, b)
      4 
      5 def basic(a,b):
----> 6         return a+b
      7 
      8 def fun_raw(a, b):

TypeError: can only concatenate list (not "dict") to list

下面我们调用使用装饰器装饰后的函数,我们会得到包含参数值的详细调用栈信息。

fun_raw(my_list, my_dict)

# 输出的部分异常信息如下:
---------------------------------------------------------------------------
> File "<ipython-input-6-943019db22e9>", line 1, in <module>
    fun_catch(my_list, my_dict)
    │         │        └ {'a': 'b'}
    │         └ [1, 2, 3]
    └ <function fun_catch at 0x7f0e34b138c0>
  File "<ipython-input-4-b6eabadbdba9>", line 14, in fun_catch
    t = basic(a,b)
        │     │ └ {'a': 'b'}
        │     └ [1, 2, 3]
        └ <function basic at 0x7f0e5ac99050>
  File "<ipython-input-4-b6eabadbdba9>", line 6, in basic
    return a+b
           │ └ {'a': 'b'}
           └ [1, 2, 3]

TypeError: can only concatenate list (not "dict") to list

项目主页:https://github.com/Delgan/loguru
安装方法:

pip install loguru

snoop - 打印执行情况

它可以显示每一行正在执行的代码,以及这一行代码的行号、变量值、返回值、执行时间。对于研究代码行为、分析异常原因非常有用,特别是使用jupyter的时候。

它的使用也非常容易,可以直接用@snoop装饰要分析的函数,或者使用with snoop:语法来包裹要分析的部分。

下面这个例子里,我实现了一个将一个数字转换成它的二进制表示的小函数。

import snoop

@snoop
def number2bits(number):
    bits = []
    while number:
        number, remainder = divmod(number, 2)
        bits.insert(0, remainder)
    return bits

number2bits(5)

我们来分析它在参数值为5的时候的执行过程。

01:09:55.71 >>> Call to number2bits in File "<ipython-input-10-501aa33d50e5>", line 4
01:09:55.71 ...... number = 5
01:09:55.71    4 | def number2bits(number):
01:09:55.71    5 |     bits = []
01:09:55.71    6 |     while number:
01:09:55.72    7 |         number, remainder = divmod(number, 2)
01:09:55.72 .............. number = 2
01:09:55.72 .............. remainder = 1
01:09:55.72    8 |         bits.insert(0, remainder)
01:09:55.72 .............. bits = [1]
01:09:55.72 .............. len(bits) = 1
01:09:55.72    6 |     while number:
01:09:55.72    7 |         number, remainder = divmod(number, 2)
01:09:55.73 .............. number = 1
01:09:55.73 .............. remainder = 0
01:09:55.73    8 |         bits.insert(0, remainder)
01:09:55.73 .............. bits = [0, 1]
01:09:55.73 .............. len(bits) = 2
01:09:55.73    6 |     while number:
01:09:55.73    7 |         number, remainder = divmod(number, 2)
01:09:55.73 .............. number = 0
01:09:55.73 .............. remainder = 1
01:09:55.73    8 |         bits.insert(0, remainder)
01:09:55.73 .............. bits = [1, 0, 1]
01:09:55.73 .............. len(bits) = 3
01:09:55.73    6 |     while number:
01:09:55.74    9 |     return bits
01:09:55.74 <<< Return value from number2bits: [1, 0, 1]
[1, 0, 1]

项目主页:https://github.com/alexmojaki/snoop
https://pypi.org/project/snoop/
它的功能很丰富,更多的功能可用参考项目主页。
安装方法:

pip install snoop

heartrate - 显示实时执行情况

它不是在命令行下直接显示的,但是功能也很强大,所以也介绍一下。它是snoop的作者在snoop的基础上开发的。它可用实时显示Python程序的运行情况。
运行下面的命令之后,它会在本地的9999端口开启一个web page。然后我们可以通过http://localhost:9999/打开,使用浏览器观察代码运行情况。当然这些都是可以改的。而且除了直接监视后续运行的代码,也可以指定特定文件。

import heartrate
heartrate.trace(browser=True)

下图是来自官方主页的效果动图,它实时显示了merge_sort算法在执行过程中各行代码的执行次数。我们可以把它当做一个profiler来用。
heartrate 运行效果

项目主页:https://github.com/alexmojaki/heartrate
安装方法:

pip install heartrate

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK