objectmanager contains object test
this is probably really simple, but I can't figure it out from the API docs. I want to test to see if a folder contains a dtml method with a known id. how do I do this from python? I've tried if context.data.news[ object_id ] which breaks with a key error, if it doesn't exist. I want something like context.data.news.has_key( object_id ) but object manager doesn't seem to have any methods like that. and doing a try, and catching key errors seems a messy way to do it.
try this: if hasattr(context.data.news, object_id) : ... Hope that helps, Kevin -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Ben Avery Sent: Wednesday, December 18, 2002 11:53 AM To: zope@zope.org Subject: [Zope] objectmanager contains object test this is probably really simple, but I can't figure it out from the API docs. I want to test to see if a folder contains a dtml method with a known id. how do I do this from python? I've tried if context.data.news[ object_id ] which breaks with a key error, if it doesn't exist. I want something like context.data.news.has_key( object_id ) but object manager doesn't seem to have any methods like that. and doing a try, and catching key errors seems a messy way to do it. _______________________________________________ Zope maillist - Zope@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 )
Kevin Carlson wrote:
try this:
if hasattr(context.data.news, object_id) : ...
If you don't want to be confused by aquiring objects from above, you should use something like: if hasattr(context.data.news.aq_explicit, object_id): -mj
Ben Avery writes:
I want to test to see if a folder contains a dtml method with a known id. how do I do this from python?
I've tried if context.data.news[ object_id ] which breaks with a key error, if it doesn't exist.
I want something like context.data.news.has_key( object_id ) but object manager doesn't seem to have any methods like that.
and doing a try, and catching key errors seems a messy way to do it. You have several options:
* "try: ... except: ..." around the code above * "hasattr(object.aq_explicit, attribute)" This is not fool proof * an external method "hasOwnAttr" providing the missing function from Acquisition import aq_base def hasOwnAttr(self,attr): return hasattr(aq_base(self),attr) * making the above method available to DTML and Python Script by modifying the Zope source. Dieter
participants (4)
-
Ben Avery -
Dieter Maurer -
Kevin Carlson -
Maik Jablonski