当前位置: 首页>>代码示例>>Python>>正文


Python CSVValidator.add_value_predicate方法代码示例

本文整理汇总了Python中csvvalidator.CSVValidator.add_value_predicate方法的典型用法代码示例。如果您正苦于以下问题:Python CSVValidator.add_value_predicate方法的具体用法?Python CSVValidator.add_value_predicate怎么用?Python CSVValidator.add_value_predicate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在csvvalidator.CSVValidator的用法示例。


在下文中一共展示了CSVValidator.add_value_predicate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_value_predicates

# 需要导入模块: from csvvalidator import CSVValidator [as 别名]
# 或者: from csvvalidator.CSVValidator import add_value_predicate [as 别名]
def test_value_predicates():
    """Test the use of value predicates."""

    field_names = ('foo', 'bar')
    validator = CSVValidator(field_names)
    foo_predicate = lambda v: math.pow(float(v), 2) < 64
    validator.add_value_predicate('foo', foo_predicate)
    bar_predicate = lambda v: math.sqrt(float(v)) > 8
    validator.add_value_predicate('bar', bar_predicate, 'X3', 'custom message')

    data = (
            ('foo', 'bar'),
            ('4', '81'), # valid
            ('9', '81'), # foo invalid
            ('4', '49') # bar invalid
            )

    problems = validator.validate(data)
    assert len(problems) == 2, len(problems)

    p0 = problems[0]
    assert p0['code'] == VALUE_PREDICATE_FALSE
    assert p0['message'] == MESSAGES[VALUE_PREDICATE_FALSE]
    assert p0['row'] == 3
    assert p0['column'] == 1
    assert p0['field'] == 'foo'
    assert p0['value'] == '9'
    assert p0['record'] == ('9', '81')

    p1 = problems[1]
    assert p1['code'] == 'X3'
    assert p1['message'] == 'custom message'
    assert p1['row'] == 4
    assert p1['column'] == 2
    assert p1['field'] == 'bar'
    assert p1['value'] == '49'
    assert p1['record'] == ('4', '49')
开发者ID:gamechanger,项目名称:csvvalidator,代码行数:39,代码来源:tests.py

示例2: test_exception_handling

# 需要导入模块: from csvvalidator import CSVValidator [as 别名]
# 或者: from csvvalidator.CSVValidator import add_value_predicate [as 别名]
def test_exception_handling():
    """Establish expectations for exception handling."""

    field_names = ('foo', 'bar')
    validator = CSVValidator(field_names)

    validator.add_value_check('foo', int)

    def buggy_value_check(v):
        """I am a buggy value check."""
        raise Exception('something went wrong')
    validator.add_value_check('bar', buggy_value_check)

    def buggy_value_predicate(v):
        """I am a buggy value predicate."""
        raise Exception('something went wrong')
    validator.add_value_predicate('bar', buggy_value_predicate)

    def buggy_record_check(r):
        """I am a buggy record check."""
        raise Exception('something went wrong')
    validator.add_record_check(buggy_record_check)

    def buggy_record_predicate(r):
        """I am a buggy record predicate."""
        raise Exception('something went wrong')
    validator.add_record_predicate(buggy_record_predicate)

    def buggy_assert(r):
        """I am a buggy assert."""
        raise Exception('something went wrong')
    validator.assert_something_buggy = buggy_assert

    def buggy_check(r):
        """I am a buggy check."""
        raise Exception('something went wrong')
    validator.check_something_buggy = buggy_check

    def buggy_each(r):
        """I am a buggy each."""
        raise Exception('something went wrong')
    validator.each_something_buggy = buggy_each

    def buggy_finally_assert():
        """I am a buggy finally assert."""
        raise Exception('something went wrong')
    validator.finally_assert_something_buggy = buggy_finally_assert

    def buggy_skip(record):
        """I am a buggy skip."""
        raise Exception('something went wrong')
    validator.add_skip(buggy_skip)

    data = (
            ('foo', 'bar'),
            ('ab', '56')
            )

    problems = validator.validate(data, report_unexpected_exceptions=False)
    n = len(problems)
    assert n == 1, n
    p = problems[0]
    assert p['row'] == 2

    problems = validator.validate(data) # by default, exceptions are reported as problems
    n = len(problems)
    assert n == 10, n

    unexpected_problems = [p for p in problems if p['code'] == UNEXPECTED_EXCEPTION]
    assert len(unexpected_problems) == 9
    for p in unexpected_problems:
        e = p['exception']
        assert e.args[0] == 'something went wrong', e.args
开发者ID:gamechanger,项目名称:csvvalidator,代码行数:75,代码来源:tests.py


注:本文中的csvvalidator.CSVValidator.add_value_predicate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。