Another Sets Problem

Steve Holden steve at holdenweb.com
Mon Dec 28 14:07:03 EST 2009


Victor Subervi wrote:
> On Mon, Dec 28, 2009 at 1:41 PM, MRAB <python at mrabarnett.plus.com
> <mailto:python at mrabarnett.plus.com>> wrote:
> 
>     Victor Subervi wrote:
> 
>         Hi;
>         I'm using python 2.4.3 which apparently requires that I import Set:
>         from sets import Set
>         I've done this. In another script I successfully manipulated
>         MySQL sets by so doing. Here's the code snippet from the script
>         where I was able to call the elements in a for loop:
> 
>                  if isinstance(colValue[0], (str, int, float, long,
>         complex, unicode, list, buffer, xrange, tuple)):
>                    pass
>                  else:
>                    try:
>                      html = "<b>%s</b>: <select name='%s'>" % (col, col)
>                      notSet = 0
>                      for itm in colValue[0]:
>                        try:
>                          color, number = itm.split(':')
>                          html += "<option name='%s'>%s</option>" % (itm,
>         color)
>                        except:
> 
> 
>     DON'T USE BARE EXCEPTS!
> 
>     (There are 2 in your code.)
> 
> 
> There are times when they are *necessary*.
> 
Perhaps so, although it's extremely difficult to think of one since the
exceptions were rationalised. Do you *really* want to catch the
exception that occurs when the user tries to stop the program with a
Control-C? Usually nowadays you want

    except Exception:

as the "widest" specification. You don't *normally* want to catch
SystemExit, KeyboardInterrupt or GeneratorException.

> 
> 
>                          html += "<option name='%s'>%s</option>" % (itm,
>         itm)
>          However, when I try that in my current script, the script
>         fails. It throws no error, but rather just quits printing to the
>         screen. Here's the code snippet:
> 
>                      elif types[x][0:3] == 'set':
>                        for f in field:
>                          print '<td>%s</td>\n' % (field)
>                      else:
>                        print '<td>%s</td>\n' % (field)
> 
>     [snip]
> 
>     You're printing the entire field for each value in the field. Is this
>     intentional?
> 
> 
> It doesn't matter. The code ceases to execute with the line:
> 
> for f in field:
> beno
> 
Well it looks to me like types[x][0:3] == 'set' and f is empty. That
wouldn't produce any printed output.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/



More information about the Python-list mailing list