[Zope] Pluggable brains?

Alexander Staubo alex@mop.no
Tue, 6 Jul 1999 02:11:14 +0200


Unfortunately, I require a very specific sorting algorithm (I'm sorting
threaded discussion topics). I solved this easily by implementing an
External Method that essentially copied the list of records into its own
list, did the processing, and set the attributes.

One curious artifact I noticed was that setattr() on the "r" class
instance does _not_ add attributes -- or at least not Zope-accessible
attributes -- to the instance. Maybe the instance is an acquisition
wrapped and I need to use aq_base? Anyway, I solved this by modfying my
SQL Method query, thus:

  select [...], ThreadLevel = 0 from [...]

Having done this, things like setattr(record, 'ThreadLevel', x) now
works.

--
Alexander Staubo             http://www.mop.no/~alex/
"What the hell, he thought, you're only young once, and threw
himself out of the window. That would at least keep the element of
surprise on his side."
--Douglas Adams, _The Hitchhiker's Guide to the Galaxy_

>-----Original Message-----
>From: Martijn Pieters [mailto:mj@antraciet.nl]
>Sent: 6. juli 1999 01:03
>To: Alexander Staubo; Zope Mailing List (E-mail); 'Phillip J. Eby'
>Subject: RE: [Zope] Pluggable brains?
>
>
>At 02:28 05/07/99 , Alexander Staubo wrote:
>>Uh, so "pluggable brains" _is_ the ability for Zope to return
>records as
>>instances of a user-definable class?
>>
>>It may be a vivid term, but descriptive it ain't. :-)
>>
>>I have a related question. I need to post-process a query result set
>>using Python; I simply want to (a) go through each record and add (or
>>set) a column, and (b) reorder the records. Can you give me a simple
>>example of how to do this? As far as I can see, records
>returned by SQL
>>Methods are instances of a special class "r" which descends
>from Record,
>>Acquisition.Implicit and an optional user-defined class (the
>"brain", I
>>guess). It seems that when such an instance is created, each column is
>>set as an attribute on the instance. If so, I guess I can
>simply put the
>>records into a list, add my column as a vanilla Python attribute, and
>>reorder them as I wish, and then return them for use in DTML? Correct?
>
>Indeed, just create the 'Brain' (Igor, is that lightning storm
>here yet?),
>and have it set the attribute. The reordering is trickier. You
>could use
>the sort attribute of the #in tag. If the order cannot be
>derived from any
>existing attributes, you just add one to the brain object
>(Igor, tweezers
>please), that, if sorted on, will create the right order.
>
>Just put a module in the Extensions directory of your Zope
>installation,
>let call it MyBrains.py. In it you define your class:
>
>class SmartRecord:
>     static_attr = 'A Static Attribute'
>
>     def dyn_attr:
>         return 'A Dynamic Attribute'
>
>And on the 'Advanced' tab of your Z SQL Method you specify
>this class and
>the file it is in ('SmartRecord' and 'MyBrains', or
>'MyBrains.py'). From
>now on, you can refer to static_attr and dyn_attr as if they
>were columns
>of the result set.
>
>PS. Above code is untested, YMMV
>
>--
>Martijn Pieters, Web Developer
>| Antraciet http://www.antraciet.nl
>| Tel: +31-35-7502100 Fax: +31-35-7502111
>| mailto:mj@antraciet.nl http://www.antraciet.nl/~mj
>| PGP:
>http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A3
2149
------------------------------------------