Hi 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 10x Dragos
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
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 )
On Sat, Apr 05, 2003 at 04:20:21PM +0300, Dragos Chirila wrote:
Anyway, I found another approach: I'm using 'setattr' and 'delattr' to add/delete properties at runtime, and it works
That's good to know. __getattr__ is already used extensively by acquisition and security machinery, and the one time i tried to wrap it, it was *not* fun. very mysterious and seemingly nonsensical errors. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's UBER TRONIC GIRL! (random hero from isometric.spaceninja.com)
Paul Winkler wrote at 2003-4-5 10:14 -0800:
On Sat, Apr 05, 2003 at 04:20:21PM +0300, Dragos Chirila wrote:
Anyway, I found another approach: I'm using 'setattr' and 'delattr' to add/delete properties at runtime, and it works
That's good to know. __getattr__ is already used extensively by acquisition and security machinery, and the one time i tried to wrap it, it was *not* fun. very mysterious and seemingly nonsensical errors.
It is not easy but workable. You find an example in my "References" product <http://www.dieter.handshake.de/pyprojects/zope> and description for what you must look for in the mailing list archives. Dieter
participants (4)
-
Dieter Maurer -
Dragos Chirila -
Oliver Bleutgen -
Paul Winkler