[Zope] q: How should I get a guaranteed unique id in Zope?

Troy Farrell troy@entheossoft.com
Mon, 14 Jan 2002 23:14:42 -0600


I do one of two things:
1) Use time.time() + user's IP stripped of dots
2) Use this code (from a python product):

# self.projects and self.deactivated projects
# are PersistentMapping()s using unique keys
# though you can use has_key to check for the
# existence of a key in an ObjectManager (such as
# a folder) as well

   def __init__(self, id = None):
     "Create the bugger."
     self.id = 'a_fixed_id'
     self.last_project_id = 1
     self.projects = PersistentMapping()
     self.deactivated_projects = PersistentMapping()

   def increment_last_id(self):
     "Increase the last id property"
     while (self.projects.has_key(str(self.last_project_id)) or 
self.deactivated_projects.has_key(str(self.last_project_id))):
       self.last_project_id = self.last_project_id + 1

   def manage_addProject(self,
                         project_id,
                         REQUEST = None):
     "Add a project."
     project = PersistentMapping()
     if project_id == 'new': # the manage_addProjectForm sets project_id 
== 'new' to get a new id.
       self.increment_last_id() # don't forget this one
       project_id = str(self.last_project_id)
     project['id'] = project_id
     self.projects[project_id] = project

I hope that helps, Joe.

Troy
-- 
Troy Farrell
Developer
Entheos Software
mailto:troy@entheossoft.com
http://www.entheossoft.com

Joe Block wrote:

> I just discovered Zope a couple of weeks ago, and have started migrating 
> a cgi based web application that I was writing in straight python to 
> Zope both as a learning exercise and to take advantage of Zope's feature 
> set.
> 
> My problem is, I have several sql tables that I'm storing data in that I 
> need to be able to have the same jobid in because I'm using the jobid to 
> match which comments are attached to which jobs.  When I was using cgis, 
> this was easy - I made a class that wrapped the following function
> 
>         def makeID(self):
>                 idstring = "%s-%s.%s-%s" % (self.prefix, os.getpid(), 
> time.time(), self.counter)
>                 self.counter = self.counter + 1
>                 return idstring
> 
> and I could be guaranteed their uniqueness because prefix was the 
> hostname of the server the cgi was running on, and the combination of 
> the other 3 parts was unique per call to makeID.
> 
> Now that I'm using Zope, the pid isn't unique any more, of course.  I 
> don't want to store a counter in the database, because from some of the 
> postings I saw in the last few days here I think it'll cause my db to 
> grow for no reason as it stores unwanted undo information.  I also 
> haven't figured out how to lock an object while I update properties 
> yet.  I don't want to use an external file either, because the constant 
> locking & unlocking seems likely to cause performance problems eventually.
> 
> I have both _The Zope Book_ and _Zope Web Application Development and 
> Content Management_ , and haven't found an answer to my problem in 
> either book as yet.
> 
> I'm running Zope 2.5.0b3 with Python 2.1.1 on Mac OS X 10.1.2
> 
> Thanks,
> 
> jpb
> -- 
> Joe Block <jpb@ApesSeekingKnowledge.net>
> 
> Madness takes its toll. Please have exact change ready.