Skip to main content

A package for performing assertions and providing informative exception messages.

Project description

https://travis-ci.org/nlesc-nano/AssertionLib.svg?branch=master 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://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

AssertionLib 2.2.3

A package for performing assertions and providing informative exception messages.

Installation

AssertionLib has no external dependencies and can be installed as following:

  • PyPi: pip install AssertionLib

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

Usage

A comprehensive overview of all available assertion methods is provided in the documentation. A few examples of some basic assertion:

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

# 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=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

A few examples of AssertionErrors raised due to incorrect method signatures:

>>> 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

A demonstration of the exception parameter. Providing an exception type will assert that the provided exception is raised during/before the assertion process:

>>> from assertionlib import assertion

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

>>> 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]

Lastly, the output of custom callables can be asserted in one of the following two ways, supplying the callable to AssertionManager.assert() or creating a custom assertion method and adding it to an instance with AssertionManager.add_to_instance():

>>> from typing import Any
>>> from assertionlib import assertion

>>> def my_fancy_func(a: Any) -> 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

# 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

AssertionLib-2.2.3.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

AssertionLib-2.2.3-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

Details for the file AssertionLib-2.2.3.tar.gz.

File metadata

  • Download URL: AssertionLib-2.2.3.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191201 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for AssertionLib-2.2.3.tar.gz
Algorithm Hash digest
SHA256 51ea5a4c89d59a342444a2f8c9d34a264bc71537fe41b92fe4f38d846b9b95c2
MD5 b17bea7347e32d2214024b219d9647f0
BLAKE2b-256 0ae77ecd6cb04f4254d67739032e8579832e931fa66ec9796b4840ba93878f84

See more details on using hashes here.

File details

Details for the file AssertionLib-2.2.3-py3-none-any.whl.

File metadata

  • Download URL: AssertionLib-2.2.3-py3-none-any.whl
  • Upload date:
  • Size: 33.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191201 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for AssertionLib-2.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 59b38ca66870b88054f0f4d3231f756d21bf56321f38d6c008bd8fc53543aeba
MD5 0ccc47281af492a94db69ab7daab3ee5
BLAKE2b-256 99d998127f2eab8fd2c5974a14b29011f43176eaf5b8fa3dfa1f0550c88497fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page