Re: append_const (Was: Python 2.5 update)

Gregg Lind <[email protected]> Wed, 1 Nov 2006 17:08:25 +0000 (UTC)
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
Martin Blais <blais <at> furius.ca> writes:

> 
> By the way, for those using up to Python-2.4 (Optik 1.5a2), here is a
> simple trick to implement the append_const action with a callback:
> 
>     def append_const_cb( const ):
>         "Create callback to implement an append_const action via a callback."
>         def cb( option, opt_str, value, parser ):
>             getattr(parser.values, option.dest).append(const)
>         return cb
> 
>     parser.add_option(...,
>         action='callback', callback=append_const_cb(YOUR_CONSTANT),
>         ...)
> 
>     parser.add_option(...,
>         action='callback', callback=append_const_cb(OTHER_CONSTANT),
>         ...)
> 
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting language
> that extends applications into web and mobile media. Attend the live webcast
> and join the prime developer group breaking into this new coding territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
> 


One small addition: we need to set the destination variable to an empty list
when we first encounter it.  

def append_const_cb( const ):
    """Create callback to implement an append_const action via a callback.
    Needed in python 2.4, since append_const first appears in python2.5
    from:  http://comments.gmane.org/gmane.comp.python.optik.user/261
    """
    def cb( option, opt_str, value, parser ):
        if not getattr(parser.values, option.dest):
          setattr(parser.values, option.dest, list()) 
        getattr(parser.values, option.dest).append(const)
    return cb




-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642