[Zope] Python Script Question

Paul Winkler pw_lists at slinkp.com
Fri May 14 15:14:16 EDT 2004


On Fri, May 14, 2004 at 11:54:13AM -0500, Laura McCord wrote:
> I am starting out with this in my script
> 
> RESPONSE = context.REQUEST.RESPONSE
> success = 0
> for id in context.objectIds():
>    success = success + 1
> return success
> 
> 
> All this does is count the contents in the folder. However, how do you
> specify what content to count. For instance, I do not want this script
> to count itself in the folder contents but I want it to include
> everything else. Any suggestions?

note that ObjectManager.objectIds() takes an optional parameter
that is a list of meta types.
Stuff like this is in the API reference:
http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/AppendixB.stx
... not easy to read, but well worth spending some time with.

So to count e.g. only DTML and ZPT objects:

...
for id in context.objectIds(['DTML Document', 'DTML Method', 
                             'Page Template']):
    success += 1
...


Another option is to get a complete list and filter it.
Python's list comprehensions are a handy way to do this:

all_ids = context.objectIds()
filtered = [id for id in all_ids if id != script.getId()]  
for id in filtered:
    success += 1
...


List comprehensions are described in the Python language
documentation, see this page:
http://www.python.org/doc/current/tut/node7.html

One more tip: if all you're doing is counting the length
of the sequence, you don't have to use arithmetic, you
can just use the built-in len() function, e.g.

success = len(context.objectIds())

-- 

Paul Winkler
http://www.slinkp.com



More information about the Zope mailing list