本文整理汇总了Python中parse_type.TypeBuilder类的典型用法代码示例。如果您正苦于以下问题:Python TypeBuilder类的具体用法?Python TypeBuilder怎么用?Python TypeBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TypeBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_enum_with_enum_class
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_make_variant__with_color_or_person
def test_make_variant__with_color_or_person(self):
type_converters = [parse_color, parse_person_choice]
parse_variant2 = TypeBuilder.make_variant(type_converters)
schema = "Variant2: {variant:Color_or_Person}"
parser = parse.Parser(schema, dict(Color_or_Person=parse_variant2))
# -- TYPE 1: Color
self.assert_match(parser, "Variant2: red", "variant", Color.red)
self.assert_match(parser, "Variant2: blue", "variant", Color.blue)
# -- TYPE 2: Person
self.assert_match(parser, "Variant2: Alice", "variant", "Alice")
self.assert_match(parser, "Variant2: Bob", "variant", "Bob")
self.assert_match(parser, "Variant2: Charly", "variant", "Charly")
# -- PARSE MISMATCH:
self.assert_mismatch(parser, "Variant2: __Alice__")
self.assert_mismatch(parser, "Variant2: Alice ")
self.assert_mismatch(parser, "Variant2: Alice2")
self.assert_mismatch(parser, "Variant2: red2")
# -- PERFORM TESTS:
self.ensure_can_parse_all_enum_values(parser,
parse_color, "Variant2: %s", "variant")
self.ensure_can_parse_all_choices(parser,
parse_person_choice, "Variant2: %s", "variant")
示例3: test_with_zero_or_more__choice
def test_with_zero_or_more__choice(self):
parse_color = TypeBuilder.make_choice(["red", "green", "blue"])
parse_colors = TypeBuilder.with_zero_or_more(parse_color)
parse_colors.name = "Colors0"
extra_types = build_type_dict([ parse_colors ])
schema = "List: {colors:Colors0}"
parser = parse.Parser(schema, extra_types)
# -- PERFORM TESTS:
self.assert_match(parser, "List: ", "colors", [ ])
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: 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 ...
示例4: test_make_variant__with_strict_raises_error_on_case_mismatch
def test_make_variant__with_strict_raises_error_on_case_mismatch(self):
# -- NEEDS:
# * re_opts=0 (IGNORECASE disabled)
# * strict=True, allow that an error is raised
type_converters = [parse_number, parse_yesno]
parse_variant = TypeBuilder.make_variant(type_converters,
strict=True, re_opts=0)
schema = "Variant: {variant:YesNo_or_Number}"
parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
self.assertRaises(AssertionError, parser.parse, "Variant: YES")
示例5: test_with_one_or_more_enum
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 ...
示例6: test_make_variant__without_strict_may_return_none_on_case_mismatch
def test_make_variant__without_strict_may_return_none_on_case_mismatch(self):
# -- NEEDS:
# * re_opts=0 (IGNORECASE disabled)
# * strict=False, otherwise an error is raised
type_converters = [parse_number, parse_yesno]
parse_variant = TypeBuilder.make_variant(type_converters, re_opts=0,
strict=False)
schema = "Variant: {variant:YesNo_or_Number}"
parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
result = parser.parse("Variant: No")
self.assertNotEqual(result, None)
self.assertEqual(result["variant"], None)
示例7: test_with_one_or_more_with_other_separator
def test_with_one_or_more_with_other_separator(self):
parse_numbers2 = TypeBuilder.with_one_or_more(parse_number, listsep=';')
parse_numbers2.name = "Numbers2"
extra_types = build_type_dict([ parse_numbers2 ])
schema = "List: {numbers:Numbers2}"
parser = parse.Parser(schema, extra_types)
# -- PERFORM TESTS:
self.assert_match(parser, "List: 1", "numbers", [ 1 ])
self.assert_match(parser, "List: 1; 2", "numbers", [ 1, 2 ])
self.assert_match(parser, "List: 1; 2; 3", "numbers", [ 1, 2, 3 ])
示例8: test_make_choice__anycase_accepted_case_sensitity
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")
示例9: test_make_choice2__with_transform
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"))
示例10: test_make_choice__anycase_accepted_lowercase_enforced
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")
示例11: test_make_variant__with_strict_and_compiled_raises_error_on_case_mismatch
def test_make_variant__with_strict_and_compiled_raises_error_on_case_mismatch(self):
# XXX re_opts=0 seems to work differently.
# -- NEEDS:
# * re_opts=0 (IGNORECASE disabled)
# * strict=True, allow that an error is raised
type_converters = [parse_number, parse_yesno]
# -- ENSURE: coverage for cornercase.
parse_number.matcher = re.compile(parse_number.pattern)
parse_variant = TypeBuilder.make_variant(type_converters,
compiled=True, re_opts=0, strict=True)
schema = "Variant: {variant:YesNo_or_Number}"
parser = parse.Parser(schema, dict(YesNo_or_Number=parse_variant))
# XXX self.assertRaises(AssertionError, parser.parse, "Variant: YES")
result = parser.parse("Variant: Yes")
self.assertNotEqual(result, None)
self.assertEqual(result["variant"], True)
示例12: test_cardinality_field_with_zero_or_one
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 ...
示例13: test_make_choice__with_transform
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")
示例14: test_without_cardinality_field
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 ...
示例15: test_parse_with_optional_and_named_fields
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)