[Zope] overload __getattr__ in brains-class?
Jo Meder
jo@meder.de
Fri, 19 Jul 2002 01:29:31 +0200
Am 18.07.2002, 21:54 Uhr
schrub Dieter Maurer <dieter@handshake.de>:
> > objects=Results.Results(result,brain,parent,None)
> >
> > Now whatever I try, either some other __getattr__ seems to be called
> > since I can get none of my "simulated" attributes to show up.
> It should work with modern Zope versions.
>
> I once tried and failed with an older version (Zope 2.1.6).
> My "__getattr__" led to an infinite loop.
I'm using 2.5.1 with python 2.1.3. AFAIK this is fairly recent.
So either I'm making a some tragically stupid mistake or something
really strange is going on... Let me elaborate a bit (sorry for what
follows, it's rather longish):
The standard procedure for simulating class attributes in Python I
normally use is (set discussions of efficiency aside for a moment):
class a:
def __init__(self):
self.realattrib_a="this is real"
def __getattr__(self,key):
if key in self.__dict__.keys():
return self.__dict__[key]
else:
return "simulated attribute"
where "realattrib_a" is the only genuine attribute of class a and
everything else should have a value of "simulated attribute"
This - of course - works perfectly when called with Python from the
command line.
Now I take this definition and use it as my brain-class. In my parent
class (which is a CMF class derived from PortalContent and
DefaultDublinCoreImpl and essentially represents an arbitrary list of
results of a RDBMS-Query) I do the following:
def calculate_results(self,selectionparams):
<some logic to construct a query string from params>
result=query_the_database(query_string)
objects=Results.Results(result,a,self,None)
return objects
Next I have a DTML-Method that essentially contains:
<dtml-let result="calculate_results(selectionparams)">
<dtml-if expr="result">
<dtml-with expr="result[0]">
<dtml-var expr="realattrib_a">
<dtml-var expr="doesntexist_b">
</dtml-with>
</dtml-if>
</dtml-let>
And to my astonishment this already barfs on
'<dtml-with expr="result[0]">' with a traceback
telling me that the __init__ of class a is flawed since there is no
attribute "realattrib_a".
??? How can an __init__-method complain about nonexisting attributes
when all I'm trying to do is define some? Ok, I tell myself, if Zope
wants me to live without sensible __init__-methods on brain-classes, I
can cope with that. I completely remove the __init__-method and the next
traceback I get is:
File /usr/lib/zope/lib/python/DocumentTemplate/DT_With.py, line 76, in render (Object: result[0])
File /usr/lib/zope/lib/python/DocumentTemplate/DT_Util.py, line 169, in eval (Object: realattrib_a)
(Info: realattrib_a)
File <string>, line 2, in f NameError: global name 'realattrib_a' is not defined
I really don't know what to try next. I know there has to be a really
simple solution to this problem, since it is such a simple thing to come
up with. But I have to admit, I'm completely lost. No ideas left to try,
no hope, no sunshine...
Perhaps I just didn't follow standard procedures for this situation, but
then I'm guilty of not having tried the appropriate search term with
zope.org or google. I'm stumped.
Jo.
--
Internetmanufaktur Jo Meder ---------------------- Berlin, Germany
http://www.meder.de/ ------------------- fon: ++49-30-417 17 63 33
Kollwitzstr. 75 ------------------------ fax: ++49-30-417 17 63 45
10435 Berlin --------------------------- mob: ++49-170- 2 98 89 97
Public GnuPG-Key ---------- http://www.meder.de/keys/jo-pubkey.txt