Skip to content
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

Open
r1chardj0n3s opened this issue Dec 3, 2012 · 7 comments
Open

Allow a field in the parse format to be optional #13

r1chardj0n3s opened this issue Dec 3, 2012 · 7 comments
Labels

Comments

@r1chardj0n3s
Copy link
Owner

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.

@salvatorelionetti
Copy link

+1

@jenisys
Copy link
Contributor

jenisys commented Oct 19, 2013

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.

@derVedro
Copy link

A None value for a not present field would be a great thing.

@harristeague
Copy link

Lack of this feature sent me back to regex, unfortunately.

@r1chardj0n3s
Copy link
Owner Author

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.

@jenisys
Copy link
Contributor

jenisys commented Apr 11, 2020

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.

@GuySuday
Copy link

@jenisys
Thank you for the alternative :)
Nevertheless, I, too, would be glad to have this option in this package ("parse") for two reasons:

  1. Using "parse_type" adds another dependency to the project, when both packages aim to achieve the same goal.
  2. 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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants