need help with python syntax

Y

yaffa

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?

thanks

yaffa
 
B

Bengt Richter

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
 
B

Bruno Desthuilliers

yaffa a écrit :
dear python gurus,

One effectively needs to have some guru-powers to answer you question...
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

Where ?
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'} ):

What do you think the expression
{'bgcolor' : '#eeeeee'} or {'bgcolor' :'white'}
evaluate to ?

(hint 1: Python has an interactive interpreter that let you test
expressions on the fly)
(hint 2: http://www.python.org/doc/2.4.1/ref/Booleans.html)
but it only seems to pick up the stuff from the
{'bgcolor' : '#eeeeee'}

Yes. That's very very True.
any ideas folks?
Yes.

1/ consider learning enough of Python to understand what you are doing.
2/ consider reading the doc (or the source code, which is usually the
most accurate documentation...) of 'bs()' (wherever this comes from).

Anyway, if you hope us to *guess* what it does and what args it expects
to do it, you're just plain wrong - sorry, but we (well, I at least)
don't have the expected psychic mind-reading skills necessary to answer
your question without much informations.

And, BTW, the bitwise or operator (|) won't help...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,985
Latest member
kazewi

Latest Threads

Top