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


Python CSVValidator.add_record_predicate方法代码示例

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


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

示例1: test_record_predicates

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

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

    def foo_gt_bar(r):
        return int(r['foo']) > int(r['bar']) # expect record will be a dictionary
    validator.add_record_predicate(foo_gt_bar) # use default code and message

    def foo_gt_2bar(r):
        return int(r['foo']) > 2 * int(r['bar'])
    validator.add_record_predicate(foo_gt_2bar, 'X4', 'custom message')

    data = (
            ('foo', 'bar'),
            ('7', '3'), # valid
            ('5', '3'), # invalid - not foo_gt_2bar
            ('1', '3') # invalid - both predicates false
            )

    problems = validator.validate(data)
    n = len(problems)
    assert n == 3, n

    row3_problems = [p for p in problems if p['row'] == 3]
    assert len(row3_problems) == 1
    p = row3_problems[0]
    assert p['code'] == 'X4'
    assert p['message'] == 'custom message'
    assert p['record'] == ('5', '3')

    row4_problems = [p for p in problems if p['row'] == 4]
    assert len(row4_problems) == 2

    row4_problems_default = [p for p in row4_problems if p['code'] == RECORD_PREDICATE_FALSE]
    assert len(row4_problems_default) == 1
    p = row4_problems_default[0]
    assert p['message'] == MESSAGES[RECORD_PREDICATE_FALSE]
    assert p['record'] == ('1', '3')

    row4_problems_custom = [p for p in row4_problems if p['code'] == 'X4']
    assert len(row4_problems_custom) == 1
    p = row4_problems_custom[0]
    assert p['message'] == 'custom message'
    assert p['record'] == ('1', '3')
开发者ID:gamechanger,项目名称:csvvalidator,代码行数:48,代码来源:tests.py

示例2: test_context

# 需要导入模块: from csvvalidator import CSVValidator [as 别名]
# 或者: from csvvalidator.CSVValidator import add_record_predicate [as 别名]
def test_context():
    """Test passing in of context information."""

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

    def foo_gt_bar(r):
        return int(r['foo']) > int(r['bar'])
    validator.add_record_predicate(foo_gt_bar)

    data = (
            ('foo', 'bar'),
            ('7', '3'), # valid
            ('1', '3') # invalid
            )

    context = {'info': 'file X'}
    problems = validator.validate(data, context=context)
    n = len(problems)
    assert n == 1, n

    p = problems[0]
    assert p['context'] == context
开发者ID:gamechanger,项目名称:csvvalidator,代码行数:25,代码来源:tests.py

示例3: test_limit

# 需要导入模块: from csvvalidator import CSVValidator [as 别名]
# 或者: from csvvalidator.CSVValidator import add_record_predicate [as 别名]
def test_limit():
    """Test the use of the limit option."""

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

    def foo_gt_bar(r):
        return int(r['foo']) > int(r['bar'])
    validator.add_record_predicate(foo_gt_bar)

    data = (
            ('foo', 'bar'),
            ('7', '3'), # valid
            ('1', '3'), # invalid
            ('2', '3') # invalid
            )

    problems = validator.validate(data, limit=1)
    n = len(problems)
    assert n == 1, n

    problems = validator.validate(data)
    n = len(problems)
    assert n == 2, n
开发者ID:gamechanger,项目名称:csvvalidator,代码行数:26,代码来源:tests.py

示例4: test_summarize

# 需要导入模块: from csvvalidator import CSVValidator [as 别名]
# 或者: from csvvalidator.CSVValidator import add_record_predicate [as 别名]
def test_summarize():
    """Test use of summarize option."""

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

    def foo_gt_bar(r):
        return int(r['foo']) > int(r['bar'])
    validator.add_record_predicate(foo_gt_bar)

    data = (
            ('foo', 'bar'),
            ('7', '3'), # valid
            ('1', '3') # invalid
            )

    problems = validator.validate(data, summarize=True)
    n = len(problems)
    assert n == 1, n

    p = problems[0]
    assert p['code'] == RECORD_PREDICATE_FALSE
    for k in ('message', 'row', 'record'):
        assert k not in p
开发者ID:gamechanger,项目名称:csvvalidator,代码行数:26,代码来源:tests.py

示例5: test_exception_handling

# 需要导入模块: from csvvalidator import CSVValidator [as 别名]
# 或者: from csvvalidator.CSVValidator import add_record_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_record_predicate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。