跳转到主要内容

一个执行断言并提供信息性异常信息的软件包。

项目描述

https://readthedocs.org/projects/assertionlib/badge/?version=latest https://badge.fury.io/py/AssertionLib.svg https://github.com/nlesc-nano/AssertionLib/workflows/Python%20package/badge.svg https://codecov.io/gh/nlesc-nano/AssertionLib/branch/master/graph/badge.svg https://zenodo.org/badge/214183943.svg

https://img.shields.io/badge/python-3.6-blue.svg https://img.shields.io/badge/python-3.7-blue.svg https://img.shields.io/badge/python-3.8-blue.svg https://img.shields.io/badge/python-3.9-blue.svg https://img.shields.io/badge/python-3.10-blue.svg https://img.shields.io/badge/python-3.11-blue.svg

AssertionLib

一个执行断言并提供信息性异常信息的软件包。

安装

  • PyPi: pip install AssertionLib

  • GitHub: pip install git+https://github.com/nlesc-nano/AssertionLib

使用方法

文档中提供了所有可用断言方法的全面概述。以下是一些基本断言的示例

>>> import numpy as np
>>> from assertionlib import assertion

# Assert the output of specific callables
>>> assertion.eq(5, 5)  # 5 == 5
>>> assertion.lt(5, 6)  # 5 < 6
>>> assertion.gt(6, 5)  # 5 > 6
>>> assertion.isinstance(5, int)
>>> assertion.hasattr(5, '__init__')
>>> assertion.any([False, False, True])
>>> assertion.isfinite(1.0)

# Simply assert a value
>>> assertion(5 == 5)
>>> assertion(isinstance(5, int))

# Apply post-processing before conducting the assertion
>>> ar_large = np.ones(10)
>>> ar_small = np.zeros(10)
>>> assertion.gt(ar_large, ar_small, post_process=np.all)  # all(ar_large > ar_small)

# Perform an assertion which will raise an AssertionError
>>> assertion.eq(5, 6, message='Fancy custom error message')  # 5 == 6
Traceback (most recent call last):
  ...
AssertionError: output = eq(a, b); assert output

exception: AssertionError = AssertionError('Fancy custom error message')

output: bool = False
a: int = 5
b: int = 6

以下是一些由于方法签名错误而引发的AssertionError的示例

>>> from assertionlib import assertion

>>> assertion.len(5)
Traceback (most recent call last):
  ...
AssertionError: output = len(obj); assert output

exception: TypeError = TypeError("object of type 'int' has no len()")

output: NoneType = None
obj: int = 5
>>> from assertionlib import assertion

>>> assertion.eq(5, 5, 5, 5)
Traceback (most recent call last):
  ...
AssertionError: output = eq(a, b, _a, _b); assert output

exception: TypeError = TypeError('eq expected 2 arguments, got 4')

output: NoneType = None
a: int = 5
b: int = 5
_a: int = 5
_b: int = 5

异常参数的演示。提供异常类型将断言在断言过程中/之前会抛出所提供的异常

>>> from assertionlib import assertion

>>> len(5)
Traceback (most recent call last):
  ...
TypeError: object of type 'int' has no len()
>>> from assertionlib import assertion

>>> assertion.len(5, exception=TypeError)  # i.e. len(5) should raise a TypeError
>>> assertion.len([5], exception=TypeError)
Traceback (most recent call last):
  ...
AssertionError: output = len(obj); assert output

exception: AssertionError = AssertionError("Failed to raise 'TypeError'")

output: int = 1
obj: list = [5]

最后,可以通过以下两种方式之一断言自定义可调用对象的结果:将可调用对象传递给 AssertionManager.assert() 或创建一个自定义断言方法并将其添加到实例中,使用 AssertionManager.add_to_instance()

>>> from assertionlib import assertion

>>> def my_fancy_func(a: object) -> bool:
...     return False

# Approach #1, supply to-be asserted callable to assertion.assert_()
>>> assertion.assert_(my_fancy_func, 5)
Traceback (most recent call last):
  ...
AssertionError: output = my_fancy_func(a); assert output

exception: AssertionError = AssertionError(None)

output: bool = False
a: int = 5
>>> from assertionlib import assertion

# Approach #2, permanantly add a new bound method using assertion.add_to_instance()
>>> assertion.add_to_instance(my_fancy_func)
>>> assertion.my_fancy_func(5)
Traceback (most recent call last):
  ...
AssertionError: output = my_fancy_func(a); assert output

exception: AssertionError = AssertionError(None)

output: bool = False
a: int = 5

项目详情


下载文件

下载您平台对应的文件。如果您不确定该选择哪个,请了解更多关于 安装包 的信息。

源代码分发

AssertionLib-3.2.2.tar.gz (38.6 kB 查看哈希值)

上传时间 源代码

构建分发

AssertionLib-3.2.2-py3-none-any.whl (33.1 kB 查看哈希值)

上传时间 Python 3

由以下组织支持