Another Sets Problem
Steve Holden
steve at holdenweb.com
Tue Dec 29 09:16:28 EST 2009
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.
>
> > [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] File
> > "/var/www/html/angrynates.com/cart/enterProducts2.py
> <http://angrynates.com/cart/enterProducts2.py>
> > <http://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
> >
> I am presuming that this is copied from an Apache error log, though you
> don't bother to say so.
>
>
> No. I just copied and pasted stuff from a previous post. I did that
> because I've noticed when someone posts something irrelevant, the
> substance of my posts sometimes gets buried. I should have edited this
> garbage out.
>
>
> The issue here is that you can't just stick print statements in anywhere
> you like. Consider the following Python:
>
> if a == 3:
> print "A is 3"
> print "Debugging"
> elif a == 4:
> print "A is 4"
> else:
> print "A is neither 3 nor 4"
>
> If you comment out the 'print "Debugging"' line you have a perfectly
> acceptable program. If you don't then you have something that is close
> to Python, but not close enough for the interpreter to be able to
> compile it.
>
>
> Duh. I might not be too sharp, but give me a little more credit than
> that, Steve. Come on.
>
So you didn't need any help to spot this problem?
>
> What's happening with your (CGI?) program is that the syntax error you
> have induced by inserting a random print statement causes an error
> message to be produced instead of your program's output; this doesn't
> look like HTTP headers. So the web server complains that your program
> has produced output that shouldn't be sent to any self-respecting
> browser.
>
>
> Perhaps.
>
>
> I further presume (though again I don't believe you bother to report it
> anywhere) that you see an "Error 500" or similar message in your web
> browser when you try and access the web page.
>
>
> Yes. I believe that's it.
>
>
> > 2) AAA prints out before the following fields:
> > AAASet(['Extra-small'])
> > AAASet(['purple:50404D'])
>
>
> And the above is the answer to your previous question. This makes me
> think you really rushed through this. You are a very helpful person, but
> please either take the time necessary or don't answer. That is, respond
> only when you have the time to devote to giving a good and thoughtful
> answer. It's evident you didn't do that here...and you're blaming me for
> it. Wrong!
>
OK, I'll not answer.
regards
Steve
>
> >
> > 3) YYY *also* prints out twice!!
> >
> > Here's the complete code once again:
> >
> [complete code snipped]
> >
> > TIA,
> > beno
> >
> Sorry, I don't intend to read all that code when the error you are
> reporting is in fact a clear demonstration that you need to further
> understand the principles of both Python and the web before you continue
> on your journey.
>
>
> Again, I know I'm really slow when it comes to this material. But I
> didn't start hacking (and it is hacking) Python code yesterday. I'm
> almost ashamed to admit it, but I've been at it 10 years. Again, I might
> be slow, but I don't think that gives you the right to tell me to go
> back to kindergarten. Help me out here. Or maybe somebody else will.
>
> Let me restate the problem:
>
> 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.
>
> Here's the full code again:
>
> #! /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:
> 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()
>
--
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