[Zope] Newbie Question: dir(...)

Paul Winkler pw_lists@slinkp.com
Fri, 6 Jun 2003 07:52:31 -0400


On Fri, Jun 06, 2003 at 09:41:47AM -0400, Tiller, Michael (M.M.) wrote:
>  Lots of Zope sites have comments about "see the Zope Book", but I find "the
> Zope Book" rather frustrating because it seems to cover things only at a
> conceptual level and doesn't really provide a good reference book.

It's hard to skim, yes, but it really does contain a lot of information.
Check the latest online edition, and DEFINITELY check the reference
sections in the back.

I realize the online Zope Book is not as easy to use as it could be.
It would really benefit from a search form, and a clickable TOC at the 
top of each chapter. I've filed feature requests for those.

> For example, I was working (by example) on developing a nice little box for
> one of my slots that would list the "Files" in the current folder (and only
> in the current folder).  Since I was converting this from the "newsBox"
> macro, I saw roughly what needed to be done.  I managed to find some
> document somewhere that covered the details of searchResults, but what I was
> struggling with mostly was something very simple:
>  
> At the point where the macro is run, there is a variable called "here"
> available.  Ignoring the background questions for the moment (who sets
> this?, is it set for all documents?, what is the "type" of this variable?),

Zope Book -> Zope Page Templates Reference -> TALES Expression Types ->
Built In Names

"here - the object to which the template is being applied"

> I had a very simple question.  I know I have this variable, what can I do
> with it?  

Depends what it resolves to. In Plone this will be whatever you're
viewing - a Document, a File, a Folder, etc.

> I just wanted to find out the parent object and then, ultimately,
> the path of the parent object so that I could use that as an argument to
> searchResults.
>  
> It took me forever to finally come up with:
> path='/'.join(here.aq_parent.getPhysicalPath()) because I didn't know about
> aq_parent, I didn't know about getPhysicalPath(), etc.

Zope Book -> API Reference -> ObjectManagerItem (which according
to the API reference is the base class for just about everything*).
That tells you about getPhysicalPath and a bunch of other useful
stuff that applies to all objects in Zope.

* this is not really true. I don't know why this was done, but
ObjectManagerItem was invented for the API documentation.
Grrrr.  SimpleItem and Item (defined in lib/python/OFS/SimpleItem.py) 
are the most common actual base classes.

Anyway, aq_parent does not seem to be in the Zope Book.
Pity.  The other document you should really check out is
the Zope Developers' Guide:

http://www.zope.org/Documentation/Books/ZDG/current/contents

chapter 6, "Acquisition", tells you about aq_parent and many
other things you can do with acquisition. Keep in mind that it's
targeted at people developing filesystem Products, so there's
a lot of nitty-gritty that you may find not useful and distracting
for tasks like customizing a Plone site.
Of special interest might be the sections "Acquisition
Wrappers", "Additional Attributes and Methods", and
"Acquisition Module Functions".  It's rather short on
concrete examples but it's a good place to look up
code that you encounter with aq_* stuff in it, to see what
it's doing.   Also be aware that some of the aq_* methods
and attributes are not available to page templates and
Python Scripts for security reasons.  When in doubt, try it
and see if you get away with it :-P

Also get familiar with zopelabs.com, there's lots of great cookbook-style
examples arranged in categories, and a decent search interface.

> It would really have helped me to know what the attributes and methods are
> for each of these objects.  I tried using "dir", e.g.:
>  
> <div tal:replace="python:dir(here)"></div>
>
> But this generated an error because it said "dir" was undefined.

I know... I remember how frustrated I was as a python programmer
when I first came to Zope and discovered I could no longer "ask
the interpreter". But read on:

> Is there a
> simple way, when working within the Zope framework, to quickly determine
> what methods are available on an object?

You might want to install (on a development box anyway... maybe not
on a production system) DocFinder and DocFinderEverywhere.
http://www.zope.org/Members/shh/DocFinderEverywhere

With them installed, the ZMI gets a "Doc" tab which you can click
on and see what classes this object inherits from, see docstrings for
all of them. Be warned though, there can be a LOT of information.
For instance, Folder inherits from 23 classes... DTML MEthod inherits
from 25 classes... etc.


-- 

Paul Winkler
http://www.slinkp.com