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


Python trafaret.extract_error函数代码示例

本文整理汇总了Python中trafaret.extract_error函数的典型用法代码示例。如果您正苦于以下问题:Python extract_error函数的具体用法?Python extract_error怎么用?Python extract_error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: test_mapping_interface

    def test_mapping_interface(self):
        trafaret = t.Dict({t.Key("foo"): t.String, t.Key("bar"): t.Float})

        # class with mapping interface but not subclass of dict
        class Map(AbcMapping):
            def __init__(self, data, *a, **kw):
                super(Map, self).__init__(*a, **kw)
                self._data = data

            def __getitem__(self, key):
                return self._data[key]

            def __iter__(self):
                for x in self._data:
                    yield x

            def __len__(self):
                return len(self._data)

        trafaret.check(Map({"foo": "xxx", "bar": 0.1}))

        res = extract_error(trafaret, object())
        self.assertEqual(res, "value does not provide Mapping interface")

        res = extract_error(trafaret, Map({"foo": "xxx"}))
        self.assertEqual(res, {"bar": "is required"})

        res = extract_error(trafaret, Map({"foo": "xxx", "bar": "str"}))
        self.assertEqual(res, {"bar": "value can't be converted to float"})
开发者ID:jettify,项目名称:trafaret,代码行数:29,代码来源:utests.py

示例2: test_dict_keys

 def test_dict_keys(self):
     res = t.DictKeys(['a', 'b']).check({'a': 1, 'b': 2})
     self.assertEqual(res, {'a': 1, 'b': 2})
     res = extract_error(t.DictKeys(['a', 'b']), {'a': 1, 'b': 2, 'c': 3})
     self.assertEqual(res, {'c': 'c is not allowed key'})
     res = extract_error(t.DictKeys(['key', 'key2']), {'key': 'val'})
     self.assertEqual(res, {'key2': 'is required'})
开发者ID:rrader,项目名称:trafaret,代码行数:7,代码来源:utests.py

示例3: test_string

 def test_string(self):
     res = t.String()
     self.assertEqual(repr(res), '<String>')
     res = t.String(allow_blank=True)
     self.assertEqual(repr(res), '<String(blank)>')
     res = t.String().check("foo")
     self.assertEqual(res, 'foo')
     res = extract_error(t.String(), "")
     self.assertEqual(res, 'blank value is not allowed')
     res = t.String(allow_blank=True).check("")
     self.assertEqual(res, '')
     res = extract_error(t.String(), 1)
     self.assertEqual(res, 'value is not a string')
     res = t.String(regex='\w+').check('wqerwqer')
     self.assertEqual(res, 'wqerwqer')
     res = extract_error(t.String(regex='^\w+$'), 'wqe rwqer')
     self.assertEqual(res, "value does not match pattern: '^\\\\w+$'")
     res = t.String(min_length=2, max_length=3).check('123')
     self.assertEqual(res, '123')
     res = extract_error(t.String(min_length=2, max_length=6), '1')
     self.assertEqual(res, 'String is shorter than 2 characters')
     res = extract_error(t.String(min_length=2, max_length=6), '1234567')
     self.assertEqual(res, 'String is longer than 6 characters')
     # TODO
     # res = String(min_length=2, max_length=6, allow_blank=True)
     # self.assertEqual(res, Traceback (most recent call last):
     #     ...
     #     AssertionError: Either allow_blank or min_length should be specified, not both
     res = t.String(min_length=0, max_length=6, allow_blank=True).check('123')
     self.assertEqual(res, '123')
开发者ID:rrader,项目名称:trafaret,代码行数:30,代码来源:utests.py

示例4: test_string

 def test_string(self):
     res = t.String()
     self.assertEqual(repr(res), "<String>")
     res = t.String(allow_blank=True)
     self.assertEqual(repr(res), "<String(blank)>")
     res = t.String().check("foo")
     self.assertEqual(res, "foo")
     res = extract_error(t.String(), "")
     self.assertEqual(res, "blank value is not allowed")
     res = t.String(allow_blank=True).check("")
     self.assertEqual(res, "")
     res = extract_error(t.String(), 1)
     self.assertEqual(res, "value is not a string")
     res = t.String(regex="\w+").check("wqerwqer")
     self.assertEqual(res, "wqerwqer")
     res = extract_error(t.String(regex="^\w+$"), "wqe rwqer")
     self.assertEqual(res, "value does not match pattern: '^\\\\w+$'")
     res = t.String(min_length=2, max_length=3).check("123")
     self.assertEqual(res, "123")
     res = extract_error(t.String(min_length=2, max_length=6), "1")
     self.assertEqual(res, "String is shorter than 2 characters")
     res = extract_error(t.String(min_length=2, max_length=6), "1234567")
     self.assertEqual(res, "String is longer than 6 characters")
     # TODO
     # res = String(min_length=2, max_length=6, allow_blank=True)
     # self.assertEqual(res, Traceback (most recent call last):
     #     ...
     #     AssertionError: Either allow_blank or min_length should be specified, not both
     res = t.String(min_length=0, max_length=6, allow_blank=True).check("123")
     self.assertEqual(res, "123")
