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? Thanks, Laura
On Fri, 2004-05-14 at 09:54, 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?
Trivial case: if success: return success - 1 else: return 0 Otherwise, you can provide objectIds a list of content types to include (see the Zope API Refererence), or you can manually exclude types by checking inside your loop. -- Jud <lists@dagnall.net> http://www.dagnall.net
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
Laura, a nice method would be to trap for only certain object types. This way you can exclude Python Scripts altogether but count everything else. e.g. for object in context.objectValues(['Folder', 'DTML Document', 'File']): in this case it will just trap for Folders, DTML Documents and Files. obviously, it's not helpful to you if you want to trap everything but itself, *including* other Python Scripts... so we can just trap for the offending Id before adding to "success". I named this script itemCount success = 0 offending_Id = "itemCount" for item in context.objectValues(): b = item.getId() if b.find (offending_Id) > -1: success = success - 1 else: success = success + 1 return success it works fine. just keep in mind if your folder has 6 items plus this script it will return 5. good luck Michael Ruberto DORTRONICS SYSTEMS INC. -----Original Message----- From: zope-bounces@zope.org [mailto:zope-bounces@zope.org]On Behalf Of Laura McCord Sent: Friday, May 14, 2004 12:54 PM To: zope@zope.org Subject: [Zope] Python Script Question 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? Thanks, Laura _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
One more case -- count everything that is *not* of a given meta type. (e.g. exclude ALL python scripts and all external methods, but count everything else): forbidden = ('Script (Python)', 'External Method') items = context.objectValues() i = 0 for item in items: if item.meta_type not in forbidden: i += 1 return i This is a little tricky, because it assumes you know that ALL other types should be counted. Which may not be what's intended, if, for example, an alternative scripting object is added. (???) But you get the idea. Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
participants (5)
-
Jud Dagnall -
Laura McCord -
Michael A. Ruberto -
Paul Winkler -
Terry Hancock