-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a field in the parse format to be optional #13
Comments
+1 |
Note that you can already use this syntax without any change in 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 |
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 |
@jenisys
Thank you in advance, and thank you for the package, very fun to use :) |
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.
The text was updated successfully, but these errors were encountered: