Re: Custom Classes?





On Wed, 2008-04-30 at 11:02 -0500, Victor Subervi wrote:
Hi;
I have the following code which produces a file every time I need to
display an image from MySQL. What garbage! Surely, python is capable
of better than this, but the last time I asked for help on it, I got
no responses. Is this only possible with custom classes? Please, give
me some guidance here!
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)


TIA,
Victor

Victor,

Once again, you've posted code that doesn't work. Your outer try block
is never terminated, and w is never initialized (so will raise an
exception inside your try block, which you probably won't see, if you
catch all exceptions. Generally speaking, unless you know what you're
trying to catch, it's better to leave off the try/except block,
*especially* during development. Stack traces are full of good
information.

Second, you never initialize w, which causes the following problem.

$ python
Python 2.3.4 (#1, Nov 20 2007, 15:18:15)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
w += 1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'w' is not defined


Third, double-spacing your code makes it difficult to read. Take out
blank lines, unless they add contextual hints which improve readability.

Post working code, and I'll answer your actual question.

Cheers,
Cliff



--
Oook,
J. Cliff Dyer
Carolina Digital Library and Archives
UNC Chapel Hill

.



Relevant Pages

  • Re: decorators tutorials
    ... I am learning python by learning django, ... allow the SystemExit exception to continue to propogate without doing ... # The func argument is a Pythong function object ... trying to get the result of DivXY, ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... >>> wish sometimes that Python would make up it's mind about what it does ... >> exception on overflow, invalid operation, and divide by 0, and "should ... >> not", by default, raise an exception on underflow or inexact. ... you can at least be pretty sure that an infinite result is the ...
    (comp.lang.python)
  • Re: exceptions
    ... How do younger languages like IO make this point more forcefully than lisp, ... deal with *all* errors in an interactive session, ... certainly in python ... > exception handling is hard-coded in syntax. ...
    (comp.lang.python)
  • Re: thread, threading; how to kill a thread?
    ... Python is embedded into C++. ... I was able to kill the threads ... nThreadId is the ID of the thread that you want to terminate (i.e. ... Calling this function will cause an exception to ...
    (comp.lang.python)
  • Re: exceptions
    ... Io was based on experience from lots of other languages. ... One typical example mentioned in AOP is precisely exception handling. ... > certainly in python ... just too bad that the end user cannot override internal C/assembler ...
    (comp.lang.python)