本文整理汇总了Python中parse_type.TypeBuilder.with_optional方法的典型用法代码示例。如果您正苦于以下问题:Python TypeBuilder.with_optional方法的具体用法?Python TypeBuilder.with_optional怎么用?Python TypeBuilder.with_optional使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parse_type.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.with_optional方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_with_optional_and_named_fields
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import with_optional [as 别名]
def test_parse_with_optional_and_named_fields(self):
parse_opt_number = TypeBuilder.with_optional(parse_number)
parse_opt_number.name = "Number?"
type_dict = build_type_dict([parse_opt_number, parse_number])
schema = "Numbers: {number1:Number?} {number2:Number}"
parser = parse.Parser(schema, type_dict)
# -- CASE: Optional number is present
result = parser.parse("Numbers: 34 12")
expected = dict(number1=34, number2=12)
self.assertIsNotNone(result)
self.assertEqual(result.named, expected)
# -- CASE: Optional number is missing
result = parser.parse("Numbers: 12")
expected = dict(number1=None, number2=12)
self.assertIsNotNone(result)
self.assertEqual(result.named, expected)
示例2: test_parse_with_optional_and_unnamed_fields
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import with_optional [as 别名]
def test_parse_with_optional_and_unnamed_fields(self):
# -- ENSURE: Cardinality.optional.group_count is correct
# REQUIRES: ParserExt := parse_type.Parser with group_count support
parse_opt_number = TypeBuilder.with_optional(parse_number)
parse_opt_number.name = "Number?"
type_dict = build_type_dict([parse_opt_number, parse_number])
schema = "Numbers: {:Number?} {:Number}"
parser = ParserExt(schema, type_dict)
# -- CASE: Optional number is present
result = parser.parse("Numbers: 34 12")
expected = (34, 12)
self.assertIsNotNone(result)
self.assertEqual(result.fixed, tuple(expected))
# -- CASE: Optional number is missing
result = parser.parse("Numbers: 12")
expected = (None, 12)
self.assertIsNotNone(result)
self.assertEqual(result.fixed, tuple(expected))
示例3: test_with_optional__choice
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import with_optional [as 别名]
def test_with_optional__choice(self):
# -- ALIAS FOR: zero_or_one
parse_color = TypeBuilder.make_choice(["red", "green", "blue"])
parse_opt_color = TypeBuilder.with_optional(parse_color)
self.check_parse_choice_with_optional(parse_opt_color)
示例4: test_with_optional__number
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import with_optional [as 别名]
def test_with_optional__number(self):
# -- ALIAS FOR: zero_or_one
parse_opt_number = TypeBuilder.with_optional(parse_number)
self.check_parse_number_with_optional(parse_opt_number)
示例5: parse_word_a
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import with_optional [as 别名]
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder
import parse
@parse.with_pattern(r"a\s+")
def parse_word_a(text):
"""Type converter for "a " (followed by one/more spaces)."""
return text.strip()
# -- SAME:
# parse_optional_word_a = TypeBuilder.with_zero_or_one(parse_word_a)
parse_optional_word_a = TypeBuilder.with_optional(parse_word_a)
register_type(optional_a_=parse_optional_word_a)
# @mark.steps
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then
from hamcrest import assert_that, equal_to, is_in
# -- OPTIONAL-PART: {:optional_a_}
# By using data type with cardinality zero or one (0..1, optional).
@when("attacked by {:optional_a_}{opponent}")
def step_attacked_by(context, a_, opponent):
context.ninja_fight.opponent = opponent