关于ide:答应我别再用-print-调试代码了好么

5次阅读

共计 2426 个字符,预计需要花费 7 分钟才能阅读完成。

置信很多程序员在调试代码时,都用过 print。代码少还好说,如果是大型项目,面对泛滥 print 的输入后果,可能要头大了。

明天举荐一个 GitHub 热门开源我的项目:PySnooper

2019 年 4 月 23 日,该我的项目推出的第一天就播种 2000+ Star,登上了 GitHub 日榜第一位,现在有近 15k Star。Python 开发者应该会喜爱的。

链接:https://github.com/cool-RR/Py…

PySnooper 是个什么货色?

如果你写的 Python 代码不能按如期那样运行,你会搜索枯肠想为啥出错了。尽管你心愿有反对断点的成熟调试器,但或者你当初不想去设置这样的调试器。

你想晓得哪些行代码是失常运行,哪些行不失常。据说大多数人会在可疑地位应用 print 输入语句。

其实 PySnooper 的作用有点相似,你不必小心翼翼地用 print 输入,只需在想调试的函数中引入一个装璜器。而后失去函数的具体日志,包含运行了哪些行、何时运行,以及何时更改了局部变量。

为什么 PySnooper 能从其余智能调试工具中怀才不遇?

因为你能够在不须要进行任何设置的状况下将其用于蹩脚的、宏大的企业代码库中。只需关上装璜器(如下示例所示),并将输入重定向到一个专用的日志文件,将日志文件门路指定为第一个参数。

PS:如果无法访问 stderr,那能够将输入重定向到指定文件,比方:@pysnooper.snoop('/my/log/file.log')

应用范例

范例是一个把数字转成二进制的函数。

import pysnooper

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

number_to_bits(6)

输入后果

Starting var:.. number = 6
21:14:32.099769 call         3 @pysnooper.snoop()
21:14:32.099769 line         5     if number:
21:14:32.099769 line         6         bits = []
New var:....... bits = []
21:14:32.099769 line         7         while number:
21:14:32.099769 line         8             number, remainder = divmod(number, 2)
New var:....... remainder = 0
Modified var:.. number = 3
21:14:32.099769 line         9             bits.insert(0, remainder)
Modified var:.. bits = [0]
21:14:32.099769 line         7         while number:
21:14:32.099769 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
21:14:32.099769 line         9             bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
21:14:32.099769 line         7         while number:
21:14:32.099769 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 0
21:14:32.099769 line         9             bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
21:14:32.099769 line         7         while number:
21:14:32.099769 line        10         return bits
21:14:32.099769 return      10         return bits

如果你不想追踪整个函数,那能够用 with 块包装你想追踪的那局部,如下:

import pysnooper
import random

def foo():
    lst = []
    for i in range(10):
        lst.append(random.randrange(1, 1000))

    with pysnooper.snoop():
        lower = min(lst)
        upper = max(lst)
        mid = (lower + upper) / 2
        print(lower, mid, upper)

foo()

输入后果

New var:....... i = 9
New var:....... lst = [681, 267, 74, 832, 284, 678, ...]
09:37:35.881721 line        10         lower = min(lst)
New var:....... lower = 74
09:37:35.882137 line        11         upper = max(lst)
New var:....... upper = 832
09:37:35.882304 line        12         mid = (lower + upper) / 2
74 453.0 832
New var:....... mid = 453.0
09:37:35.882486 line        13         print(lower, mid, upper)
Elapsed time: 00:00:00.000344

如何装置?

最佳形式:pip

$ pip install pysnooper

其余形式:

  • Conda:$ conda install -c conda-forge pysnooper
  • Arch Linux:$ yay -S python-pysnooper
  • Fedora Linux:dnf install python3-pysnooper

开源前哨 日常分享热门、乏味和实用的开源我的项目。参加保护 10 万 + Star 的开源技术资源库,包含:Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET 等。

正文完
 0