Custom Classes?

J. Clifford Dyer jcd at unc.edu
Fri May 2 08:01:19 EDT 2008


On Thu, 2008-05-01 at 17:31 -0500, Victor Subervi wrote:
> On Wed, Apr 30, 2008 at 12:35 PM, J. Cliff Dyer <jcd at unc.edu> wrote:
>         Post working code, and I'll answer your actual question.
> 
> Good grief!  The code is *not* double spaced! Take a look. Click to
> the end of the first line and hit the right arrow key, and see for
> yourself.  As for not initializing w, well, I did in my code and just
> forgot to cut and paste that. Same with the except. Sorry on those.
> And yes, I pull out try clauses when I need to look at stacks. Here:

Taking a closer look--it's still double spaced, but I'm sure that's just
something funky in how it's getting rendered along the way.  

That said, your code still doesn't work.  After removing your try
statements, and the corresponding except-pass blocks, I get the
following problems:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    os.remove(getpic)
NameError: name 'os' is not defined

and when I fix that:

Traceback (most recent call last):
  File "test.py", line 34, in <module>
    print '<td><input type="hidden" name="%s"' % str(x), ' value="%s">'
% pic

Please test your code snippet before you post it.  Testing the program
you extracted it from is not sufficient.

On line 34 (or thereabouts--I had to add a line for import os, and
stripped out all blank lines, since my email client is still rendering
it double spaced), I think you wanted "x" instead of str(x).  At least,
that's what your generated script wants.  str(x) takes the value of the
variable x (not yet defined), and converts it to a str.  It does not
give you a str with a value of "x".  

As for the generated script, why are you creating that on the fly every
time?  Why not just create it as an independent python file?  When you
want an image displayed in another HTML file, static or generated, just
point your src to that python file with the appropriate arguments.

<img src='http://example.com/path/to/getpic.py?x=4'/>

Also, remove the line that says "print 'Content-Type: text/html'."  You
only need one content type, and it should be image/jpeg, or whatever
type your images are.  If you are going to have a few pngs in there as
well, for example, you'll need some mechanism for determining what type
of image the file actually is, and outputting the appropriate header
from:

Content-Type: image/jpeg
Content-Type: image/png
Content-Type: image/gif

This could be by checking the file extension, probing the file itself,
or by including the mime-type in your pics dictionary like so:

pics = {1: ('pic1', 'image/jpeg'), 2: ('pict_num_two', 'image/png')} 


Then, of course, you'll have to revise how you retrieve the information.

That should be enough to get you moving in the right direction.

Cheers,
Cliff
> 
> 
> w = 0
> 
> 
> try:
> 
>   w += 1
> 
>   getpic = "getpic" + str(w) + ".py"
> 
>   try:
> 
>     os.remove(getpic)
> 
>   except:
> 
>     pass
> 
>   code = """
> 
> #!/usr/local/bin/python
> 
> import cgitb; cgitb.enable()
> 
> import MySQLdb
> 
> import cgi
> 
> import sys,os
> 
> sys.path.append(os.getcwd())
> 
> from login import login
> 
> user, passwd, db, host = login()
> 
> form = cgi.FieldStorage()
> 
> picid = int(form["id"].value)
> 
> x = int(form["x"].value)
> 
> pics =
> {1:'pic1',2:'pic1_thumb',3:'pic2',4:'pic2_thumb',5:'pic3',6:'pic3_thumb',7:'pic4',8:'pic4_thumb',\
> 
> 9:'pic5',10:'pic5_thumb',11:'pic6',12:'pic6_thumb'}
> 
> pic = pics[x]
> 
> print 'Content-Type: text/html'
> 
> db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
> 
> cursor= db.cursor()
> 
> sql = "select " + pic + " from products where id='" + str(picid) +
> "';"
> 
> cursor.execute(sql)
> 
> content = cursor.fetchall()[0][0].tostring()
> 
> cursor.close()
> 
> print 'Content-Type: image/jpeg'
> 
> print
> 
> print content
> 
> """
> 
>   script = open(getpic, "w")
> 
>   script.write(code)
> 
>   print '<td><input type="hidden" name="%s"' % str(x), ' value="%s">'
> % pic
> 
>   print '<img src="%s?id=%d&x=%d"><br /><br /></td>\n' % (getpic, d,
> y)
> 
> except:
> 
>   pass
> 
> 
> TIA,
> Victor
> 
-- 
Oook!
J. Cliff Dyer
Carolina Digital Library and Archives
UNC Chapel Hill




More information about the Python-list mailing list