need help with python syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yaffa

    #1

    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

  • Bengt Richter

    #2
    Re: need help with python syntax

    On 11 Aug 2005 11:56:49 -0700, "yaffa" <yaffalandis@gm ail.com> wrote:
    [color=blue]
    >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?
    >[/color]
    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':'#ee eeee'} 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

    Comment

    • Bruno Desthuilliers

      #3
      Re: need help with python syntax

      yaffa a écrit :[color=blue]
      > dear python gurus,[/color]

      One effectively needs to have some guru-powers to answer you question...
      [color=blue]
      > 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[/color]

      Where ?
      [color=blue]
      > 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'} ):[/color]

      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)
      [color=blue]
      > but it only seems to pick up the stuff from the
      > {'bgcolor' : '#eeeeee'}[/color]

      Yes. That's very very True.
      [color=blue]
      >
      > any ideas folks?
      >[/color]
      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...

      Comment

      Working...