本文整理汇总了Python中parse_type.TypeBuilder.make_choice2方法的典型用法代码示例。如果您正苦于以下问题:Python TypeBuilder.make_choice2方法的具体用法?Python TypeBuilder.make_choice2怎么用?Python TypeBuilder.make_choice2使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parse_type.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.make_choice2方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_choice2__with_transform
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice2 [as 别名]
def test_make_choice2__with_transform(self):
transform = lambda x: x.lower()
parse_choice2 = TypeBuilder.make_choice2(["ZERO", "one", "Two"],
transform=transform)
self.assertSequenceEqual(parse_choice2.choices, ["zero", "one", "two"])
schema = "Answer: {answer:NumberWordChoice}"
parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice2))
# -- PERFORM TESTS:
# NOTE: Parser uses re.IGNORECASE => Any case is accepted.
self.assert_match(parser, "Answer: zERO", "answer", (0, "zero"))
self.assert_match(parser, "Answer: ONE", "answer", (1, "one"))
self.assert_match(parser, "Answer: Two", "answer", (2, "two"))
示例2: test_make_choice2__samecase_match_or_error
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice2 [as 别名]
def test_make_choice2__samecase_match_or_error(self):
# -- NOTE: strict=True => Enable errors due to case-mismatch.
parse_choice2 = TypeBuilder.make_choice2(["Zero", "one", "TWO"],
strict=True)
schema = "Answer: {answer:NumberWordChoice}"
parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice2))
# -- PERFORM TESTS: Case matches.
# NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
self.assert_match(parser, "Answer: Zero", "answer", (0, "Zero"))
self.assert_match(parser, "Answer: one", "answer", (1, "one"))
self.assert_match(parser, "Answer: TWO", "answer", (2, "TWO"))
# -- PERFORM TESTS: EXACT-CASE MISMATCH
case_mismatch_input_data = ["zero", "ZERO", "One", "ONE", "two" ]
for input_value in case_mismatch_input_data:
input_text = "Answer: %s" % input_value
with self.assertRaises(ValueError):
parser.parse(input_text)