473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Jun 27 '08 #1
0 1320

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Neil Zanella | last post by:
Hello, In Python, classes are objects. But there is no way to custom print a class object. This would require some syntax such as the one commented out below: With the current "foo =...
7
by: Adam | last post by:
Im trying to add an httphandler for all *.sgf file extensions. I have developed the handler, 1. installed it into the gac 2. added it to the machine.config: <httpHandlers> <add verb="*"...
5
by: mtv | last post by:
Hi all, I have the following code: ================================ Webservice side: public class MyWS: WebService { private myLib.DataObject curDataObject;
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
8
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
27
by: Keith Wilby | last post by:
I've been trying to get my head around custom classes by following the example in the Visual Basic Language Developer's Handbook by Sybex. I think I have a handle on what they're about, albeit a...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.