本文整理汇总了Python中parse_type.TypeBuilder.make_choice方法的典型用法代码示例。如果您正苦于以下问题:Python TypeBuilder.make_choice方法的具体用法?Python TypeBuilder.make_choice怎么用?Python TypeBuilder.make_choice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parse_type.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.make_choice方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_choice__anycase_accepted_case_sensitity
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_make_choice__anycase_accepted_case_sensitity(self):
# -- NOTE: strict=False => Disable errors due to case-mismatch.
parse_choice = TypeBuilder.make_choice(["one", "two", "three"],
strict=False)
schema = "Answer: {answer:NumberWordChoice}"
parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))
# -- PERFORM TESTS:
# NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
self.assert_match(parser, "Answer: one", "answer", "one")
self.assert_match(parser, "Answer: TWO", "answer", "TWO")
self.assert_match(parser, "Answer: Three", "answer", "Three")
示例2: test_make_choice__anycase_accepted_lowercase_enforced
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_make_choice__anycase_accepted_lowercase_enforced(self):
# -- NOTE: strict=True => Enable errors due to case-mismatch.
parse_choice = TypeBuilder.make_choice(["one", "two", "three"],
transform=lambda x: x.lower(), strict=True)
schema = "Answer: {answer:NumberWordChoice}"
parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))
# -- PERFORM TESTS:
# NOTE: Parser uses re.IGNORECASE flag
# => Any case accepted, but result is in lower case.
self.assert_match(parser, "Answer: one", "answer", "one")
self.assert_match(parser, "Answer: TWO", "answer", "two")
self.assert_match(parser, "Answer: Three", "answer", "three")
示例3: test_cardinality_field_with_zero_or_one
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_cardinality_field_with_zero_or_one(self):
# -- SETUP:
parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
parse_person.name = "Person" # For testing only.
extra_types = build_type_dict([ parse_person ])
schema = "Optional: {person:Person?}"
parser = parse.Parser(schema, extra_types)
# -- PERFORM TESTS:
self.assert_match(parser, "Optional: ", "person", None)
self.assert_match(parser, "Optional: Alice", "person", "Alice")
self.assert_match(parser, "Optional: Bob", "person", "Bob")
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "Optional: Anna", "person") # Similar1.
self.assert_mismatch(parser, "Optional: Boby", "person") # Similar2.
self.assert_mismatch(parser, "Optional: a", "person") # INVALID ...
示例4: test_without_cardinality_field
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_without_cardinality_field(self):
# -- IMPLCIT CARDINALITY: one
# -- SETUP:
parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
parse_person.name = "Person" # For testing only.
extra_types = build_type_dict([ parse_person ])
schema = "One: {person:Person}"
parser = parse.Parser(schema, extra_types)
# -- PERFORM TESTS:
self.assert_match(parser, "One: Alice", "person", "Alice")
self.assert_match(parser, "One: Bob", "person", "Bob")
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "One: ", "person") # Missing.
self.assert_mismatch(parser, "One: BAlice", "person") # Similar1.
self.assert_mismatch(parser, "One: Boby", "person") # Similar2.
self.assert_mismatch(parser, "One: a", "person") # INVALID ...
示例5: test_make_choice__with_transform
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_make_choice__with_transform(self):
transform = lambda x: x.upper()
parse_choice = TypeBuilder.make_choice(["ONE", "two", "Three"],
transform)
self.assertSequenceEqual(parse_choice.choices, ["ONE", "TWO", "THREE"])
schema = "Answer: {answer:NumberWordChoice}"
parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))
# -- PERFORM TESTS:
self.assert_match(parser, "Answer: one", "answer", "ONE")
self.assert_match(parser, "Answer: two", "answer", "TWO")
self.ensure_can_parse_all_choices(parser,
parse_choice, "Answer: %s", "answer")
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "Answer: __one__", "answer")
self.assert_mismatch(parser, "Answer: one ", "answer")
self.assert_mismatch(parser, "Answer: one ZZZ", "answer")
示例6: test_with_one_or_more_choice
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_with_one_or_more_choice(self):
parse_color = TypeBuilder.make_choice(["red", "green", "blue"])
parse_colors = TypeBuilder.with_one_or_more(parse_color)
parse_colors.name = "Colors"
extra_types = build_type_dict([ parse_colors ])
schema = "List: {colors:Colors}"
parser = parse.Parser(schema, extra_types)
# -- PERFORM TESTS:
self.assert_match(parser, "List: green", "colors", [ "green" ])
self.assert_match(parser, "List: red, green", "colors", [ "red", "green" ])
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "List: ", "colors") # Zero items.
self.assert_mismatch(parser, "List: x", "colors") # Not a Color.
self.assert_mismatch(parser, "List: black", "colors") # Unknown
self.assert_mismatch(parser, "List: red,", "colors") # Trailing sep.
self.assert_mismatch(parser, "List: a, b", "colors") # List of ...
示例7: test_make_choice__samecase_match_or_error
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_make_choice__samecase_match_or_error(self):
# -- NOTE: strict=True => Enable errors due to case-mismatch.
parse_choice = TypeBuilder.make_choice(["One", "TWO", "three"],
strict=True)
schema = "Answer: {answer:NumberWordChoice}"
parser = parse.Parser(schema, dict(NumberWordChoice=parse_choice))
# -- PERFORM TESTS: Case matches.
# NOTE: Parser uses re.IGNORECASE flag => Any case accepted.
self.assert_match(parser, "Answer: One", "answer", "One")
self.assert_match(parser, "Answer: TWO", "answer", "TWO")
self.assert_match(parser, "Answer: three", "answer", "three")
# -- PERFORM TESTS: EXACT-CASE MISMATCH
case_mismatch_input_data = ["one", "ONE", "Two", "two", "Three" ]
for input_value in case_mismatch_input_data:
input_text = "Answer: %s" % input_value
with self.assertRaises(ValueError):
parser.parse(input_text)
示例8: test_cardinality_field_with_one_or_more
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def test_cardinality_field_with_one_or_more(self):
# -- SETUP:
parse_person = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
parse_person.name = "Person" # For testing only.
extra_types = build_type_dict([ parse_person ])
schema = "List: {persons:Person+}"
parser = parse.Parser(schema, extra_types)
# -- PERFORM TESTS:
self.assert_match(parser, "List: Alice", "persons", [ "Alice" ])
self.assert_match(parser, "List: Bob", "persons", [ "Bob" ])
self.assert_match(parser, "List: Bob, Alice",
"persons", [ "Bob", "Alice" ])
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "List: ", "persons") # Zero items.
self.assert_mismatch(parser, "List: BAlice", "persons") # Unknown1.
self.assert_mismatch(parser, "List: Boby", "persons") # Unknown2.
self.assert_mismatch(parser, "List: Alice,", "persons") # Trailing,
self.assert_mismatch(parser, "List: a, b", "persons") # List of...
示例9: test_with_optional__choice
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [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)
示例10: Meeting
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
# DOMAIN MODEL:
# ----------------------------------------------------------------------------
class Meeting(object):
def __init__(self):
self.persons = set()
# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import matchers
from parse_type import TypeBuilder
company_persons = [ "Alice", "Bob", "Charly", "Dodo" ]
parse_person = TypeBuilder.make_choice(company_persons)
matchers.register_type(Person=parse_person)
# -- MANY-TYPE: Persons := list<Person> with list-separator = "and"
# parse_persons = TypeBuilder.with_one_or_more(parse_person, listsep="and")
parse_persons = TypeBuilder.with_many(parse_person, listsep="and")
matchers.register_type(PersonAndMore=parse_persons)
# @mark.steps
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then
# -- MANY-VARIANT 1: Use Cardinality field in parse expression (comma-separated)
示例11: Color
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
"on": True, "off": False,
"true": True, "false": False,
})
parse_yesno.name = "YesNo" # For testing only.
# -- ENUM CLASS:
class Color(Enum):
red = 1
green = 2
blue = 3
parse_color = TypeBuilder.make_enum(Color)
parse_color.name = "Color"
# -- CHOICE DATATYPE:
parse_person_choice = TypeBuilder.make_choice(["Alice", "Bob", "Charly"])
parse_person_choice.name = "PersonChoice" # For testing only.
# -----------------------------------------------------------------------------
# ABSTRACT TEST CASE:
# -----------------------------------------------------------------------------
class TestCase(unittest.TestCase):
# -- PYTHON VERSION BACKWARD-COMPATIBILTY:
if not hasattr(unittest.TestCase, "assertIsNone"):
def assertIsNone(self, obj, msg=None):
self.assert_(obj is None, msg)
def assertIsNotNone(self, obj, msg=None):
self.assert_(obj is not None, msg)
示例12: calculate_price_for_vegetable
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
def calculate_price_for_vegetable(self, vegetable, amount):
price_per_unit = self.vegetable_price_list[vegetable]
return price_per_unit*amount
def calculate_price_for(self, shop_item, amount):
price_per_unit = self.common_price_list[shop_item]
return price_per_unit*amount
# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder
parse_vegetable = TypeBuilder.make_choice(["cucumbers", "lettuce"])
register_type(Vegetable=parse_vegetable)
parse_fruit = TypeBuilder.make_choice(["apples", "pears"])
register_type(Fruit=parse_fruit)
# @mark.steps
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then
@given(u"I go to a shop")
def step_given_I_go_to_a_shop(context):
context.shop = Shop()
context.shopping_cart = [ ]
示例13: items
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
Scenario:
Given I go to a shop to buy ingredients for a meal
And I buy apples
And I buy beef
"""
# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder
# -- CHOICE: Constrain to a list of supported items (as string).
offered_shop_items = [ "apples", "beef", "potatoes", "pork" ]
parse_shop_item = TypeBuilder.make_choice(offered_shop_items)
register_type(ShopItem=parse_shop_item)
# @mark.steps
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then
@given(u"I go to a shop to buy ingredients for a meal")
def step_given_I_go_to_a_shop(context):
context.shopping_cart = [ ]
@when(u"I buy {shop_item:ShopItem}")
def step_when_I_buy(context, shop_item):
assert shop_item in offered_shop_items
示例14: slurp_space
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_choice [as 别名]
| blue |
"""
# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder
def slurp_space(text):
return text
slurp_space.pattern = r"\s*"
register_type(slurp_space=slurp_space)
parse_color = TypeBuilder.make_choice([ "red", "green", "blue", "yellow" ])
register_type(Color=parse_color)
# -- MANY-TYPE: Persons := list<Person> with list-separator = "and"
# parse_colors = TypeBuilder.with_many0(parse_color, listsep="and")
parse_colors0A= TypeBuilder.with_zero_or_more(parse_color, listsep="and")
register_type(OptionalColorAndMore=parse_colors0A)
# -- NEEDED-UNTIL: parse_type.cfparse.Parser is used by behave.
# parse_colors0C = TypeBuilder.with_zero_or_more(parse_color)
# type_dict = {"Color*": parse_colors0C}
# register_type(**type_dict)
# @mark.steps
# ----------------------------------------------------------------------------