[Zope3-Users] Modified IntId utility

Maken Seteva crapkonto at gmail.com
Mon Sep 24 12:53:58 EDT 2007


Fellow Zopers.
I have made a slight modification to my intid util by inheriting from  
IntId and overriding _generateId().
I do this to be sure that all new created objects will have an  
incrementing number where
the first object created gets id 1, the next gets id 2 and so on.  
This way I get for "free" sorting by
oldest/newest.


class MyIntIds(IntId):
      # We need a non-volatile nextid
      nextid = None

      def _generateId(self):
          # In the unlikely event that the nextid already
          # exists, we run this in a while loop to fix it.
          while True:
              if len(self) == 0:
                  self.nextid = 1
              elif self.nextid is None:
                  self.nextid = self.refs.maxKey() + 1
              uid = self.nextid
              self.nextid += 1
              if uid not in self.refs:
                  return uid
              # Normally we would never get here..
              self.nextid = None


What do you think about this? Is it "safe" to do this, or have i  
forgotten any unforseen oddities
that might occur in the future :O

Regards
Seteva


More information about the Zope3-users mailing list