parse
parse copied to clipboard
Allow a field in the parse format to be optional
The suggested syntax from jenisys is to suffix the type with "?" which I believe is reasonable. Thus:
{honorific:s?} {given:s} {sur:s}
would match both of:
"Mr Richard Jones" "Jens Engels"
The "honorific" element in the result object would have the value None.
+1
Note that you can already use this syntax without any change in parse
as long as you provide your own type converters for 2 variants (one for cardinality=1 and for cardinality=0..1). But that is normally the tricky part.
EXAMPLE:
from parse import Parser, with_pattern
from parse_type import TypeBuilder
@with_pattern(r"\d+")
def parse_number(text):
return int(text)
parse_optional_number = TypeBuilder.with_optional(parse_number)
type_dict = {"Number": parse_number, "Number?": parse_optional_number}
schema = "Hello {number1:Number} {number2:Number?}"
parser = Parser(schema, type_dict)
result = parser.parse("Hello 12 13")
assert result.number1 == 12
assert result.number2 == 13
result = parser.parse("Hello 12 ")
assert result.number1 == 12
assert result.number2 == None
The code is provided at https://github.com/jenisys/parse_type Currently a working draft based on extensions of my parse fork.
A None value for a not present field would be a great thing.
Lack of this feature sent me back to regex, unfortunately.
Just to be clear: I don't consider parse to be a replacement for all use cases of regex. I don't want it to become complicated to understand because it tries to be.
Just to mention it again: As already stated above, this problem is already solved in parse_type
, that extends the existing parse
module that is provided here.
@jenisys Thank you for the alternative :) Nevertheless, I, too, would be glad to have this option in this package ("parse") for two reasons:
- Using "parse_type" adds another dependency to the project, when both packages aim to achieve the same goal.
- I understand @r1chardj0n3s 's point (not wanting to complicate the package), but "?" seems to be a very common tool regex tool.
Thank you in advance, and thank you for the package, very fun to use :)