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

Vitor Varalonga vpedrosa@ciberbit.pt
Wed, 16 Oct 2002 16:33:20 +0100


--------------FACF010A01DC811ABD166FE8
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

    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

--------------FACF010A01DC811ABD166FE8
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<tt>&nbsp;&nbsp;&nbsp; Hi!</tt>
<br><tt>&nbsp;</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp;&nbsp; I need to check, inside a Python script,
if an object with a</tt>
<br><tt>particular ID exists inside a folder.</tt>
<br><tt>&nbsp;&nbsp;&nbsp; Right now I'm using the following code:</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp; # this is the folder I want to search</tt>
<br><tt>&nbsp;&nbsp;&nbsp; myFolder = container.REQUEST.PARENTS[0].img_folder</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp; # search the folder for an object with ID &lt;object_id></tt>
<br><tt>&nbsp;&nbsp;&nbsp; obj = getattr(myFolder, object_id, None)</tt>
<br><tt>&nbsp;&nbsp;&nbsp; if obj is not None:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # there is no such object</tt>
<br><tt>&nbsp;&nbsp;&nbsp; else:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # there is an object
with that id</tt>
<br><tt>&nbsp;</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp; Is there a more efficient way to perform this
operation?</tt>
<br><tt>&nbsp;</tt><tt></tt>
<p><tt>kind regards,</tt><tt></tt>
<p><tt>Vitor Varalonga</tt></html>

--------------FACF010A01DC811ABD166FE8--