By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

need help with python syntax

P: n/a
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

Aug 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 11 Aug 2005 11:56:49 -0700, "yaffa" <ya*********@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
Aug 11 '05 #2

P: n/a
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...
Aug 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.