need help with python syntax

Bengt Richter bokr at oz.net
Thu Aug 11 17:15:40 EDT 2005


On 11 Aug 2005 11:56:49 -0700, "yaffa" <yxxxxlxxxxx at gmail.com> wrote:

>dear python gurus,
>
>quick question on syntax.
>
>i have a line of code like this
>
>for incident in bs('tr',  {'bgcolor' : '#eeeeee'}):
>
>
>what i want it to do is look for 'bgcolor' : '#eeeeee' or 'bgcolor' :
>'white' and then do a whole bunch of stuff.
>
>i've tried this:
>
>for incident in bs('tr',  {'bgcolor' : '#eeeeee'} or {'bgcolor' :
>'white'} ):   but it only seems to pick up the stuff from the
>{'bgcolor' : '#eeeeee'}
>
>
>any ideas folks?
>
First, what is bs (LOL, sorry ;-) ?

Is it a function or class constructor that returns an iterator?
What do you expect incident successively to be bound to as you iterate?
Note that
    {'bgcolor':'#eeeeee'} or {'bgcolor' :'white'}
is an expression that is guaranteed never to evaluate to the 'or' part,
(and always to the first part) since bool({'bgcolor':'#eeeeee'}) is always True.

BTW, if you have a dict d which might define 'bgcolor' as '#eeeeee' or 'white'
you could check for either white something like (untested)

    if d['bgcolor'] in ('#eeeeee', 'white'): # put most common white code first for better speed
        print 'white'
    else:
        print 'not my idea of white'

or if you had a LOT of codes to check (for white or whatever) you could check them faster using a set,
e.g.,

    whites = set('#eeeeee white #dddddd'.split())  # not a LOT ;-)
    if d['bgcolor'] in whites:
        print 'white, sort of'
    ...

Regards,
Bengt Richter



More information about the Python-list mailing list