本文整理汇总了Python中parse_type.TypeBuilder.make_enum方法的典型用法代码示例。如果您正苦于以下问题:Python TypeBuilder.make_enum方法的具体用法?Python TypeBuilder.make_enum怎么用?Python TypeBuilder.make_enum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parse_type.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.make_enum方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_enum_with_enum_class
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_enum [as 别名]
def test_make_enum_with_enum_class(self):
"""
Use :meth:`parse_type.TypeBuilder.make_enum()` with enum34 classes.
"""
class Color(Enum):
red = 1
green = 2
blue = 3
parse_color = TypeBuilder.make_enum(Color)
parse_color.name = "Color"
schema = "Answer: {color:Color}"
parser = parse.Parser(schema, dict(Color=parse_color))
# -- PERFORM TESTS:
self.ensure_can_parse_all_enum_values(parser,
parse_color, "Answer: %s", "color")
# -- VALID:
self.assert_match(parser, "Answer: red", "color", Color.red)
self.assert_match(parser, "Answer: green", "color", Color.green)
self.assert_match(parser, "Answer: blue", "color", Color.blue)
# -- IGNORE-CASE: In parsing, calls type converter function !!!
self.assert_match(parser, "Answer: RED", "color", Color.red)
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "Answer: __RED__", "color")
self.assert_mismatch(parser, "Answer: red ", "color")
self.assert_mismatch(parser, "Answer: redx", "color")
self.assert_mismatch(parser, "Answer: redx ZZZ", "color")
示例2: test_with_one_or_more_enum
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_enum [as 别名]
def test_with_one_or_more_enum(self):
parse_color = TypeBuilder.make_enum({"red": 1, "green":2, "blue": 3})
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", [ 2 ])
self.assert_match(parser, "List: red, green", "colors", [ 1, 2 ])
# -- 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 ...
示例3: int
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_enum [as 别名]
turns text into an int to help make feature files nicer to read
:param text: str text to parse into an int
:return: int parsed text
"""
return int(text.strip())
parse_numbers = TypeBuilder.with_many(parse_number, listsep=',')
register_type(ListInts=parse_numbers)
true_false_mapper = {
'True': True,
'False': False
}
parse_bool = TypeBuilder.make_enum(true_false_mapper)
register_type(Boolean=parse_bool)
use_step_matcher('parse')
@when("I find the multiples of 3 and 5 under {limit:d}")
def i_find_multiples_of_3_and_5_under(context, limit):
context.limit = limit
context.sequence = find_multiples_of_3_and_5(limit)
@when("I find the fibonacci numbers under {limit:d}")
def i_find_the_fibonacci_numbers_under(context, limit):
context.limit = limit
示例4: parse_number
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_enum [as 别名]
import unittest
# -----------------------------------------------------------------------------
# TEST SUPPORT FOR: TypeBuilder Tests
# -----------------------------------------------------------------------------
# -- PROOF-OF-CONCEPT DATATYPE:
def parse_number(text):
return int(text)
parse_number.pattern = r"\d+" # Provide better regexp pattern than default.
parse_number.name = "Number" # For testing only.
# -- ENUM DATATYPE:
parse_yesno = TypeBuilder.make_enum({
"yes": True, "no": False,
"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"])
示例5: True
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_enum [as 别名]
# ------------------------------------------------------------------------
answer_oracle = {
"Do you love me?": True,
"Do you hate me?": False,
"Do you kiss me?": True,
}
# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder
# -- ENUM: Returns True (for "yes" and "jubilee"), False (for "no")
parse_yesno = TypeBuilder.make_enum({"yes": True, "no": False, "jubilee": True})
register_type(YesNo=parse_yesno)
# @mark.steps
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import when, then
from hamcrest import assert_that, equal_to
@when(u'Romeo asks Julia: "{question}"')
def step_when_romeo_asks_julia(context, question):
context.question = question
示例6: before_scenario
# 需要导入模块: from parse_type import TypeBuilder [as 别名]
# 或者: from parse_type.TypeBuilder import make_enum [as 别名]
import os
import tempfile
import shutil
import sys
from parse_type import TypeBuilder
from behave import register_type
parse_bool = TypeBuilder.make_enum({"False": False, "True": True})
register_type(Bool=parse_bool)
def before_scenario(context, scenario):
context.tmpdir = tempfile.mkdtemp()
sys.path.append(context.tmpdir + '/output')
os.chdir(context.tmpdir)
def after_scenario(context, scenario):
sys.path.remove(context.tmpdir + '/output')
os.chdir('/')
shutil.rmtree(context.tmpdir)