本文整理汇总了Python中parse_type.TypeBuilder.make_variant方法的典型用法代码示例。如果您正苦于以下问题:Python TypeBuilder.make_variant方法的具体用法?Python TypeBuilder.make_variant怎么用?Python TypeBuilder.make_variant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parse_type.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.make_variant方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_variant__with_color_or_person
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__with_color_or_person(self):
type_converters = [parse_color, parse_person_choice]
parse_variant2 = TypeBuilder.make_variant(type_converters)
schema = "Variant2: {variant:Color_or_Person}"
parser = parse.Parser(schema, dict(Color_or_Person=parse_variant2))
# -- TYPE 1: Color
self.assert_match(parser, "Variant2: red", "variant", Color.red)
self.assert_match(parser, "Variant2: blue", "variant", Color.blue)
# -- TYPE 2: Person
self.assert_match(parser, "Variant2: Alice", "variant", "Alice")
self.assert_match(parser, "Variant2: Bob", "variant", "Bob")
self.assert_match(parser, "Variant2: Charly", "variant", "Charly")
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "Variant2: __Alice__")
self.assert_mismatch(parser, "Variant2: Alice ")
self.assert_mismatch(parser, "Variant2: Alice2")
self.assert_mismatch(parser, "Variant2: red2")
# -- PERFORM TESTS:
self.ensure_can_parse_all_enum_values(parser,
parse_color, "Variant2: %s", "variant")
self.ensure_can_parse_all_choices(parser,
parse_person_choice, "Variant2: %s", "variant")
示例2: test_make_variant__with_strict_raises_error_on_case_mismatch
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__with_strict_raises_error_on_case_mismatch(self):
# -- NEEDS:
# * re_opts=0 (IGNORECASE disabled)
# * strict=True, allow that an error is raised
type_converters = [parse_number, parse_yesno]
parse_variant = TypeBuilder.make_variant(type_converters,
strict=True, re_opts=0)
schema = "Variant: {variant:YesNo_or_Number}"
parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
self.assertRaises(AssertionError, parser.parse, "Variant: YES")
示例3: test_make_variant__without_strict_may_return_none_on_case_mismatch
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__without_strict_may_return_none_on_case_mismatch(self):
# -- NEEDS:
# * re_opts=0 (IGNORECASE disabled)
# * strict=False, otherwise an error is raised
type_converters = [parse_number, parse_yesno]
parse_variant = TypeBuilder.make_variant(type_converters, re_opts=0,
strict=False)
schema = "Variant: {variant:YesNo_or_Number}"
parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
result = parser.parse("Variant: No")
self.assertNotEqual(result, None)
self.assertEqual(result["variant"], None)
示例4: test_make_variant__with_strict_and_compiled_raises_error_on_case_mismatch
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__with_strict_and_compiled_raises_error_on_case_mismatch(self):
# XXX re_opts=0 seems to work differently.
# -- NEEDS:
# * re_opts=0 (IGNORECASE disabled)
# * strict=True, allow that an error is raised
type_converters = [parse_number, parse_yesno]
# -- ENSURE: coverage for cornercase.
parse_number.matcher = re.compile(parse_number.pattern)
parse_variant = TypeBuilder.make_variant(type_converters,
compiled=True, re_opts=0, strict=True)
schema = "Variant: {variant:YesNo_or_Number}"
parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
# XXX self.assertRaises(AssertionError, parser.parse, "Variant: YES")
result = parser.parse("Variant: Yes")
self.assertNotEqual(result, None)
self.assertEqual(result["variant"], True)
示例5: test_make_variant__with_strict
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__with_strict(self):
# -- SKIP: IGNORECASE checks which would raise an error in strict mode.
type_converters = [parse_number, parse_yesno]
parse_variant = TypeBuilder.make_variant(type_converters, strict=True)
self.check_parse_variant_number_or_yesno(parse_variant,
with_ignorecase=False)
示例6: test_make_variant__with_re_opts_IGNORECASE
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__with_re_opts_IGNORECASE(self):
type_converters = [parse_number, parse_yesno]
parse_variant3 = TypeBuilder.make_variant(type_converters,
re_opts=re.IGNORECASE)
self.check_parse_variant_number_or_yesno(parse_variant3)
示例7: test_make_variant__compiled
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__compiled(self):
# -- REVERSED ORDER VARIANT:
type_converters = [parse_number, parse_yesno]
parse_variant2 = TypeBuilder.make_variant(type_converters,
compiled=True)
self.check_parse_variant_number_or_yesno(parse_variant2)
示例8: test_make_variant__uncompiled
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_variant [as 别名]
def test_make_variant__uncompiled(self):
type_converters = [parse_yesno, parse_number]
parse_variant1 = TypeBuilder.make_variant(type_converters)
self.check_parse_variant_number_or_yesno(parse_variant1)