[Tutor] Quick question

Eric Brunson brunson at brunson.com
Fri Sep 21 21:39:18 CEST 2007


There are quite a few ways to do what you want.  Here's are a few 
variations on a theme:

try:
    if not any( ( len(queuePacket['procSeq']),
                  len(queuePacket['opacSeq']),
                  len(queuePacket['keySeq']) ) ):
        # Do your stuff here
        do_stuff()
except KeyError:
    pass


Or, using a generator:

try:
    if not any( len(queuePacket[key]) 
                for key in ( 'procSeq', 'opacSeq', 'keySeq' ) ):
        # Do your stuff here
        do_stuff()
except KeyError:
    pass


Or, using a list comprehension:

try:
    if not [ 1 for key in ( 'procSeq', 'opacSeq', 'keySeq' ) if len(queuePacket[key]) != 0 ] ):
        # Do your stuff here
        do_stuff()
except KeyError:
    pass


Tino Dai wrote:
> Is there a more pythonic way of doing this:
>
>   if queuePacket.has_key('procSeq') and \
>   queuePacket.has_key('opacSeq') and \
>   queuePacket.has_key('keySeq') and \
>   len(queuePacket['procSeq']) == 0 and \
>   len(queuePacket['opacSeq']) == 0 and \
>  len(queuePacket['keySeq']) == 0:
>
>
> ?
>
> -Thanks,
> Tino
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list