Another Sets Problem

Victor Subervi victorsubervi at gmail.com
Tue Dec 29 10:56:10 EST 2009


On Tue, Dec 29, 2009 at 10:16 AM, Steve Holden <steve at holdenweb.com> wrote:

> Victor Subervi wrote:
> > On Tue, Dec 29, 2009 at 9:33 AM, Steve Holden <steve at holdenweb.com
> > <mailto:steve at 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.

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.

#! /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'
              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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091229/61b5c06d/attachment-0001.html>


More information about the Python-list mailing list