本文主要是介绍Python使用devtools提高调试效率,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安利一个开源库:GitHub - samuelcolvin/python-devtools: Dev tools for python
## 1. 打印调试信息
示例代码:
# pip install asynctor devtools httpx
import asynctor
from devtools import debug
from httpx import AsyncClientasync def main():async with AsyncClient(follow_redirects=True) as client:r = await client.get("https://qq.com")print(r)debug(r)if __name__ == "__main__":asynctor.run(main)
结果如下:
<Response [200 OK]>
demo.py:11 mainr: <Response [200 OK]> (Response)
其中第一行是print(r)的输出,另外两行是debug(r)的输出,可以看到debug会打印更多的信息。
## 2. 打印程序段耗时
# pip install asynctor devtools httpx
import asynctor
from devtools import debug
from httpx import AsyncClientasync def main():with debug.timer("Let's fetch qq.com"):async with AsyncClient(follow_redirects=True) as client:r = await client.get("https://qq.com")print(r)debug(r)if __name__ == "__main__":asynctor.run(main)
输出如下:
Let's fetch qq.com: 0.375s elapsed
<Response [200 OK]>
demo.py:12 mainr: <Response [200 OK]> (Response)
可以看到输出的信息里,自动加了冒号,并打印出了以秒为单位的程序段耗时
这篇关于Python使用devtools提高调试效率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!