[Zope] How to test if an object exist

Phil Harris phil.harris@zope.co.uk
Wed, 23 Aug 2000 15:36:55 +0100


def testFolder(self):
    self=self.this()
    if hasattr(self,'id of object to check for'):
      sub1=self.folder1
      exec "object2create = 'folder11'"
      try:
          if sub1.object2create:
              return 'Object exists'
      except AttributeError:
          sub1.manage_addFolder(object2create)
          return 'Object created'
    else:
      print 'oops folder does not exist, bugger'

----- Original Message -----
From: "Francois-Regis CHALAOUX"
<Francois-Regis.CHALAOUX@sanofi-synthelabo.com>
To: "Receipt Notification Requested" <zope@zope.org>
Sent: Wednesday, August 23, 2000 2:51 PM
Subject: [Zope] How to test if an object exist


> Hi,
>
> In the following code I test if a folder exists. The folder name to test
is
> generated on the fly and here I try to simultate it with 'exce' function
(may
> my error). The first time object2create does not exist and object2create
is
> well created but if run again the script I obtain the following error :
>
> Errot Type: NameError
> Error Value: folder1
>
> How to manage this problem ?
>
> FR.
>
> =====
> CODE
> =====
> def testFolder(self):
>     self=self.this()
>     sub1=self.folder1
>     exec "object2create = 'folder11'"
>     try:
>         if sub1.object2create:
>             return 'Object exists'
>     except AttributeError:
>         sub1.manage_addFolder(object2create)
>         return 'Object created'
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> 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 )