Python/Cython predicate/guard/validation system.
Project description
This package helps defining and creating small and reusable components that can serve as guard or validation methods.
Example
Below is an example of a validation on a content item.
from dataclasses import dataclass
from prejudice.errors import ConstraintError
from prejudice.validators import Or
@dataclass
class Document:
id: str
body: str = ''
content_type: str = 'text/plain'
def non_empty_document(item):
"""Implementation of a validator/predicate
"""
if not item.body:
raise ConstraintError('Body is empty.')
class ContentType:
def __init__(self, content_type):
self.ct = content_type
def __call__(self, item):
if item.content_type != self.ct:
raise ConstraintError(
f'Expected {self.ct}, got {item.content_type}.')
validator = Or((
ContentType('text/plain'),
Or((ContentType('text/html'), non_empty_document))
))
document = Document(id='test', content_type='application/json')
validator(document) # raises ConstraintsErrors
CHANGES
0.1.1 (2023-11-09)
Cython update and included py3.11
0.1 (2022-03-16)
Initial release.
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
prejudice-0.1.1.tar.gz
(125.8 kB
view details)
File details
Details for the file prejudice-0.1.1.tar.gz
.
File metadata
- Download URL: prejudice-0.1.1.tar.gz
- Upload date:
- Size: 125.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a2538f80c6d9d2820123abcf5782a26987afb46db69f1f76fa56c190b5555b2 |
|
MD5 | a8210e9aee102e592dceebbdd653fd45 |
|
BLAKE2b-256 | 428df7c5dc50e0d097674e1c9b4c9192bb7e6702808407dad9ad0cdb00aab783 |