PYTHON: Searching for an object inside a folder
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? kind regards, Vitor Varalonga
Hi Vitor, this is a faq ;) Look for documentation on ZopeFind() and ZopeFindAndApply() (The Zope source is last resort, but also google, Zope.org and the parameters on Zopes managements "Find" page) Or see the following example: http://www.zope.org/Members/chrisw/zopefind HTH Tino Wildenhain --On Mittwoch, 16. Oktober 2002 16:33 +0100 Vitor Varalonga <vpedrosa@ciberbit.pt> 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?
kind regards,
Vitor Varalonga
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
participants (3)
-
Maik Jablonski -
Tino Wildenhain -
Vitor Varalonga