[Zope] Re: Pluggable brains sample using Script (Python) (J Cameron Cooper)

Julian Sanchez julian.sanchez@ieee.org
Tue, 15 Apr 2003 21:15:35 -0500


Cameron,

Cool! I knew it was going to be easy. Thanks :-)

BTW, I noticed that once you get an instance of the object, you *can* read
the attributes (which of course correspond to the columns from the SQL
query), but it doesn't let me change the values unless I create "wrapper
methods" inside the class.

Is that a Zope security thing, bug, unimplemented feature... do you know?

Thanks,
Julian

> 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