On Tue, Dec 29, 2009 at 6:29 AM, Jean-Michel Pichavant < jeanmic...@sequans.com> wrote:
> Matt Nordhoff wrote: > >> Victor Subervi wrote: >> >> >>> On Mon, Dec 28, 2009 at 1:41 PM, MRAB <pyt...@mrabarnett.plus.com >>> <mailto:pyt...@mrabarnett.plus.com>> wrote: >>> >>> >>>> DON'T USE BARE EXCEPTS! >>>> >>>> (There are 2 in your code.) >>>> >>>> >>> There are times when they are *necessary*. >>> >>> >> >> No, there aren't. >> >> Even if there were, this is not one of those situations. >> >> > And to elaborate a little bit, someone said in this list (sorry, don't > remember who) that often people think that making their code robust is one > of the top priority, especially when you are providing some services to > clients. That could be true. The fact is that most newcomers thinks bare try > except will do the trick: "look, my server never crashes". Yes it does not > crash, but even worse, it handles exception in an inapropriate way that > leads the server to behave in a reliable, yet unpredictable, manner. And > that is definitely *not* being robust. > You all have made very good points about bare excepts. I promise you I will work on this...AFTER I've finished the first working copy of this shopping cart and gotten caught up on my work, and start to clean this shopping cart up to make it truly professional. HOWEVER, there is NO bare except influencing the problem which I am trying to fix. Can we PLEASE set this issue aside and deal with the problem of this post?? Here it is again: On Mon, Dec 28, 2009 at 4:23 PM, Steve Holden <st...@holdenweb.com> wrote: > There is only one way for the piece of code you quote to print nothing > (unless you suspect a bug in the Python interpreter, but the probability > of that is so low compared with the probability of your making a mistake > in interpretation of the data that I am going to ignore it). > > Hence my deduction. If types[x][0:3] != 'set' then the else clause would > definitely print something. Hence types[x][0:3] == 'set', and the for > statement is executing. But again, if field were anything other than an > empty container it would produce printed output. > > But if it's empty, no printed output would be produced: > > >>> field = set([]) > >>> for f in field: > ... print "Steve is wrong" > ... > >>> > > Does this make sense? > Boy, does it! It looks great...on paper. Here's some revised code to debug: # print 'XXX', types[x] elif types[x][0:3] == 'set': for f in field: print '<td>AAA%s</td>\n' % (field) else: print 'YYY' 1) If I uncomment the commented line, it throws this error: [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] File "/var/www/html/angrynates.com/cart/enterProducts2.py", line 138, referer: http://angrynates.com/cart/enterProducts.py [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] elif types[x][0:3] == 'set':, referer: http://angrynates.com/cart/enterProducts.py [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] ^, referer: http://angrynates.com/cart/enterProducts.py [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] SyntaxError: invalid syntax, referer: http://angrynates.com/cart/enterProducts.py [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] Premature end of script headers: enterProducts2.py, referer: http://angrynates.com/cart/enterProducts.py 2) AAA prints out before the following fields: AAASet(['Extra-small']) AAASet(['purple:50404D']) 3) YYY *also* prints out twice!! Here's the complete code once again: - Show quoted text - #! /usr/bin/python import MySQLdb import cgi import sys,os sys.path.append(os.getcwd()) from login import login from sets import Set def enterProducts2(): print '''Content-type: text/html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <form enctype='multipart/form-data' action='enterProducts3.py' method='post'> ''' form = cgi.FieldStorage() store = form.getfirst('store') print "<input type='hidden' name='store' value='%s' />" % store user, passwd, db, host = login() count = 0 count2 = 0 db = MySQLdb.connect(host, user, passwd, db) cursor = db.cursor() cursor.execute('select ID from %s;' % store) test = cursor.fetchall() if len(test) > 0: cursor.execute('show columns from %s;' % store) colNames = [itm[0] for itm in cursor] types = [itm[1] for itm in cursor] colNamesWithCommas = ', '.join(colNames) try: cursor.execute('select ID from %s;' % store) ids = cursor.fetchall() if store == 'prescriptions': cursor.execute('show tables like "%PersonalData";') personalDataTables = [itm[0] for itm in cursor] else: personalDataTables = [] print '<h1>%s</h1>\n<table><tr><td>\n' % (store[0].upper() + store[1:]) print '<h3>What do you want to do?</h3>\n<table><tr><td>\n' print "<input type=radio name='whatDo' value='insert' />Add<br />\n" print "<input type=radio name='whatDo' value='update' />Update<br />\n" print "<input type=radio name='whatDo' value='delete' />Delete<br />\n" print '</td></tr></table>\n<br /><br />\n' print '<table border=1>\n' print '<tr>\n' print '<th align=center><b>Check</b></th>\n' i = 0 while i < len(colNames): if i == 0: # This is the ID field print '<th align=center><b>', colNames[i], '</b></th>\n' try: cursor.execute('describe relationships%s;' % (store[0].upper() + store[1:])) relationshipsDescription = cursor.fetchall() cursor.execute('select * from relationships%s where %sID="%s";' % (store[0].upper() + store[1:], store[0].upper() + store[1:], ids[0][0])) relationshipFields = cursor.fetchone() j = 0 for relDescrip in relationshipsDescription: if j != 0: # Skip the store ID print '<th><b>%s Name</b></th>\n' % (relDescrip[0][:-2]) j += 1 except: pass # There are no relationships else: print '<th align=center><b>', colNames[i], '</b></th>\n' i += 1 print '</tr>\n' j = 0 z = 3 a = 0 for id in ids: a += 1 j += 1 for d in id: bg = ['#ffffff', '#d2d2d2', '#F6E5DF', '#EAF8D5'] z += 1 print '<tr bgcolor="%s">' % bg[z % 4] cursor.execute('select * from %s where ID=%s;' % (store, str(d))) col_fields = cursor.fetchall() col_fields = col_fields[0] tmp = [] for field in col_fields: tmp.append(field) col_fields = [] for field in tmp: col_fields.append(field) i = 0 x = 0 y = 0 w = 0 for field in col_fields: if colNames[x] == 'SKU': sku = field if colNames[x][:3] == 'pic': y += 1 w += 1 print '<td><input type="hidden" name="%s" />\n' % str(x) # im = Image.open(getpic) # width, height = im.size() # for infile in sys.argv[1:]: # outfile = os.path.splitext(infile)[0] + "_thumb.jpg" # if infile != outfile: # try: # im = Image.open(infile) # im.thumbnail((128, (width/128)*height), Image.ANTIALIAS) # im.save(outfile, "JPEG") # except IOError: # print "cannot create thumbnail for ", infile # print '<img src="%s"><br /><br /></td>\n' % (outfile) print '<img src="getpic.py?store=%s&pic=% d&id=%s" width="100"></td>\n' % (store, w-1, ids[0][0]) count2 += 1 else: if x == 0: # This is the ID field d = id[0] check = 'check' + str(d) i += 1 print '<td><input type="checkbox" name="', check, '" value="', field, '" /></td>\n' print '<td>%s</td>\n' % (field) if personalDataTables != []: i = 0 for relField in relationshipFields: if i != 0: # The first value is the same as d sql = 'select FirstName, LastName from %sPersonalData where ID="%s";' % (relationshipsDescription[i][0][:-2].lower(), relField) cursor.execute(sql) names = cursor.fetchone() print '<td>%s %s</td>\n' % (names[0], names[1]) i += 1 # print 'XXX', types[x] elif types[x][0:3] == 'set': for f in field: print '<td>AAA%s</td>\n' % (field) else: print 'YYY' else: if (isinstance(field, str)) and (len(field) > 60): print '<td>%s</td>\n' % (field[:60] + '...') else: print '<td>%s</td>\n' % (field) x += 1 print '</tr><tr>\n' print '<td align=center colspan=', len(colNames) + 1, '>\n' print '</td></tr></table>\n' print '<input type="hidden" name="count" value="%s" />\n' % count2 print '<input type="submit" value=" Send " />\n' except: print 'There is no data yet. Please add a product.\n<br /><br />' print "<input type='hidden' name='whatDo' value='insert' />\n" print '<input type="submit" value=" Add " />\n' else: print 'There is no data yet. Please add a product.\n<br /><br />' print "<input type='hidden' name='whatDo' value='insert' />\n" print '<input type="submit" value=" Add " />\n' print ''' </form> </body> </html> ''' cursor.close() enterProducts2() TIA, beno
-- http://mail.python.org/mailman/listinfo/python-list