Simple Validation Library for Python
Project description
| Python | |
| Package | |
| Meta |
pycsi
pycsi is a simple python library to validate data and/or build a validation schema.
Installation
pip install pycsi
Usage
We might begin by creating validation callables such:
def is_divisible_by_three(x: float):
return x % 3 == 0
def is_all_positive(numbers: list[float]):
return all(number > 0 for number in numbers)
def is_quotient_positive(x: float, y: float):
return x / y > 0
Then we can instantiate a Subvalidator instance and the validation callables to it:
from pycsi import Subvalidator
first_subvalidator = Subvalidator(
"First condition",
"Number is not divisible by 3",
is_divisible_by_three,
x=3
)
second_subvalidator = Subvalidator(
"Second condition",
"Not every number is positive",
is_all_positive,
numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, -10],
)
third_subvalidator = Subvalidator(
"Third condition",
"Quotient is not positive",
is_quotient_positive,
x=10,
y=2,
)
Finally we can create a validator object and add the subvalidators to it.
from pycsi import Validator
validator = Validator()
validator.add(first_subvalidator)
validator.add(second_subvalidator)
validator.add(third_subvalidator)
validator.run()
The validator will run all the subvalidators and the unvalidated_set property will return a set of the failed subvalidators.
print(validator.unvalidated_set)
{ValidatorError(condition='Second condition', error_message='Not every number is positive')}
Asynchronous Usage
This library also supports async validation. We can create asyncronous validation callables such:
import asyncio
async def async_is_divisible_by_three(x: float) -> bool:
await asyncio.sleep(0.1)
return is_divisible_by_three(x)
async def async_is_all_positive(numbers: list[float]) -> bool:
await asyncio.sleep(0.1)
return is_all_positive(numbers)
Instead of using the Subvalidator API, we are going to use the AsyncSubvalidator in the next step:
from pycsi import AsyncSubvalidator
first_subvalidator = AsyncSubvalidator(
"First condition",
"Number is not divisible by 3",
async_is_divisible_by_three,
x=10,
)
second_subvalidator = AsyncSubvalidator(
"Second condition",
"Not every number is positive",
async_is_all_positive,
numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
)
Same for the Validator API, we will switch the the AsyncValidator.
from pycsi import AsyncValidator
validator = AsyncValidator()
validator.add(first_subvalidator)
validator.add(second_subvalidator)
validator.run()
print(validator.unvalidated_set)
{ValidatorError(condition='First condition', error_message='Number is not divisible by 3')}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycsi-0.1.0.tar.gz.
File metadata
- Download URL: pycsi-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b7e2d7e0bc2c2f12240a28a5d72fc41c2f26ccedadf321a4b0f71f820808857
|
|
| MD5 |
9a268143b264e7f22064058bc7edde33
|
|
| BLAKE2b-256 |
593da142c8dd3e95af07e4675b47ed75e3900a6d4d2229a5bebf1035d656d3b0
|
File details
Details for the file pycsi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pycsi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77538b9ec88d2d12f0373695d9ad588d621e46e30662d14d77ecb7390cc023d2
|
|
| MD5 |
8a7d4e2b5f780385057fbebbc05daac3
|
|
| BLAKE2b-256 |
8fa21e18d3ff770f3aa8f39a4da608fe6aa1feb4d2333ba264f96ed1b3f398d1
|