码迷,mamicode.com
首页 > 其他好文 > 详细

代替print输出的PY调试库:PySnooper

时间:2019-05-11 13:38:56      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:sum   分析   github   代码   prompt   ota   output   span   install   

 

PySnooper

Github:https://github.com/lotapp/PySnooper

pip install pysnooper

使用:分析整个代码

@pysnooper.snoop()
def avg(args):
    return sum(args) / len(args)

PS:调试代码片段

with pysnooper.snoop():
    code
In [1]:
import functools
import pysnooper

# 分析整个代码
@pysnooper.snoop()
def sum(args):
    return functools.reduce(lambda x, y: x + y, args)

def avg(args):
    # return sum(args) / len(args)
    sum_num = sum(args)
    # 分析片段代码
    with pysnooper.snoop():
        avg_num = sum_num / len(args)
    return avg_num
In [2]:
# 求和
sum(list(range(1, 101)))
 
Starting var:.. args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
12:21:19.846556 call         6 def sum(args):
12:21:19.847553 line         7     return functools.reduce(lambda x, y: x + y, args)
12:21:19.847553 return       7     return functools.reduce(lambda x, y: x + y, args)
Return value:.. 5050
Out[2]:
5050
In [3]:
# 平均值
avg(list(range(1, 101)))
 
Starting var:.. args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
12:21:19.940523 call         6 def sum(args):
12:21:19.941526 line         7     return functools.reduce(lambda x, y: x + y, args)
12:21:19.941526 return       7     return functools.reduce(lambda x, y: x + y, args)
Return value:.. 5050
New var:....... args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
New var:....... sum_num = 5050
12:21:19.942524 line        14         avg_num = sum_num / len(args)
Out[3]:
50.5
 

代替print输出的PY调试库:PySnooper

标签:sum   分析   github   代码   prompt   ota   output   span   install   

原文地址:https://www.cnblogs.com/dotnetcrazy/p/10848257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!