Re: Generating a unique identifier

2007-09-10 Thread stephen . lewitowski
On Sep 7, 1:03 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > I have an application that will be producing many instances, using them > for a while, then tossing them away, and I want each one to have a unique > identifier that won't be re-used for the lifetime of the Python se

Re: Generating a unique identifier

2007-09-08 Thread Hrvoje Niksic
Steven D'Aprano <[EMAIL PROTECTED]> writes: > Should garbage-collecting 16 million strings really take 20+ > minutes? It shouldn't. For testing purposes I've created a set of 16 milion strings like this: s = set() for n in xrange(1600): s.add('somerandomprefix' + str(n)) # prefix makes t

Re: Generating a unique identifier

2007-09-07 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > > Sorry, make that 32 or 40 instead of 10, if the number of id's is large, > > to make birthday collisions unlikely. > > I did a small empirical test, and with 16 million ids, I found no > collisions. 16 million 32-byte ids? With string and dictiona

Re: Generating a unique identifier

2007-09-07 Thread Steven D'Aprano
On Fri, 07 Sep 2007 19:17:44 -0700, Paul Rubin wrote: >> Here's something which is a little less predictable than a straight >> counter: > > It's still very easy to generate valid id's from that, or guess the next > one with non-negligible probability. Well, in my specific application, the only

Re: Generating a unique identifier

2007-09-07 Thread Steven D'Aprano
On Fri, 07 Sep 2007 08:47:58 -0700, Paul Rubin wrote: > Paul Rubin writes: >> def unique_id(): >>return os.urandom(10).encode('hex') > > Sorry, make that 32 or 40 instead of 10, if the number of id's is large, > to make birthday collisions unlikely. I did a small e

Re: Generating a unique identifier

2007-09-07 Thread Steve Holden
Paul Rubin wrote: > Paul Rubin writes: >> the probability is about exp(-(2**60 / 2*2**160)) = 1/exp(2**101) > > Bah. Got that backwards and calculated wrong. The probability of no > collisions is > > exp(-(2**60) / (2*2**160)) = exp(-(2**-101)) = approx (1 - 2**-1

Re: Generating a unique identifier

2007-09-07 Thread Paul Rubin
Paul Rubin writes: > the probability is about exp(-(2**60 / 2*2**160)) = 1/exp(2**101) Bah. Got that backwards and calculated wrong. The probability of no collisions is exp(-(2**60) / (2*2**160)) = exp(-(2**-101)) = approx (1 - 2**-101) which is very close to 1.

Re: Generating a unique identifier

2007-09-07 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > > unique_id = itertools.count(1234567890) > > Sweet! > > I really must make itertools second-nature. I always forget it. Beware that count() output (like enumerate) is always <= sys.maxint. It raises an exception if it overflows. IMO this is a bug.

Re: Generating a unique identifier

2007-09-07 Thread Paul Rubin
Ben Finney <[EMAIL PROTECTED]> writes: > > http://docs.python.org/lib/module-uuid.html > I second this recommendation. If you want arbitrary unique IDs that > are not a function of the data they identify, the simplest solution is > a monotonically-increasing serial number. If you want more than

Re: Generating a unique identifier

2007-09-07 Thread Steven D'Aprano
On Fri, 07 Sep 2007 08:42:45 -0700, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: >> def unique_id(): >> n = 1234567890 >> while True: >> yield n >> n += 1 > > unique_id = itertools.count(1234567890) Sweet! I really must make itertools second-nature. I

Re: Generating a unique identifier

2007-09-07 Thread Ben Finney
Will Maier <[EMAIL PROTECTED]> writes: > On Fri, Sep 07, 2007 at 12:03:23PM -, Steven D'Aprano wrote: > [...] > > which is easy enough, but I thought I'd check if there was an > > existing solution in the standard library that I missed. Also, for > > other applications, I might want them to be

Re: Generating a unique identifier

2007-09-07 Thread Paul Rubin
Paul Rubin writes: > def unique_id(): >return os.urandom(10).encode('hex') Sorry, make that 32 or 40 instead of 10, if the number of id's is large, to make birthday collisions unlikely. If you don't want the id's to be that large, you can implement a Feistel cipher

Re: Generating a unique identifier

2007-09-07 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > def unique_id(): > n = 1234567890 > while True: > yield n > n += 1 unique_id = itertools.count(1234567890) > which is easy enough, but I thought I'd check if there was an existing > solution in the standard library that I miss

Re: Generating a unique identifier

2007-09-07 Thread Wildemar Wildenburger
Will Maier wrote: > On Fri, Sep 07, 2007 at 12:03:23PM -, Steven D'Aprano wrote: > [...] >> which is easy enough, but I thought I'd check if there was an existing >> solution in the standard library that I missed. Also, for other >> applications, I might want them to be rather less predictabl

Re: Generating a unique identifier

2007-09-07 Thread kyosohma
On Sep 7, 7:03 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > I have an application that will be producing many instances, using them > for a while, then tossing them away, and I want each one to have a unique > identifier that won't be re-used for the lifetime of the Python se

Re: Generating a unique identifier

2007-09-07 Thread Marc 'BlackJack' Rintsch
On Fri, 07 Sep 2007 12:03:23 +, Steven D'Aprano wrote: > I have an application that will be producing many instances, using them > for a while, then tossing them away, and I want each one to have a unique > identifier that won't be re-used for the lifetime of the Python session. > > I can't

Re: Generating a unique identifier

2007-09-07 Thread Will Maier
On Fri, Sep 07, 2007 at 12:03:23PM -, Steven D'Aprano wrote: [...] > which is easy enough, but I thought I'd check if there was an existing > solution in the standard library that I missed. Also, for other > applications, I might want them to be rather less predictable. 2.5 includes the uuid

Generating a unique identifier

2007-09-07 Thread Steven D'Aprano
I have an application that will be producing many instances, using them for a while, then tossing them away, and I want each one to have a unique identifier that won't be re-used for the lifetime of the Python session. I can't use the id() of the object, because that is only guaranteed to be un