Re: [Zope-dev] Can't return / publish object of a dictionary?
Thanks for the tips, it was the missing wrapper. I'm quite a newbie in Python programming, so please forgive me. I've got two other questions: 1. Say we have a Class A with an attribute myObjects which is just a list. Now, I'd like to write my own Class B(A) - it extends A. But, in Class B, myObjects has to be a method because it has to be rebuild everytime it is called. So, how can I do something like: myObjects = getMyObjects() ? I tried to do this in the class itself, didn't work. Also tried self.myObjects = self.getMyObjects() - result wasn't what I wanted, myObjects got (of course) the resulting list of getMyObjects(), but I need myObjects to be a reference to getMyObjects() How to do this? 2. Class A has another attribute, say data, which I want to control in Class B(A), i. e. everytime data is accessed (reading or writing), I want to intercept those actions. If it was just reading, I could use the strategy from 1., but I also want to control made changes to that attribute. Do I absolutely have to rewrite all the methods which access the wanted attribute, or is there another possibility with some kind of references, perhaps similar to software interrupts in DOS? Thanks for your patience! Lars -- Lars Heber T-Systems GEI GmbH Hausanschrift: Clausstrasse 3, 09126 Chemnitz Postanschrift: Clausstrasse 3, 09126 Chemnitz Telefon : (+49 371) 5359-271 Fax : (+49 371) 5359-133 E-Mail : Lars.Heber@t-systems.com Internet: http://www.t-systems.de
Lars Heber wrote:
I've got two other questions:
1. Say we have a Class A with an attribute myObjects which is just a list. Now, I'd like to write my own Class B(A) - it extends A. But, in Class B, myObjects has to be a method because it has to be rebuild everytime it is called. So, how can I do something like: myObjects = getMyObjects() ? I tried to do this in the class itself, didn't work. Also tried self.myObjects = self.getMyObjects() - result wasn't what I wanted, myObjects got (of course) the resulting list of getMyObjects(), but I need myObjects to be a reference to getMyObjects()
I use a form of the following to compute dynamic titles for Zope objects, where the 'title' attribute is the result of a method call. Modifying it slightly for your case... from ComputedAttribute import ComputedAttribute Class B(A): def getMyObjects(self): ... myObjects = ComputedAttribute(lambda self: self.getMyObjects())
2. Class A has another attribute, say data, which I want to control in Class B(A), i. e. everytime data is accessed (reading or writing), I want to intercept those actions. If it was just reading, I could use the strategy from 1., but I also want to control made changes to that attribute. Do I absolutely have to rewrite all the methods which access the wanted attribute, or is there another possibility with some kind of references, perhaps similar to software interrupts in DOS?
I'm not sure about this one, but I suspect an override of the __getattr__() method will let you intercept the lookup of the 'data' attribute, do any pre/post actions you wish, then return the real 'data'. You might try a ComputedAttribute arrangement instead and see if it works for your case since that would be easier. __getattr__ overrides can get into infinite loops w/o careful designs. Jeff Rush
Thousand thanks for ComputedAttribute! You saved not only my day, but my whole week!!! -- Lars Heber T-Systems GEI GmbH Hausanschrift: Clausstrasse 3, 09126 Chemnitz Postanschrift: Clausstrasse 3, 09126 Chemnitz Telefon : (+49 371) 5359-271 Fax : (+49 371) 5359-133 E-Mail : Lars.Heber@t-systems.com Internet: http://www.t-systems.de
Jeff Rush wrote:
I use a form of the following to compute dynamic titles for Zope objects, where the 'title' attribute is the result of a method call. Modifying it slightly for your case...
from ComputedAttribute import ComputedAttribute
Class B(A): def getMyObjects(self): ... myObjects = ComputedAttribute(lambda self: self.getMyObjects())
In Python 2.2 (which is unfortunately not yet an option unless you are using a bleeding edge Zope) you can also use the new property type: http://www.python.org/doc/2.2.1/whatsnew/sect-rellinks.html#SECTION000340000... seb
On Sat, 2003-01-18 at 17:44, Seb Bacon wrote:
Jeff Rush wrote:
I use a form of the following to compute dynamic titles for Zope objects, where the 'title' attribute is the result of a method call. Modifying it slightly for your case...
from ComputedAttribute import ComputedAttribute
Class B(A): def getMyObjects(self): ... myObjects = ComputedAttribute(lambda self: self.getMyObjects())
In Python 2.2 (which is unfortunately not yet an option unless you are using a bleeding edge Zope) you can also use the new property type:
I might be wrong but I believe class properties only work with new-style classes, and I don't know if Zope ExtensionClass-based objects qualify... Cheers, Leo
Yeah, the new property type in Python 2.2 is pretty cool, but out of range for use under Zope at the moment. The ExtensionClass requirement for persistence eliminates using it with new style Python classes. Plus... the Lars, the original poster, said he was a Python newbie and he certainly doesn't need to get lost wandering around in bleeding edge versions. I think Zope 2.7 or 3.0 is supposed to support new style classes and persistence. But not today... -Jeff Leonardo Rochael Almeida wrote:
On Sat, 2003-01-18 at 17:44, Seb Bacon wrote:
Jeff Rush wrote:
I use a form of the following to compute dynamic titles for Zope objects, where the 'title' attribute is the result of a method call. Modifying it slightly for your case...
from ComputedAttribute import ComputedAttribute
Class B(A): def getMyObjects(self): ... myObjects = ComputedAttribute(lambda self: self.getMyObjects())
In Python 2.2 (which is unfortunately not yet an option unless you are using a bleeding edge Zope) you can also use the new property type:
I might be wrong but I believe class properties only work with new-style classes, and I don't know if Zope ExtensionClass-based objects qualify...
Cheers, Leo
Jeff Rush wrote:
Yeah, the new property type in Python 2.2 is pretty cool, but out of range for use under Zope at the moment. The ExtensionClass requirement for persistence eliminates using it with new style Python classes.
Why? I can't see why ExtensionClass should not work with the new property type. Or is the problem specifically to do with persisting a property? In any case, it is certainly out of range for Zope since Zope does not yet officially support python 2.2 - I just mentioned it out of interest. Cheers, seb
participants (4)
-
Jeff Rush -
Lars Heber -
Leonardo Rochael Almeida -
Seb Bacon