[Zope] overwrite __getattr__
Dragos Chirila
d.chirila@bucarest.finsiel.ro
Sat, 5 Apr 2003 16:20:21 +0300
HI
You ask : "Why don't you just use normal properties/class attributes?"
Because, these properties are "dynamic" . I can define any number of
properties, for each property i also add an index in Catalog object.
I use that dictionary to keep these "dynamic properties", pairs ('id',
'value').
When the catalog asks for 'field1' property it doesn't find it because it is
not a REAL property. So I want to be able to tell the Catalog: "ok, you can
take this property from my dictionary", I mean -
self.dictionary['fiedl1'].
Anyway, I found another approach: I'm using 'setattr' and 'delattr' to
add/delete properties at runtime, and it works
Dragos
----- Original Message -----
From: "Oliver Bleutgen" <myzope@gmx.net>
To: <zope@zope.org>
Sent: Saturday, April 05, 2003 4:27 PM
Subject: Re: [Zope] overwrite __getattr__
> Dragos Chirila wrote:
> > I have a class that extends 'Folder' and in this class i have a property
> > of type dictionary.
> > This dictionary has the following meaning: key = property id, value = a
> > value (string or whatever).
> > I want to use the catalog to index and search for this kind of objects.
> > For each property I have an index in catalog.
> >
> > Let's say the dictionary is {'field1' : 'value of field 1', 'field2':
> > 'value of field2'}.
> > I also have 2 indexes named 'field1' and 'field2'.
> >
> > The problem is that when the catalog tries to catalog my object doesn't
> > find the properties 'field1' and 'field2' for my object !!!
> > So, I overwrite __getattr__ to tell it how to handle these properties
> >
> > def __getattr__(self, name):
> > if name=='field1':
> > return self.dict['field1']
> > elif name=='field2':
> > return self.dict['field2']
> > else:
> > -- How do I tell him to execute the default ?????
> >
> > I want to handle __getattr__ just for some special attributtes names
>
> Just a general question, you seem not to use arbitrary keys for your
> dict, since you have to know them beforehand to be able to index them.
> Why don't you just use normal properties/class attributes?
>
> What I did, when I had to store an arbitrary dict-like structure into
> the zcatalog, was using two attributes of type tuple. Then I wrote
> getter/setter methods which masked the real structure appropriately.
>
> There may be better ways to do things like that.
>
> HTH,
> oliver
>
>
>
>
>
>
>
>
> _______________________________________________
> Zope maillist - Zope@zope.org
> http://mail.zope.org/mailman/listinfo/zope
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://mail.zope.org/mailman/listinfo/zope-announce
> http://mail.zope.org/mailman/listinfo/zope-dev )
>