@property strips acquisition context

9 messages Options
Embed this post
Permalink
Hedley Roos () @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
Hi

I have a Dexterity custom class. I decorate a method foo with @property
but inside the method 'self' is not acquisition wrapped anymore. I'm
sure the object is wrapped before my attempt to access ob.foo

I'm sure it is not related to Dexterity but I mention it in case.

So the question is: should I be surprised that it is not working?

Thanks
Hedley

_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
David Glick-2 () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
On Aug 31, 2009, at 8:15 AM, Hedley Roos wrote:

> Hi
>
> I have a Dexterity custom class. I decorate a method foo with  
> @property
> but inside the method 'self' is not acquisition wrapped anymore. I'm
> sure the object is wrapped before my attempt to access ob.foo
>
> I'm sure it is not related to Dexterity but I mention it in case.
>
> So the question is: should I be surprised that it is not working?


This is a known side effect of the @property decorator.  Not sure what  
the best workaround is.  Anyone?


David Glick
Web Developer
ONE/Northwest

New tools and strategies for engaging people in protecting the  
environment

http://www.onenw.org
[hidden email]
work: (206) 286-1235 x32
mobile: (206) 679-3833

Subscribe to ONEList, our email newsletter!
Practical advice for effective online engagement
http://www.onenw.org/full_signup





_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Martijn Pieters () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
On Mon, Aug 31, 2009 at 18:54, David Glick<[hidden email]> wrote:

>> I have a Dexterity custom class. I decorate a method foo with @property
>> but inside the method 'self' is not acquisition wrapped anymore. I'm
>> sure the object is wrapped before my attempt to access ob.foo
>>
>> I'm sure it is not related to Dexterity but I mention it in case.
>>
>> So the question is: should I be surprised that it is not working?
>
> This is a known side effect of the @property decorator.  Not sure what the
> best workaround is.  Anyone?

Use ExtensionClass.ComputedAttribute instead:

  from ExtensionClass import ComputedAttribute

  @ComputedAttribute
  def foo(self):
      # self now remains Acquisition wrapped

--
Martijn Pieters

_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Hedley Roos-2 () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
> Use ExtensionClass.ComputedAttribute instead:
>
>  from ExtensionClass import ComputedAttribute
>
>  @ComputedAttribute
>  def foo(self):
>      # self now remains Acquisition wrapped


Thanks Martijn. BTW I accidentally posted to the plone-dev list where
Ross also replied but I'll stick to this list.

I have to do "from ComputedAttribute import ComputedAttribute". This
is for Zope 2.10 (think so) and Plone 3.3.

I still cannot get it to work. It is not a train smash but even so I'd
like to know what is wrong. How did you know you can use
ComputedAttribute as a decorator?

Hedley

_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Hedley Roos-2 () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
Replying to my own post for future reference...

I was not successful in using @ComputedAttribute, but I could do

def _foo(self):
  # Trivial example
  return self.aq_parent.id
foo = ComputedAttribute(_foo, 1)

This looks a lot like the way we used to do decorators before we got
the @ syntactic sugar. Maybe the correct way to use the decorator is
@ComputedAttribute(1). I don't know and I do not have enough time to
investigate.

Hedley

_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Martin Aspeli () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
In reply to this post by David Glick-2
David Glick wrote:

> On Aug 31, 2009, at 8:15 AM, Hedley Roos wrote:
>
>> Hi
>>
>> I have a Dexterity custom class. I decorate a method foo with  
>> @property
>> but inside the method 'self' is not acquisition wrapped anymore. I'm
>> sure the object is wrapped before my attempt to access ob.foo
>>
>> I'm sure it is not related to Dexterity but I mention it in case.
>>
>> So the question is: should I be surprised that it is not working?
>
>
> This is a known side effect of the @property decorator.  Not sure what  
> the best workaround is.  Anyone?

The best workaround is to not depend on acquisition. :)

The other workaround is to use a ComputedAttribute.

The third workaround is to do something like:

 >>> from zope.app.component.hooks import getSite
 >>> site = getSite()

The 'site' object is now the aq-wrapped portal root, from which you can
acquire stuff.

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book


_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Martin Aspeli () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
In reply to this post by Hedley Roos-2
Hedley Roos wrote:

> Replying to my own post for future reference...
>
> I was not successful in using @ComputedAttribute, but I could do
>
> def _foo(self):
>   # Trivial example
>   return self.aq_parent.id
> foo = ComputedAttribute(_foo, 1)
>
> This looks a lot like the way we used to do decorators before we got
> the @ syntactic sugar. Maybe the correct way to use the decorator is
> @ComputedAttribute(1). I don't know and I do not have enough time to
> investigate.

I think you'd need to make a custom decorator. Something like:

def aq_aware_property(fn):
     return ComputedAttribute(fn, 1)

Then you can do:

@aq_aware_property
def foo(self):
     return self.aq_parent.id

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book


_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Hedley Roos () Re: Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
In reply to this post by Martin Aspeli
Martin Aspeli wrote:
> The best workaround is to not depend on acquisition. :)

I don't explicitly want the acquisition, but I can't do getPhysicalPath
without it. __parent__ is not available either else I could traverse
upwards. At the simplest level I just want self's container.

I did end up implementing as per your suggestion.

Thanks
Hedley

_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers
Martijn Pieters () Re: @property strips acquisition context
Reply Threaded More More options
Print post
Permalink
In reply to this post by Hedley Roos-2
On Mon, Aug 31, 2009 at 21:42, Hedley Roos<[hidden email]> wrote:
> I still cannot get it to work. It is not a train smash but even so I'd
> like to know what is wrong. How did you know you can use
> ComputedAttribute as a decorator?

I didn't; I forgot about the extra unwrapping parameter. Mea culpa! As
suggested, you could create your own decorator that uses
ComputedAttribute:

  def acqproperty(func):
      return ComputedAttribute(func, 1)

or better still:

  def acqproperty(level=1):
      def _decorator(func):
          return ComputedAttribute(func, level)
      return _decorator

--
Martijn Pieters

_______________________________________________
Product-Developers mailing list
[hidden email]
http://lists.plone.org/mailman/listinfo/product-developers