ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> zope-List
zope-List
Re: [Zope] Howto use getattr() without acquisition?
by Gilles Lenfant other posts by this author
Dec 31 2002 5:02PM messages near this date
[Zope] Howto use getattr() without acquisition? | Re: [Zope] Howto use getattr() without acquisition?
I had a similar problem some weeks ago and Dieter Maurer gave me the
enlightenment.

I think you should use getattr(object.aq_explicit, 'someattr')

Hereafter is the copy/paste of his post.

You may want to read the "Acquisition" section of

  <http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html> 

You will learn that acquisition wrappers have two components: "aq_self"
and "aq_parent", and that an implicit acquisition wrapper first
asks "aq_self" and, if unsuccessful, automatically asks "aq_parent"
for an attribute lookup, while an explicit wrapper only asks "aq_self".

With these explanations you will understand that
"hasattr(obj.aq_explicit,*)"
is equivalent to "hasattr(obj.aq_base,*)" iff "obj.aq_self" is not
itself acquisition wrapped.


Dieter



----- Original Message -----
From: dave@[...].com
To: zope@[...].org
Sent: Tuesday, December 31, 2002 5:08 PM
Subject: [Zope] Howto use getattr() without acquisition?



I'm building a python script to generate breadcrumb navigation links on each
page of my website.

It creates hyperlinks and uses the title or id for the link. I'd like to add
a feature so that if the folder has a "nickname" property, it will use this
for the breadcrumb hyperlink instead of the title.

I tried using "hasattr()" to see if the current folder has a nickname
property. It seems to work if the folder has a nickname property. However,
if the folder doesn't have that properly, but the parent does, it still
returns "true" and then getattr() acquires it from the parent instead of
using the title. Is there a way to tell hasattr() to *not* use acquisition?
Or perhaps another method which would do what I want? I'm sure there is a
much more elegant way to do this...

(These question feels like a FAQ, and I did do my best to search the group
archives, but couldn't find anything appropriate-- most breadcrumb
conversations seem to revolve around DTML and getProperty().)

--dave


"""
This script creates breadcrumb style navigation links.

It walks the REQUEST.PARENTS list of parents and creates
a hyperlink for each parent. It stops at the Examples folder.
"""
links=[]
for parent in context.REQUEST.PARENTS[1:]:
    id = parent.getId()
    if hasattr(parent, 'nickname'):
        label = getattr(parent, 'nickname')
    elif parent.title != "":
        label = parent.title
    else:
        label = parent.getId()
    links.insert(0, """<a href="%s" class="breadcrumb"> %s</a>""" %
(parent.absolute_url(), label))

# Now get label for current container
if context.hasProperty('nickname'):
  label = context.nickname
elif context.title != "":
  label = context.title
else:
  label = context.getId()

return " : ".join(links) + " : " + label



_______________________________________________
Zope maillist  -  Zope@[...].org
http://lists.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope-dev )
Thread:

Gilles Lenfant
Dieter Maurer
Chris Withers

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved