[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
> 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:
... 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:

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

More information about the Zope mailing list