Re: [Zope-dev] Acquisition wishlist :-)
Chris Withers writes:
Hmmm, I guess I like the way it is but my wishlist (damn, Christmas just gone ;-) would be:
for Acquisiton.Implicit, be able to do something like:
class MyClass (Acquisition.Implicit):
acquisition = ClassAcquisitionInfo()
acquisition.donotacquire('index_html') This would be great.
and, likewise, for Acquisition.Explicit, to be able to to something like:
class MyClass (Acquisition.Explicit):
acquisition = ClassAcquisitionInfo()
acquisition.acquire('index_html') acquisition.acquire('fred') You already can do that, though with a different syntax (I would need to search for in the documentation).
Dieter
Dieter Maurer wrote:
acquisition.donotacquire('index_html')
This would be great.
Indeed :-)
class MyClass (Acquisition.Explicit):
acquisition = ClassAcquisitionInfo()
acquisition.acquire('index_html') acquisition.acquire('fred') You already can do that, though with a different syntax (I would need to search for in the documentation).
You may mean that if x is an Acquisition.Explicit object, you can do: x.aq_acquire('your_attribute') (syntax may be wrong ;-) What I meant is that through a declaration in the class you could saying acquire the 'your_attribute' attribute but nothing else. So, you could still do: x.your_attribute ...which would be acquired, but... x.index_html ...which wouldn't be acquired. cheers, Chris
Dieter
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
On Thu, Jan 04, 2001 at 10:46:35AM +0000, Chris Withers wrote:
Dieter Maurer wrote:
acquisition.donotacquire('index_html')
This would be great.
Indeed :-)
class MyClass (Acquisition.Explicit):
acquisition = ClassAcquisitionInfo()
acquisition.acquire('index_html') acquisition.acquire('fred') You already can do that, though with a different syntax (I would need to search for in the documentation).
You may mean that if x is an Acquisition.Explicit object, you can do:
x.aq_acquire('your_attribute') (syntax may be wrong ;-)
What I meant is that through a declaration in the class you could saying acquire the 'your_attribute' attribute but nothing else. So, you could still do:
x.your_attribute ...which would be acquired, but... x.index_html ...which wouldn't be acquired.
You could use ComputedAttribute for that: class MyClass(Acquisition.Explicit): # The following attribute is acquired transparently def _acquired_your_attribute(self): return self.aq_acquire('your_attribute') your_attribute = ComputedAttribute(_acquired_your_attribute, 1) # index_html isn't index_html = None Or you could define a __getattr__ that does a lookup in a list for explicetly acquired attributes: _acquired = ('index_html', 'fred') def __getitem__(self, key): if key in self._acquired: return self.aq_acquire(key) raise AttributeError, name -- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Martijn Pieters wrote:
You could use ComputedAttribute for that:
class MyClass(Acquisition.Explicit): # The following attribute is acquired transparently def _acquired_your_attribute(self): return self.aq_acquire('your_attribute') your_attribute = ComputedAttribute(_acquired_your_attribute, 1)
# index_html isn't index_html = None
That looks cool :-) Where's it documented? what does the 1 mean? cheers, Chris
On Fri, Jan 05, 2001 at 12:22:32PM +0000, Chris Withers wrote:
Martijn Pieters wrote:
You could use ComputedAttribute for that:
class MyClass(Acquisition.Explicit): # The following attribute is acquired transparently def _acquired_your_attribute(self): return self.aq_acquire('your_attribute') your_attribute = ComputedAttribute(_acquired_your_attribute, 1)
# index_html isn't index_html = None
That looks cool :-)
Where's it documented? what does the 1 mean?
Erm. The ExtensionClass.stx documentation hints at a ComputedAttribute class (but as an example of how you could use an ExtensionClass). The current C implementation of ComputedAttribute is not, as far as I can see, documented. As for the '1', the CVS log has the following to say on that: Added second "level" argument for computed attributes. This makes it easier to create computed attributes that work with acquisition. Normally, computed attributes are called with unwrapped objects. Passing a level of 1, causes computed attributes to be called with one level of wrapping. Note that the innermost (single) level of wrapping typically reflects a containment context with any extra access contexts stripped off. As I understand it, it makes self.aq_acquire possible. See also: http://cvs.zope.org/Zope2/lib/Components/ExtensionClass/ComputedAttribute.c -- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Anyone know if there are plans for a second O'Reilly book to cover development stuff like this? . o O ( Zope: The Definitive Developers Reference ) Ah, I guess I can only dream for now :-) cheers, Chris Martijn Pieters wrote:
On Fri, Jan 05, 2001 at 12:22:32PM +0000, Chris Withers wrote:
Martijn Pieters wrote:
You could use ComputedAttribute for that:
class MyClass(Acquisition.Explicit): # The following attribute is acquired transparently def _acquired_your_attribute(self): return self.aq_acquire('your_attribute') your_attribute = ComputedAttribute(_acquired_your_attribute, 1)
# index_html isn't index_html = None
That looks cool :-)
Where's it documented? what does the 1 mean?
Erm. The ExtensionClass.stx documentation hints at a ComputedAttribute class (but as an example of how you could use an ExtensionClass). The current C implementation of ComputedAttribute is not, as far as I can see, documented.
As for the '1', the CVS log has the following to say on that:
Added second "level" argument for computed attributes. This makes it easier to create computed attributes that work with acquisition. Normally, computed attributes are called with unwrapped objects. Passing a level of 1, causes computed attributes to be called with one level of wrapping. Note that the innermost (single) level of wrapping typically reflects a containment context with any extra access contexts stripped off.
As I understand it, it makes self.aq_acquire possible.
See also:
http://cvs.zope.org/Zope2/lib/Components/ExtensionClass/ComputedAttribute.c
-- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Martijn Pieters wrote:
Erm. The ExtensionClass.stx documentation hints at a ComputedAttribute class (but as an example of how you could use an ExtensionClass). The current C implementation of ComputedAttribute is not, as far as I can see, documented.
Now I think I know the answer to this one, but I'll ask just to be sure: class MyClass(Persistent Acquisition.Explicit): def _set_your_attribute (self,value): self._v_your_attribute = value def _get_your_attribute (self): return self._v_your_attribute your_attribute = ComputedAttribute(_get_your_attribute) ...with this class, your_attribute isn't going to play in Persistence, is it? (so I can update it lots without worrying about ZODB size growing... :-) Hmm... more questions: If I do: x = MyClass() x.your_attribute = 1 ...what happens? Where do you import the ComputedAttribute module from? cheers, Chris
On Wed, Jan 10, 2001 at 04:13:49PM +0000, Chris Withers wrote:
Martijn Pieters wrote:
Erm. The ExtensionClass.stx documentation hints at a ComputedAttribute class (but as an example of how you could use an ExtensionClass). The current C implementation of ComputedAttribute is not, as far as I can see, documented.
Now I think I know the answer to this one, but I'll ask just to be sure:
class MyClass(Persistent Acquisition.Explicit):
def _set_your_attribute (self,value): self._v_your_attribute = value
def _get_your_attribute (self): return self._v_your_attribute
your_attribute = ComputedAttribute(_get_your_attribute)
...with this class, your_attribute isn't going to play in Persistence, is it? (so I can update it lots without worrying about ZODB size growing... :-)
Yup, this allows you to alias your_attribute to _v_your_attribute without creating an attribute that *will* persist in the process.
Hmm... more questions:
If I do:
x = MyClass() x.your_attribute = 1
...what happens?
your_attribute is set to one instead of the ComputedAttribute instance and concequently persisted. If you want _set_your_attribute to be called, you need to override __setattr__: def __setattr__(self, name, value): setter = getattr(self, '_set_' + name, None) if setter: setter(value) else: raise AttributeError, "no such attribute: " + `name`
Where do you import the ComputedAttribute module from?
from ComputedAttribute import ComputedAttribute -- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Martijn Pieters wrote:
...with this class, your_attribute isn't going to play in Persistence, is it? (so I can update it lots without worrying about ZODB size growing... :-)
Yup, this allows you to alias your_attribute to _v_your_attribute without creating an attribute that *will* persist in the process.
yay! :-)
your_attribute is set to one instead of the ComputedAttribute instance and concequently persisted.
d'Oh... of course...
If you want _set_your_attribute to be called, you need to override __setattr__:
def __setattr__(self, name, value): setter = getattr(self, '_set_' + name, None) if setter: setter(value) else: raise AttributeError, "no such attribute: " + `name`
Hmmm... how would you change this to call the __setattr__ that was there before you overrode it, if a setter could not be found? cheers for all the help, this thread might make quite god docs for ComputedAttribute ;-) Chris
On Wed, Jan 10, 2001 at 05:07:07PM +0000, Chris Withers wrote:
If you want _set_your_attribute to be called, you need to override __setattr__:
def __setattr__(self, name, value): setter = getattr(self, '_set_' + name, None) if setter: setter(value) else: raise AttributeError, "no such attribute: " + `name`
Hmmm... how would you change this to call the __setattr__ that was there before you overrode it, if a setter could not be found?
The same way you call any overridden method, by calling it on the class you inherit it from. So: class Foo: def __setattr__(self, name, value): # Whatever pass class Bar(Foo): def __setattr__(self, name, value): Foo.__setattr__(self, name, value) # More whatever -- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
There's a (much) simpler way: class MyClass(Acquisition.Explicit): your_attribute = Acquisition.Acquired # index_html isn't index_html = None "Acquired" is a special object that the acquisition module looks for. However, I wasn't aware you could add a "1" to the ComputedAttribute constructor. Thanks! Shane Martijn Pieters wrote:
On Thu, Jan 04, 2001 at 10:46:35AM +0000, Chris Withers wrote:
Dieter Maurer wrote:
acquisition.donotacquire('index_html')
This would be great.
Indeed :-)
class MyClass (Acquisition.Explicit):
acquisition = ClassAcquisitionInfo()
acquisition.acquire('index_html') acquisition.acquire('fred') You already can do that, though with a different syntax (I would need to search for in the documentation).
You may mean that if x is an Acquisition.Explicit object, you can do:
x.aq_acquire('your_attribute') (syntax may be wrong ;-)
What I meant is that through a declaration in the class you could saying acquire the 'your_attribute' attribute but nothing else. So, you could still do:
x.your_attribute ...which would be acquired, but... x.index_html ...which wouldn't be acquired.
You could use ComputedAttribute for that:
class MyClass(Acquisition.Explicit): # The following attribute is acquired transparently def _acquired_your_attribute(self): return self.aq_acquire('your_attribute') your_attribute = ComputedAttribute(_acquired_your_attribute, 1)
# index_html isn't index_html = None
Or you could define a __getattr__ that does a lookup in a list for explicetly acquired attributes:
_acquired = ('index_html', 'fred') def __getitem__(self, key): if key in self._acquired: return self.aq_acquire(key) raise AttributeError, name
-- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Shane Hathaway wrote:
There's a (much) simpler way:
class MyClass(Acquisition.Explicit): your_attribute = Acquisition.Acquired
# index_html isn't index_html = None
Cool :-) And I suppose the other part of my wishlist: class MyClass(Acquisition.Implicit): # your_attribute will be acquied # index_html won't index_html = None Sorry for wasting the lists' time :-S Chris
Shane Hathaway wrote:
There's a (much) simpler way:
class MyClass(Acquisition.Explicit): your_attribute = Acquisition.Acquired
# index_html isn't index_html = None
"Acquired" is a special object that the acquisition module looks for.
Just noticed you can do this also: class MyClass: # note lack of base class ;-) your_attribute = Acquisition.Acquired ...got the idea from Traversable.py Now if I could just figure out how to make non-SimpleItem classes aware of security (see my other recent post :-S) cheers, Chris
participants (4)
-
Chris Withers -
Dieter Maurer -
Martijn Pieters -
Shane Hathaway