开发者ID:jettify,项目名称:trafaret,代码行数:30,代码来源:utests.py

示例5: test_mutually_exclusive

    def test_mutually_exclusive(self):
        trafaret = t.Dict({
            t.Key('ami', optional=True): t.String,
            t.Key('ami_config_key', optional=True): t.String,
            t.Key('label'): t.String
        }).mutually_exclusive(* [('ami', 'ami_config_key')])
        self.assertEqual(
            trafaret.check({
                'label': 'nya',
                'ami_config_key': 'the_key'
            }), {'label': 'nya',
                 'ami_config_key': 'the_key'})

        self.assertEqual(
            trafaret.check({
                'label': 'nya',
                'ami': 'ami-d2384821'
            }), {'label': 'nya',
                 'ami': 'ami-d2384821'})

        res = t.extract_error(trafaret, {
            'label': 'nya',
            'ami_config_key': 'the_key',
            'ami': 'ami-d2384821'
        })
        self.assertEqual(res, {'ami': 'ami mutually exclusive with ami_config_key'})

        res = t.extract_error(trafaret, {
            'label': 'nya'
            })
        self.assertEqual(res, {'ami': 'at least one key required from set: ami, ami_config_key'})
开发者ID:Axik,项目名称:trafaret,代码行数:31,代码来源:test_base.py

示例6: test_mapping

 def test_mapping(self):
     trafaret = t.Mapping(t.String, t.Int)
     self.assertEqual(repr(trafaret), "<Mapping(<String> => <Int>)>")
     res = trafaret.check({"foo": 1, "bar": 2})
     self.assertEqual(res, {"bar": 2, "foo": 1})
     res = extract_error(trafaret, {"foo": 1, "bar": None})
     self.assertEqual(res, {"bar": {"value": "value is not int"}})
     res = extract_error(trafaret, {"foo": 1, 2: "bar"})
     self.assertEqual(res, {2: {"key": "value is not a string", "value": "value can't be converted to int"}})
开发者ID:jettify,项目名称:trafaret,代码行数:9,代码来源:utests.py

示例7: test_base2

 def test_base2(self):
     trafaret = t.Dict({t.Key('bar', optional=True): t.String}, foo=t.Int)
     trafaret.allow_extra('*')
     res = trafaret.check({"foo": 1, "ham": 100, "baz": None})
     self.assertEqual(res, {'baz': None, 'foo': 1, 'ham': 100})
     res = extract_error(trafaret, {"bar": 1, "ham": 100, "baz": None})
     self.assertEqual(res, {'bar': 'value is not a string', 'foo': 'is required'})
     res = extract_error(trafaret, {"foo": 1, "bar": 1, "ham": 100, "baz": None})
     self.assertEqual(res, {'bar': 'value is not a string'})
开发者ID:rrader,项目名称:trafaret,代码行数:9,代码来源:utests.py

示例8: test_meta_res

 def test_meta_res(self):
     res = (t.Int > 5).check(10)
     self.assertEqual(res, 10)
     res = extract_error(t.Int > 5, 1)
     self.assertEqual(res, 'value should be greater than 5')
     res = (t.Int < 3).check(1)
     self.assertEqual(res, 1)
     res = extract_error(t.Int < 3, 3)
     self.assertEqual(res, 'value should be less than 3')
开发者ID:rrader,项目名称:trafaret,代码行数:9,代码来源:utests.py

示例9: test_int

 def test_int(self):
     res = repr(t.Int())
     self.assertEqual(res, '<Int>')
     res = t.Int().check(5)
     self.assertEqual(res, 5)
     res = extract_error(t.Int(), 1.1)
     self.assertEqual(res, 'value is not int')
     res = extract_error(t.Int(), 1 + 1j)
     self.assertEqual(res, 'value is not int')
开发者ID:rrader,项目名称:trafaret,代码行数:9,代码来源:utests.py

示例10: test_mongo_id

    def test_mongo_id(self):
        c = MongoId()
        self.assertIsInstance(repr(c), str)
        self.assertEqual(c.check("5583f69d690b2d70a4afdfae"),
                         ObjectId('5583f69d690b2d70a4afdfae'))
        res = extract_error(c, 'just_id')
        self.assertEqual(res, "'just_id' is not a valid ObjectId, it must be"
                              " a 12-byte input or a 24-character hex string")

        res = extract_error(c, None)
        self.assertEqual(res, "blank value is not allowed")
开发者ID:Deepwalker,项目名称:trafaret,代码行数:11,代码来源:test_mongo.py

