checkplz

A python package for checks and assertions of data and objects


License
MIT
Install
pip install checkplz==0.0.5

Documentation

CheckPlz

This package provides for inline checks (i.e. checkers) which assure that objects can meet a complicated set of assumptions. Special handling of raised exceptions allow the collection of multiple exceptions to these assertions before interrupting the process. You can think of this as a close relative to unit testing, where you get the benefits of a robust set of checks which can report errors independent of one another, with the convenience of single method type checking.

If exceptions are found, they are re-raised with a complete list of all the exceptions identified during the check. If no exceptions are found, the checker will return the original value that was passed to it.

Example

from checkplz import sequence
# example 1
x = sequence([1,2,3])
# example 2
y = sequence([1,2,5])

In the first example, the sequence matches the assumptions of the checker. Because no exceptions are identified, the object provider ([1,2,3]) will be assigned to the object x. However, in the second, the sequence has a gap which violates the assumptions of the checker. This will result in an exception being raised before the assignment to y can occur.

The utility of these simple assertions can be better shown when they are used in a more complex scenario. For our next example, let's say we're interested in taking a list of lists, and checking each of them individually for proper sequencing.

from checkplz import sequence
list_of_lists = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
]
# example 3
lol = [sequence(z) for z in list_of_lists]
# example 4
min_lol = [min(sequence(z)) for z in list_of_lists]

Here we use the checker as part of an inline list comprehension, and you can see the utility of such concise syntax. In example #3, we simply assign the object back to a list comprehension, in effect recreating an identical object, using the checker as an opportunity to raise exceptions for anything that violates our assumptions. In example #4, we wrap the checked result in an additional function, allowing us to first check the object before moving on with an additional step that prevents us from performing the check later.