[Zope] Passing data to/from Python

Chris McDonough chrism@digicool.com
Sat, 25 Mar 2000 23:02:37 -0500


Mark Wexler wrote:
> 
> Dear Zopistas,
> 
> I am in the process of learning about Zope, and trying to
> determine whether to make the switch from pure Python CGI
> scripts. If someone could answer the following questions (and I
> apologize, as it will probably seem naive), it would really help.
> 
> Let's suppose I have a page where the user queries a data base,
> for instance a telephone directory. The telephone directory is in
> an Excel file. At present, I have a CGI script that opens an
> ODBC.Windows connection, executes an SQL query, and outputs the
> results (names, phone numbers) as an HTML table using "print".
> 
> What is the best way to do this in Zope? Here are the points that
> I don't get:
> 
> 1. It seems that the only way to establish a DB connection is
> through Gadfly. There are warnings everywhere that Gadfly is
> inefficient. Therefore, I want to use ODBC (ODBC.Windows), like I
> did before. How?

There's an ODBC database adapter for Windows machines.  It does not work
under UNIX.  The Sybase DA works against MS SQL Server 6.5 and below
from UNIX.

Look under "Downloads" on Zope.org.

Also read the ZSQL User Guide.

> 2. I might eventually need to do complex calculations on the
> retrieved data, so I would prefer using Python code in an
> external method, rather than a DTML method. In order to get Zope
> to accept the line "import ODBC.Windows", I had to copy the ODBC
> and DateTime directories from my regular Python installation
> (PYTHON/lib) to ZOPE/bin/lib. But then, when I try running the
> script, I get one of the following problems:
> 
>    a. If I go through authentification, the script runs, but I
>    cannot establish the connection to the database.
> 
>    b. If I type in the URL of my search page directly, without
>    authentification, I get a Zope error: "initialization of
>    module mxODBC failed
>    (exceptions.AttributeError:mxDateTimeAPI)".
> 
> Is my ODBC.Windows installation wrong, or do I have a more
> fundamental problem?

Hmm.  I've never seen ODBC.Windows.  We've punted on supporting ODBC
under UNIX, and we're now supposed to be contracting out to some other
folks to provide it for us.  Not sure when this will happen.  You can
try to write your own ODBC DA for UNIX/Linux.  There is a HowTo on
writing a DA someplace out there that Chris Petrilli wrote.
 
> 3. What if my Python function in my external method wants to
> return information to my DTML method? For instance, how do I
> return a Python list, and what do I do in my DTML method to
> process it (e.g., output a line for every member of the list)?

external method named "pyfunction":

def pyfunction(self):
   return ['1','2','3']

a dtml method that uses pyfunction:

<dtml-in pyfunction>
   <dtml-var sequence-item><br>
</dtml-in>

Should output:
1
2
3

> 
> 4. The way I call my Python function now is through the <form
> method="POST" action="search"> tag, where "search" is the
> external method ID. The form has a field called "name", and my
> Python function has an argument also called "name", and so it
> recuperates the string that was entered in the "name" field. Can
> I pass the "name" as an argument to a DTML method?

If you use an external method to handle the form input, you should be
able
to get at its attributes by looking in REQUEST ala:

def myformhandler(self, REQUEST):
    a = REQUEST['a']
    b = REQUEST['b']

where 'a' and 'b' are form field names.  This can also be done by:

def myformhandler(self):
    a = self.REQUEST['a']
    b = self.REQUEST['b']


> If these questions have answers, I would really appreciate them.
> Otherwise, if I'm completely off-base (for instance, if I'm
> thinking in Python terms and not in Zope terms), please point me
> in the right direction!
> 
> Thanks in advance.
> Mark Wexler
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )

-- 
Chris McDonough
Digital Creations
Publishers of Zope - http://www.zope.org