几款好用的python工具

1、PySnooper–调试工具包

安装:

pip install pysnooper

在需要调试的函数上使用PySnooper添加一个装饰器,就能获得运行函数的log,包括执行的代码行、执行时间及局部变量发生的变化的时间
示例

import pysnooper

@pysnooper.snoop()
def  bug_test(test_num):
    test_num +=1
    print(test_num)

2、loguru–简单优雅的的日志工具

安装:

pip install loguru

使用方法
···
from loguru import logger
logger.debug(‘this is a debug message’)
···
日志直接输出到控制台,如果想输入到指定文件:

logger.add('runtime.log')
logger.debug('this is a debug')

其他的参数

def add(
    self,
    sink,
    *,
    level=_defaults.LOGURU_LEVEL,
    format=_defaults.LOGURU_FORMAT,
    filter=_defaults.LOGURU_FILTER,
    colorize=_defaults.LOGURU_COLORIZE,
    serialize=_defaults.LOGURU_SERIALIZE,
    backtrace=_defaults.LOGURU_BACKTRACE,
    diagnose=_defaults.LOGURU_DIAGNOSE,
    enqueue=_defaults.LOGURU_ENQUEUE,
    catch=_defaults.LOGURU_CATCH,
    **kwargs
):

format、filter、level的概念和格式和 logging 模块都是基本一样的了,例如:

logger.add('runtime.log', format="{time} {level} {message}", filter="my_module", level="INFO")

删除重写日志文件的内容

from loguru import logger

trace = logger.add('runtime.log')
logger.debug('this is a debug message')
logger.remove(trace)
logger.debug('this is another debug message')

rotation参数
表示文件的更新 ,比如我们想一天输出一个日志文件,或者文件太大了自动分隔日志文件,我们可以直接使用 add 方法的 rotation 参数进行配置。

logger.add('runtime_{time}.log', rotation="500 MB")

可以实现每 500MB 存储一个文件,每个 log 文件过大就会新创建一个 log 文件。我们在配置 log 名字时加上了一个 time 占位符,这样在生成时可以自动将时间替换进去,生成一个文件名包含时间的 log 文件。
另外我们也可以使用 rotation 参数实现定时创建 log 文件,例如:

logger.add('runtime_{time}.log', rotation='00:00')

具体的说明

- an |int| which corresponds to the maximum file size in bytes before that the current
  logged file is closed and a new one started over.
- a |timedelta| which indicates the frequency of each new rotation.
- a |time| which specifies the hour when the daily rotation should occur.
- a |str| for human-friendly parametrization of one of the previously enumerated types.
  Examples: ``"100 MB"``, ``"0.5 GB"``, ``"1 month 2 weeks"``, ``"4 days"``, ``"10h"``,
  ``"monthly"``, ``"18:00"``, ``"sunday"``, ``"w0"``, ``"monday at 12:00"``, ...
- a |function|_ which will be called before logging. It should accept two
  arguments: the logged message and the file object, and it should return ``True`` if
  the rotation should happen now, ``False`` otherwise.

retention参数
配置日志最长保留时间

logger.add('runtime.log', retention='10 days')

只保留最近10天的log
compression参数
loguru 还可以配置文件的压缩格式,比如使用 zip 文件格式保存,示例如下:

logger.add('runtime.log', compression='zip')

Traceback记录
在很多情况下,如果遇到运行错误,而我们在打印输出 log 的时候万一不小心没有配置好 Traceback 的输出,很有可能我们就没法追踪错误所在了。

但用了 loguru 之后,我们用它提供的装饰器就可以直接进行 Traceback 的记录,类似这样的配置即可

@logger.catch
def my_function(x, y, z):
    # An error? It's caught anyway!
    return 1 / (x + y + z)

运行完毕之后,可以发现 log 里面就出现了 Traceback 信息,而且给我们输出了当时的变量值,真的是不能再赞了!结果如下:

> File "run.py", line 15, in <module>
   my_function(0, 0, 0)
   └ <function my_function at 0x1171dd510>

 File "/private/var/py/logurutest/demo5.py", line 13, in my_function
   return 1 / (x + y + z)
               │   │   └ 0
               │   └ 0
               └ 0

ZeroDivisionError: division by zero

3、pytest工具

安装:

pip install pytest

pytest测试样例的编写规则:

  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可

生成测试报告

# 生成Html格式的报告
py.test --resultlog=path

# 生成XML格式的报告
py.test --junitxml=path
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值