[Zope] Pluggable brains sample using Script (Python)

J Cameron Cooper jccooper@jcameroncooper.com
Mon, 14 Apr 2003 14:24:34 -0500


> I was wondering if somebody has a handy example on how to use 
> pluggable brains inside python scripts. For the most part I've only 
> seen the sample in which you have a class:
>  
> class MyDataClass:
>     def getSomething(self):
>        return self.field1 + self.field2
>  
> and then you have a DTML method that goes something like
>  
> <dtml-in myquery>
>   <dtml-var getSomething>
> </dtml-in>
>  
> How would I access the MyDataClass if I were to write a python script 
> rather than a DTML method?

Untested, but it should look very similar to this:

for elt in context.myquery(parameter1="dog", parameter2="goose"):
  print elt.getSomething()

The parameters are those of the ZSQL Method, if any. The example above 
assumes two dummy parameters. Depending on your architecture, 'context' 
might be 'container' or another variable.

It might also be written like

for elt in context.myquery(REQUEST=REQUEST):
  print elt.getSomething()

if you're getting your parameters from the HTTP request.

             --jcc