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 AsyncClient


async 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 main
    r: <Response [200 OK]> (Response)

其中第一行是print(r)的输出,另外两行是debug(r)的输出,可以看到debug会打印更多的信息。

## 2. 打印程序段耗时

# pip install asynctor devtools httpx
import asynctor
from devtools import debug
from httpx import AsyncClient


async 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 main
    r: <Response [200 OK]> (Response)

可以看到输出的信息里,自动加了冒号,并打印出了以秒为单位的程序段耗时 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值