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


Python TypeBuilder.with_optional方法代码示例

本文整理汇总了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)
开发者ID:amigadave,项目名称:parse_type,代码行数:21,代码来源:test_cardinality.py

示例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))
开发者ID:amigadave,项目名称:parse_type,代码行数:23,代码来源:test_cardinality.py

示例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)
开发者ID:amigadave,项目名称:parse_type,代码行数:7,代码来源:test_cardinality.py

示例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)
开发者ID:amigadave,项目名称:parse_type,代码行数:6,代码来源:test_cardinality.py

示例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
开发者ID:CoolCloud,项目名称:behave.example,代码行数:33,代码来源:step_cardinality_zero_or_one.py


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