[Zope] Re: PYTHON: Searching for an object inside a folder

ielbert ielbert@genuone.com
Fri, 15 Nov 2002 15:39:45 -0000


I would like to bring back the old issue because we did some tests 
and figured that the often-used idiom:
   if hasattr(myFolder.aq_explicit,object_id):
       #myFolder contains object_id
       #myFolder.aq_explicit prevents aquiring from above...

does not always work as expected.

It may return different result if, for example, the script is called 
as:     otherFolder/myFolder/myFolder/script

Please correct me if I am wrong but it would be better to say:
   if hasattr(myFolder.aq_inner.aq_explicit,object_id):
       #....

Regards,
Igor

--- In zope@y..., Maik Jablonski <maik.jablonski@u...> wrote:
Vitor Varalonga wrote:
>     Hi!
>  
> 
>      I need to check, inside a Python script, if an object with a
> particular ID exists inside a folder.
>     Right now I'm using the following code:
> 
>     # this is the folder I want to search
>     myFolder = container.REQUEST.PARENTS[0].img_folder
> 
>     # search the folder for an object with ID <object_id>
>     obj = getattr(myFolder, object_id, None)
>     if obj is not None:
>         # there is no such object
>     else:
>         # there is an object with that id
>  
> 
>     Is there a more efficient way to perform this operation?

hi,

untested..;-), but should work:

if hasattr(myFolder.aq_explicit,object_id):
    # there is an object with object_id
else:
      there is no object with object_id

myFolder.aq_explicit prevents aquiring from above...;-)

cheers, maik
-- 
Maik Jablonski                 __o
www.zfl.uni-bielefeld.de     _ \<_    Deutsche Zope User Group
Bielefeld, Germany          (_)/(_)               www.dzug.org