跳转到主要内容

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

项目描述

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

示例

以下是对内容项进行验证的示例。

from dataclasses import dataclass
from roughrider.predicate.errors import ConstraintError
from roughrider.predicate.validators import Validator, 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(Validator):

    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.3.1 (2021-10-21)

  • 更改扩展的包装。

0.3 (2021-10-21)

  • 代码重构和Cython优化。

0.2 (2021-10-20)

  • 修复了resolve_validator错误列表扩展。

0.1 (2021-10-09)

  • 首次发布。

项目详情


下载文件

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

源代码分发

roughrider.predicate-0.3.1.tar.gz (75.6 kB 查看哈希值)

上传时间 源代码

由以下机构支持

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