473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help with python syntax

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
2 1388
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Crew Reynolds | last post by:
I want to create a dictionary of note on/off timings and access it by the name of the object. The following code works for one row. a = "Hammer1" b = notes = {a:b} print notes >>> 45
21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
75
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the...
6
by: Jeff Duffy | last post by:
Hi all. I've been wondering why python itself doesn't provide a switch to check a file for valid syntax. I know that you can currently call python -c "import py_compile;...
4
by: yaffa | last post by:
dear folks, i'm trying to append a semicolon to my addr string and am using the syntax below. for some reason the added on of the ; doesn't work. when i print it out later on it only shows the...
31
by: metaperl | last post by:
-- python -i File "<stdin>", line 1 class = "algebra" ^ SyntaxError: invalid syntax Why isn' t the parser smart enough to see that class followed by an identifier is used for class...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
3
by: Kinokunya | last post by:
Hi guys, My group and I will be working on our final year project, the scope to do a program/web-based application similar areas of functionalities like the PyLint and PyChecker; a Python syntax...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.