Victor Subervi wrote:
> On Tue, Dec 29, 2009 at 10:16 AM, Steve Holden <st...@holdenweb.com
> <mailto:st...@holdenweb.com>> wrote:
> 
>     Victor Subervi wrote:
>     > On Tue, Dec 29, 2009 at 9:33 AM, Steve Holden <st...@holdenweb.com
>     <mailto:st...@holdenweb.com>
>     > <mailto:st...@holdenweb.com <mailto:st...@holdenweb.com>>> wrote:
>     >
>     >     That's a bit like being told you have to produce something
>     green, and
>     >     then when you do, being told "no, not that green, a light
>     green". So you
>     >     produce something a lighter gree and then being told "no, a
>     slightly
>     >     redder green". And so on.
>     >
>     >     In other words, you aren't giving us the whole picture here,
>     and without
>     >     the whole picture you will only ever get bits of the answer.
>     >
>     >
>     > Later on I give you "the whole picture"...which you summarily toss in
>     > the garbage can. What am I supposed to do with that??
>     >
>     That isn't "the whole picture", that's "the Python soup I produced to
>     query the SQL soup I produced because I thought it might answer my
>     problem".
> 
>     >
>     >
>     >     > #              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:
>     >     >
>     >     What happens if you *don't* uncomment the commented line?
>     >
>     >
>     > As I mentioned, it prints BOTH the AAA and the YYY lines! The AAA is
>     > printed with something like this:
>     > AAASet([purple:223344])
>     >
>     Try moving the "else" so it's associated with the "if ... elif" rather
>     than the "for". It's currently indented one level too far.
> 
> 
> There is currently an else where you indicate, as one can see in the
> complete code I am once again supplying with this post. That prints out
> pretty much all the other fields and does so just fine.
> 
See, this is another indication of the problem. The code snippet quoted
above *clearly* has the "else" matching the "for" and not the "if" above
it. This is different from the code you quote below.

> Once again, it seems nobody is bothering to address this issue. Is it
> beyond your reach as it is beyond mine? I feel compelled to restate the
> problem once again as I did in my original post, and to once again post
> the entire code. Hopefully someone will help me see my error.
> 
> 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:
>                   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)
> 
> Notice that I can slice to determine if it's a set (I've printed
> something after that call to be sure). But once I try to loop through
> the set it quits printing to screen. The entire code follows.
> 
You *say* you can "slice to determine if it's a set", but sets aren't
sliceable, just like they aren't indexable, because no position is
associated with an element of a set (they are like dicts in that respect):

>>> from sets import Set
>>> s = Set("abc")
>>> s
Set(['a', 'c', 'b'])
>>> s[1:2]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
>>>

So whatever it is you think you are looping over, it certainly isn't a set.

It's difficult to help someone whose thinking is so imprecise.

You say your program "stops printing to the screen", but you aren't
printing to the screen in the first place - you are generating output
that a web server is sending across the network, and that output is
being interpreted by a web browser. This has certain implications, whihc
you appear determined to ignore.

> #! /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
>               elif types[x][0:3] == 'set':
>                 for f in field:
>                   print '<td>%s</td>\n' % (field)
>                 else:
>                   print 'YYY'

If the above "for" statement (and its associated else) prints nothing
then the clear conclusion - indeed the only possible conclusion ofr a
rational individual to draw - is that "field", whatever type of object
it is (and why, by the way, haven't you just inserted a

    print type(field), field

statement immediately before the "for" loop to give you the information
you need) IT IS EMPTY. If it's a string, it's an empty string. If it's a
set, it's an empty set. If it's a list, it's an empty list.

There is simply NO other way that

                for f in field:
                  print '<td>%s</td>\n' % (field)
                else:
                   print 'YYY'

can produce no output. I'd focus on understanding that, if I were you.
The rest is (for the moment) irrelevant.



>               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
> 
As for the rest, without any knowledge of the structure of your database
it's difficult to know how to advise you.  I am indeed impressed that
you discovered it's possible to have MySQL perform

  SHOW TABLES LIKE '%PersonalData'

but at the same time it suggests that you are building a database which
has lots of different tables with the same structure. This is usually a
bad sign - if the tables have the same structure then why not just put
all the data in a single table, with an additional field to
differentiate between the different occurrences.

If the tables *don't* have the same structure then you may have no
option, but then you need to understand very clearly how each type of
value is represented in your Python program.

This is why I suggested that your efforts would be better spent focusing
on fundamentals. As it is you keep promising to revisit the fundamentals
"as soon as I get this shopping cart up", like a man who can't afford to
spend the time to save himself more time. If you don't pay attention to
the fundamentals then you'll never get the shopping cart up, because you
will never understand enough about what you are trying to do.

I realise this must be deeply frustrating to someone as in tune with the
fundamental nature of the universe as you claim to be, but as Euclid
pointed out to Ptolemy about geometry, "there is no royal road to
[Python] programming". You have to pay attention to lots of detail in
order to understand how to build complex systems that work under a wide
range of circumstances.

If there were a God, she'd be a programmer.

regards
 Steve

PS: Let's just have this discussion through the newsgroup from now on.
That way when I don't have time to help people I'm not distracted by
random emails that I won't be replying to.
-- 
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/
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to