[Zope] Creating instances of products - finding id for them - example

Heimo Laukkanen huima@fountainpark.org
Tue, 05 Mar 2002 14:24:47 +0200


Here is a small example how I create in one PythonScript new instances 
of products - and manage to get new ids for them. Ideas I got from 
discussions on zope-mailing list. Comments appreciated

#
# idtsrat = id which we are trying to crete, this is the base for new id
# First we check wether the id exists, if it exists
# If it exists we start to add a counting number after it and checking
# wether that exists

if hasattr(context, idstart):
        while hasattr(context, (idstart + str (start +1 ))):
            start = start + 1
        id = idstart + str(start +1)
else:
        id = idstart

#
# And then we try to create the instance. If it fails - for example
# another thread created instance with same id. Add one to the id and
# try again

while 1:
    try:
       ****Create the instance here with id****; break;
    except:
        start= start + 1
        id = idstart + str(start +1)