SourceForge has been redesigned. Learn more.
Close

#1 allow None as default value for Optional parse element

closed
nobody
None
5
2006-04-01
2005-09-13
No

The current code for the Optional parse element does
not allow None as a default value:

class Optional(ParseElementEnhance):
...
def __init__( self, exprs, default=None ):
...

def parseImpl( self, instring, loc, doActions=True ):
...
if self.defaultValue is not None:
tokens = [ self.defaultValue ]
else:
tokens = []

I sometimes want to use None as the default value. I
could do that if the code were rewritten instead as
something like:

class Optional(ParseElementEnhance):
...
_undefined = object()
def __init__( self, exprs, default=_undefined ):
...

def parseImpl( self, instring, loc, doActions=True ):
...
if self.defaultValue is not self._undefined:
tokens = [ self.defaultValue ]
else:
tokens = []

There's a minor backwards compatibility concern, if
people have been passing None as a second argument
expecting to get the same behavior as a single
argument. This seems like it would be an uncommon
practice, but I don't know.

Discussion

  • Paul McGuire

    Paul McGuire - 2005-12-07

    Logged In: YES
    user_id=893320

    Steven -

    I have two minds on this:
    1. No problem. In fact, to address the backwards
    compatibility option, let's make _undefined a
    documented "noDefaultValue" value, that people could
    expressly pass (or take as the constructor default) to get
    the single-argument behavior.
    2. Wait a minute! Why make other people learn some non-
    Pythonic defaulting mechanism? Why don't you define the
    special form of None in your calling code in the first
    place? Define your own default argument, something
    like "exprNotPresent = object()", and pass that as the
    default value.

    I could go either way, what do you think?

    -- Paul

     
  • Paul McGuire

    Paul McGuire - 2005-12-07

    Logged In: YES
    user_id=893320

    Steven -

    I have two minds on this:
    1. No problem. In fact, to address the backwards
    compatibility option, let's make _undefined a
    documented "noDefaultValue" value, that people could
    expressly pass (or take as the constructor default) to get
    the single-argument behavior.
    2. Wait a minute! Why make other people learn some non-
    Pythonic defaulting mechanism? Why don't you define the
    special form of None in your calling code in the first
    place? Define your own default argument, something
    like "exprNotPresent = object()", and pass that as the
    default value.

    I could go either way, what do you think?

    -- Paul

     
  • Paul McGuire

    Paul McGuire - 2005-12-07

    Logged In: YES
    user_id=893320

    Steven -

    I have two minds on this:
    1. No problem. In fact, to address the backwards
    compatibility option, let's make _undefined a
    documented "noDefaultValue" value, that people could
    expressly pass (or take as the constructor default) to get
    the single-argument behavior.
    2. Wait a minute! Why make other people learn some non-
    Pythonic defaulting mechanism? Why don't you define the
    special form of None in your calling code in the first
    place? Define your own default argument, something
    like "exprNotPresent = object()", and pass that as the
    default value.

    I could go either way, what do you think?

    -- Paul

     
  • Anonymous

    Anonymous - 2005-12-08

    Logged In: YES
    user_id=945502

    > 2. Wait a minute! Why make other people learn some non-
    > Pythonic defaulting mechanism?

    I'm not entirely sure this is the usual notion of defaulting
    with None -- consider that dict.get() still returns None
    when no default is supplied.

    That said, I think the real complication is that I'm trying
    to use pyparsing to create objects that aren't strings. If
    you assume that the results of a pyparsing expression are
    always strings, and therefore default should always be a
    string, then passing None as a default value to indicate
    that nothing should be added to the list is perfectly
    reasonable. But if the default can be any type, then None
    is just as good an object as anything else. Since my uses
    of pyparsing almost always produce non-strings as results,
    not being able to produce all normal Python objects
    (including None) is a problem for me.

    OTOH, the documentation clearly states that the default
    object should be a string, so I can understand if wanting to
    create objects other than strings with pyparsing is not
    really supported. (Of course, I'm going to continue to do
    it because it's by far the simplest way to parse into Python
    objects some of the structures I encounter.) ;)

     
  • Paul McGuire

    Paul McGuire - 2006-03-28

    Logged In: YES
    user_id=893320

    This will be fixed in the next release, some time in the
    next few weeks. For simplicity, the special default value
    is purely internal to pyparsing, and is not exposed to the
    user.

    Thanks for being patient!

    -- Paul

     
  • Anonymous

    Anonymous - 2006-03-28

    Logged In: YES
    user_id=945502

    Great. Thanks!

     
  • Paul McGuire

    Paul McGuire - 2006-04-01

    Logged In: YES
    user_id=893320

    This feature was included in version 1.4.2, released today.

     
  • Paul McGuire

    Paul McGuire - 2006-04-01
    • status: open --> closed