pyparsing: crash on empty element

Discussion in 'Python' started by gry@ll.mit.edu, Mar 6, 2006.

  1. Guest

    [python 2.3.3, pyparsing 1.3]
    I have:

    def unpack_sql_array(s):
    # unpack a postgres "array", e.g. "{'w1','w2','w3'}" into a
    list(str)
    import pyparsing as pp
    withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes)
    withoutquotes = pp.CharsNotIn('",{}')
    parser = pp.StringStart() + \
    pp.Literal('{').suppress() + \
    pp.delimitedList(withquotes | withoutquotes) + \
    pp.Literal('}').suppress() + \
    pp.StringEnd()
    return parser.parseString(s).asList()

    which works beautifully, except on the input: "{}". How can I neatly
    modify the parser to return an empty list in this case?
    Yes, obviously, I could say
    if s=='{}': return []
    It just seems like I'm missing some simple intrinsic way to get this
    out of the parser. I am hoping to become more skillful in using the
    wonderful pyparsing module!

    -- George Young
    , Mar 6, 2006
    #1
    1. Advertising

  2. Paul McGuire Guest

    To indicate that a particular parse expression may be empty, pyparsing
    includes the Optional element.

    Change:
    pp.delimitedList(withquotes | withoutquotes) + \

    to:
    pp.Optional( pp.delimitedList(withquotes | withoutquotes) ) + \

    and I think this will have the desired effect.

    -- Paul


    <> wrote in message
    news:...
    > [python 2.3.3, pyparsing 1.3]
    > I have:
    >
    > def unpack_sql_array(s):
    > # unpack a postgres "array", e.g. "{'w1','w2','w3'}" into a
    > list(str)
    > import pyparsing as pp
    > withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes)
    > withoutquotes = pp.CharsNotIn('",{}')
    > parser = pp.StringStart() + \
    > pp.Literal('{').suppress() + \
    > pp.delimitedList(withquotes | withoutquotes) + \
    > pp.Literal('}').suppress() + \
    > pp.StringEnd()
    > return parser.parseString(s).asList()
    >
    > which works beautifully, except on the input: "{}". How can I neatly
    > modify the parser to return an empty list in this case?
    > Yes, obviously, I could say
    > if s=='{}': return []
    > It just seems like I'm missing some simple intrinsic way to get this
    > out of the parser. I am hoping to become more skillful in using the
    > wonderful pyparsing module!
    >
    > -- George Young
    >
    Paul McGuire, Mar 6, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,023
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    803
    spiff
    Nov 10, 2005
  3. HANM
    Replies:
    2
    Views:
    706
    Joseph Kesselman
    Jan 29, 2008
  4. Paul McGuire

    Re: pyparsing: match empty line

    Paul McGuire, Sep 3, 2008, in forum: Python
    Replies:
    1
    Views:
    485
    Paul McGuire
    Sep 3, 2008
  5. c676228
    Replies:
    1
    Views:
    118
    Luke Zhang [MSFT]
    Sep 20, 2006
Loading...

Share This Page