[Zope] Re: Unique id product
Gijs Reulen
greulen@gilsing.nl
Mon, 31 Jul 2000 11:17:46 +0200
A different approach:
For each entry I just use the current date/time including a call:
time.sleep(0.001) This way I am sure each entry will have a unique id. It is
only slightly slower (0.001 second for each calc). However, it requires
serial processing of entries and a stable time (only server-side
processing). Therefore very usable for processing batch-entries as I do.
Gijs Reulen
> -----Oorspronkelijk bericht-----
> Van: alexande@cat-box.net [mailto:alexande@cat-box.net]Namens Steve
> Alexander
> Verzonden: vrijdag 28 juli 2000 19:20
> Aan: zope@zope.org; Sin Hang Kin
> Onderwerp: [Zope] Re: Unique id product
>
>
> On 2 July, 2000 Steve Alexander wrote:
> >
> > If you just want ids that are unique to a folder, try the following
> > algorithm (taken originally from the Discussion object code in the PTK).
> > The variable "self" is the one passed to the constructor method of a
> > particular Python class, and represents the folder you want to put the
> > new object into. I'm assuming the object is a "FooBar Item". The
> > "foobar-%06d" bit generates a unique id for the object that looks like
> > "foobar-290172837", based on the current time.
> >
> > id = int(DateTime().timeTime())
> > while hasattr(self, str(id)):
> > id = id +1
> > id = 'foobar-%09d' % id
> >
> > The advantage of this approach is that there is very little contention
> > in most cases.
>
> I just looked back over some code where I used this algorithm, and I
> realised that it doesn't actually work :-( The identifiers that are
> searched for do not reflect the identifiers that are produced.
>
> The original code from the PTK does work. Here it is, adapted very
> slightly:
>
> # Find an unused id in location
> id = int(DateTime().timeTime())
> while hasattr(self, `id`):
> id = id + 1
> return id
>
> Here's my own, now fixed, code. Note the addition of a "max_tries"
> variable.
>
> if id == 'auto' or id == None:
> n = int(DateTime().timeTime())
> max_tries = 10
> fk = lambda key: 'foobar-%d' % key # function to format key
> id = fk(n)
> while max_tries and hasattr(self, str(id)):
> n = n + 1
> max_tries = max_tries-1
> id = fk(n)
>
>
> Sorry for posting such rubbish the first time around :-/
>
> --
> Steve Alexander
> Software Engineer
> Cat-Box limited
> http://www.cat-box.net
>
> _______________________________________________
> 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 )
>
>