[Zope] How to call a python script within a Z-SQL method?
Terry Hancock
hancock at anansispaceworks.com
Fri Nov 28 14:22:16 EST 2003
On Thursday 27 November 2003 09:12 pm, Julian Clark wrote:
> Using a sql query called from within a python script, would you write the
> query within the script? or would you call a seperate sql method from the
> script?
The latter, definitely. You just want to call the ZSQL method (which is
just like any other function, really. The only thing that is weird is that
ZSQL methods only take keyword arguments).
The object you get back is a "results" object. I can tell you that it
isn't really*, but it very much acts like a list of class instances where each
instance contains attributes corresponding to the keys in the returned
DB row. (And no methods -- you can add methods to them with
"Pluggable Brains", but that's an advanced topic).
Your 2nd question therefore doesn't arise.
I would recommend separately testing the ZSQL method object (use
the test tab that is part of ZSQL method objects' management
interface), before trying to implement a python script around it.
You'll also want to dump the results as text from the Python script
to show you what it looks like as a Python literal:
## In your script
results = myZSQLQuery(foo='spam', spam='foo', ...)
return str(results)
That'll help you figure out the python code you need to do the
job. You will likely find list comprehensions useful:
def isGoodRow(row):
# decide if we want this row
if good: return 1
else: return 0
def cookRow(row):
# do some stuff to the row data
return cooked_row
return [cookRow(r) for r in results if isGoodRow(r)]
or something like that.
Cheers,
Terry
*It's actually a specially implement instance with a sequence
interface, that simulates this behavior. Apparently for improved
performance with long query results.
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
More information about the Zope
mailing list