跳转到主要内容

Python/Cython谓词/守卫/验证系统。

项目描述

此包有助于定义和创建小型且可重用的组件,这些组件可以用作守卫或验证方法。

示例

以下是一个内容项验证的示例。

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

变更

0.1.1 (2023-11-09)

  • Cython更新并包含py3.11

0.1 (2022-03-16)

  • 初始发布。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

prejudice-0.1.1.tar.gz (125.8 kB 查看哈希)

上传时间

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面