parse icon indicating copy to clipboard operation
parse copied to clipboard

Allow a field in the parse format to be optional

Open r1chardj0n3s opened this issue 11 years ago • 7 comments

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.

r1chardj0n3s avatar Dec 03 '12 05:12 r1chardj0n3s

+1

salvatorelionetti avatar Oct 18 '13 21:10 salvatorelionetti

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.

jenisys avatar Oct 19 '13 20:10 jenisys

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

derVedro avatar Dec 12 '17 17:12 derVedro

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

harristeague avatar Apr 11 '20 02:04 harristeague

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.

r1chardj0n3s avatar Apr 11 '20 02:04 r1chardj0n3s

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 avatar Apr 11 '20 11:04 jenisys

@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 :)

GuySuday avatar Apr 22 '22 16:04 GuySuday