示例11: test_mongo_id

    def test_mongo_id(self):
        from trafaret.contrib.object_id import ObjectId

        c = t.MongoId()
        self.assertIsInstance(repr(c), str)
        self.assertEqual(c.check("5583f69d690b2d70a4afdfae"), ObjectId("5583f69d690b2d70a4afdfae"))
        res = extract_error(c, "just_id")
        self.assertEqual(
            res, "'just_id' is not a valid ObjectId, it must be" " a 12-byte input or a 24-character hex string"
        )

        res = extract_error(c, None)
        self.assertEqual(res, "blank value is not allowed")
开发者ID:jettify,项目名称:trafaret,代码行数:13,代码来源:utests.py

示例12: test_forward

 def test_forward(self):
     node = t.Forward()
     node << t.Dict(name=t.String, children=t.List[node])
     self.assertEqual(repr(node), "<Forward(<Dict(children=<List(<recur>)>, name=<String>)>)>")
     res = node.check({"name": "foo", "children": []}) == {"children": [], "name": "foo"}
     self.assertEqual(res, True)
     res = extract_error(node, {"name": "foo", "children": [1]})
     self.assertEqual(res, {"children": {0: "value does not provide Mapping interface"}})
     res = node.check({"name": "foo", "children": [{"name": "bar", "children": []}]})
     self.assertEqual(res, {"children": [{"children": [], "name": "bar"}], "name": "foo"})
     empty_node = t.Forward()
     self.assertEqual(repr(empty_node), "<Forward(None)>")
     res = extract_error(empty_node, "something")
     self.assertEqual(res, "trafaret not set yet")
开发者ID:jettify,项目名称:trafaret,代码行数:14,代码来源:utests.py

示例13: test_email

 def test_email(self):
     res = t.Email().check("[email protected]")
     self.assertEqual(res, "[email protected]")
     res = extract_error(t.Email(), "[email protected]")  # try without domain-part
     self.assertEqual(res, "value is not a valid email address")
     res = str(t.Email().check("[email protected]пример.рф"))  # try with `idna` encoding
     self.assertEqual(res, "[email protected]")
     res = (t.Email() >> (lambda m: m.groupdict()["domain"])).check("[email protected]")
     self.assertEqual(res, "example.net")
     res = extract_error(t.Email(), "foo")
     self.assertEqual(res, "value is not a valid email address")
     res = extract_error(t.Email(), "f" * 10000 + "@correct.domain.edu")
     self.assertEqual(res, "value is not a valid email address")
     res = extract_error(t.Email(), "f" * 248 + "@x.edu") == "f" * 248 + "@x.edu"
     self.assertEqual(res, True)
开发者ID:jettify,项目名称:trafaret,代码行数:15,代码来源:utests.py

示例14: test_email

 def test_email(self):
     res = t.Email().check('[email protected]')
     self.assertEqual(res, '[email protected]')
     res = extract_error(t.Email(),'[email protected]') # try without domain-part
     self.assertEqual(res, 'value is not a valid email address')
     res = str(t.Email().check('[email protected]пример.рф')) # try with `idna` encoding
     self.assertEqual(res, '[email protected]')
     res = (t.Email() >> (lambda m: m.groupdict()['domain'])).check('[email protected]')
     self.assertEqual(res, 'example.net')
     res = extract_error(t.Email(), 'foo')
     self.assertEqual(res, 'value is not a valid email address')
     res = extract_error(t.Email(), 'f' * 10000 + '@correct.domain.edu')
     self.assertEqual(res, 'value is not a valid email address')
     res = extract_error(t.Email(), 'f' * 248 + '@x.edu') == 'f' * 248 + '@x.edu'
     self.assertEqual(res, True)
开发者ID:rrader,项目名称:trafaret,代码行数:15,代码来源:utests.py

示例15: test_str_bool

 def test_str_bool(self):
     res = extract_error(t.StrBool(), "aloha")
     self.assertEqual(res, "value can't be converted to Bool")
     res = t.StrBool().check(1)
     self.assertEqual(res, True)
     res = t.StrBool().check(0)
     self.assertEqual(res, False)
     res = t.StrBool().check("y")
     self.assertEqual(res, True)
     res = t.StrBool().check("n")
     self.assertEqual(res, False)
     res = t.StrBool().check(None)
     self.assertEqual(res, False)
     res = t.StrBool().check("1")
     self.assertEqual(res, True)
     res = t.StrBool().check("0")
     self.assertEqual(res, False)
     res = t.StrBool().check("YeS")
     self.assertEqual(res, True)
     res = t.StrBool().check("No")
     self.assertEqual(res, False)
     res = t.StrBool().check(True)
     self.assertEqual(res, True)
     res = t.StrBool().check(False)
     self.assertEqual(res, False)
开发者ID:jettify,项目名称:trafaret,代码行数:25,代码来源:utests.py


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