GithubHelp home page GithubHelp logo

nlesc-nano / assertionlib Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 279 KB

A package for performing assertions and providing informative exception messages.

License: Apache License 2.0

Python 100.00%
python-3 python-3-6 python-3-7 assertion assertions assertion-library python-3-8 testing unit-testing python-3-9

assertionlib's Introduction

https://readthedocs.org/projects/assertionlib/badge/?version=latest

AssertionLib

A package for performing assertions and providing informative exception messages.

Installation

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

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

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

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 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's People

Contributors

bvb93 avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

bvb93

assertionlib's Issues

README.rst not included in PyPI

This README is loaded in the __init__.py. However, as this README is not included in the PyPI distribution, this causes errors when trying to import the package:

failing sanity check for 'AssertionLib' extension: command "python -c "import assertionlib"" failed; output:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/lib/python3.9/site-packages/assertionlib/__init__.py", line 14, in <module>
    __doc__ = load_readme(_README, encoding='utf-8')
  File "/lib/python3.9/site-packages/assertionlib/functions.py", line 467, in load_readme
    with open(readme, **kwargs) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/lib/python3.9/site-packages/assertionlib/README.rst', 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.