jenisys / parse_type

parse_type extends the parse module (opposite of string.format())

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parse standard types with cardinality causes traceback

rscrimojr opened this issue · comments

I am using Python 2.7.10, MacOS Sierra version 10.12.4 and parse_type version 0.3.4

Below are specific examples but I have tried other parse standard types, e.g. 'd', 'g', etc. with the similar exceptions. Also, if I am not using the library correctly, please let me know what I should do to correct my issues.

E.g.

>>> from parse_type.cfparse import Parser
>>> p = Parser(r'I {method:S} the {fruits:S+}', {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cfparse.py", line 44, in __init__
    missing = self.create_missing_types(schema, extra_types, type_builder)
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cfparse.py", line 70, in create_missing_types
    return type_builder.create_type_variants(missing, type_dict)
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cardinality_field.py", line 163, in create_type_variants
    type_variant = cls.create_type_variant(type_name, type_dict)
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cardinality_field.py", line 135, in create_type_variant
    raise MissingTypeError(primary_name)
parse_type.cardinality_field.MissingTypeError: 'S'

Then if I do not use named fields, I get this:

>>> from parse_type.cfparse import Parser
>>> p = Parser(r'I {:S} the {:S+}', {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cfparse.py", line 44, in __init__
    missing = self.create_missing_types(schema, extra_types, type_builder)
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cfparse.py", line 70, in create_missing_types
    return type_builder.create_type_variants(missing, type_dict)
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cardinality_field.py", line 163, in create_type_variants
    type_variant = cls.create_type_variant(type_name, type_dict)
  File ".../python-2.7.10/lib/python2.7/site-packages/parse_type/cardinality_field.py", line 135, in create_type_variant
    raise MissingTypeError(primary_name)
parse_type.cardinality_field.MissingTypeError: 'S'

I don't think this is supposed to happen.

However, when I do the following, we get the expected result:

>>> from parse_type.cfparse import Parser
>>> p = Parser(r'I {:S} the {:S}.', {})
>>> m = p.search(r'I 9 the 10.', evaluate_result=False)
>>> m
<parse.Match object at 0x106d95d90>
>>> m.evaluate_result()
<Result ('9', '10') {}>
>>> m = p.search(r'I eat the apple,pear,banana.', evaluate_result=False)
>>> m
<parse.Match object at 0x106d95b90>
>>> m.evaluate_result()
<Result ('eat', 'apple,pear,banana') {}>
>>>

Also:

>>> from parse_type.cfparse import Parser
>>> p = Parser(r'I {method:S} the {fruits:S}.', {})
>>> m = p.search(r'I eat the apple,pear,banana.', evaluate_result=False)
>>> m
<parse.Match object at 0x1037a7bd0>
>>> m.evaluate_result()
<Result () {'method': 'eat', 'fruits': 'apple,pear,banana'}>
>>>

Cardinality field support requires that you have the corresponding type information for cardinality=1, meaning:

  • type converter function (to use to support the other cardinalities)
  • type pattern

Currently, this is only supported for user-defined types. The information for the built-in types in the parse module are deeply embedded in the implementation and not easily accessible. Therefore, when you use the Cardinality field support, you need a user-defined type first.

NOTE: The exception in the traceback clearly states that is information is missing.