[Zope] Pluggable Brains?

Evan Simpson evan@tokenexchange.com
Wed, 4 Aug 1999 16:55:03 -0500


Pluggable Brains are very cool, yet very simple (if you know Python, which
isn't hard).

You place a module in the Extensions folder of Zope, just as you would for
an External Method.  Instead of a function, you define a class in the
module.  On the Advanced tab of your ZSQL Method, you specify the names of
this module and class.  Presto!  All records returned by the ZSQL Method are
now instances of your class (sort of), and you can use any methods defined
for the class.

For example, with:

class MyClass:
  def tax(self, REQUEST):
    return self.subtotal * REQUEST.taxrate
  def shipping(self, REQUEST):
    return self.subtotal * REQUEST.shiprate
  def total(self):
    return self.subtotal + self.tax(REQUEST) + self.shipping(REQUEST)

you could do:

<!--#in SQL_lineitems-->
<p>Subtotal: <!--#var subtotal-->
<p>Tax: <!--#var tax-->
<p>Shipping: <!--#var shipping-->
<p>Total: <!--#var total-->
<!--#/in-->

For another example, I have a query which returns a list of items, people,
and quantities.  I want to make a list of all distinct items, all distinct
people, and the quantity for each distinct (item, person) pair from this
query, so I write:

class Shoplist:
  def accumulate(self, REQUEST):
    rvars = [[], [], {}]
    for i, rvar in ((0, 'supplylist'), (1, 'wholist'), (2, 'supplywho')):
      if REQUEST.other.has_key(rvar):
        rvars[i] = REQUEST.other[rvar]
      else:
        REQUEST.other[rvar] = rvars[i]
    supplylist, wholist, supplywho = rvars

    if not self.supply in supplylist: supplylist.append(self.supply)
    if not self.who in wholist: wholist.append(self.who)
    supplywho[(self.supply, self.who)] = self.qty

and then use:

<!--#in SQL_getpairs--> <!--#call accumulate--> <!--#/in-->
<p>Supplies: <!--#var supplylist-->
<p>People: <!--#var wholist-->
<p>qtymap: <!--#var supplywho-->

----- Original Message -----
From: <Tony.McDonald@newcastle.ac.uk>
To: <zope@zope.org>
Sent: Wednesday, August 04, 1999 4:17 PM
Subject: [Zope] Pluggable Brains?


> People,
> Can someone point me to some documentation, messages or source code that
will give me a handle on what 'pluggable brains' are and what I can do with
them?
>
> Rob Page has just made a post saying that Zope/ZODB3/pluggable brains can
go a long way to integrating with relational databases and I want to make
sure I've got a clue about all this! :)