|
|
 |
|
Title: Simple Universally Unique ID (UUID or GUID)
Submitter: Carl Free Jr
(other recipes)
Last Updated: 2003/08/02
Version no: 1.0
Category:
Databases
|
|
2 vote(s)
|
|
|
|
Description:
This is a short & sweet UUID function. Uniqueness is based on network address, time, and random.
Another good point: does not create a "hot spot" when used in a b-tree (database) index. In other words, if your IDs look something like "abc100" and "abc101" and "abc102"; then they will all hit the same spot of a b-tree index, and cause the index to require frequent reorganization. On the other hand, if your IDs look more like "d29fa" and "67b2c" and "e5d36" (nothing alike); then they will spread out over the index, and your index will require infrequent reorganization.
Source: Text Source
import time, random, md5
def uuid( *args ):
"""
Generates a universally unique ID.
Any arguments only create more randomness.
"""
t = long( time.time() * 1000 )
r = long( random.random()*100000000000000000L )
try:
a = socket.gethostbyname( socket.gethostname() )
except:
a = random.random()*100000000000000000L
data = str(t)+' '+str(r)+' '+str(a)+' '+str(args)
data = md5.md5(data).hexdigest()
return data
Discussion:
Down side: Not especially fast. I would integrate a similar C or java version of this (without the MD5) into my python code if this is a problem.
|
|
Add comment
|
|
Number of comments: 5
socket missing..., Bill Scherer, 2003/08/07
It's a tad faster if actually import socket...
Beware the blanket exception.
Add comment
What about compliance with the UUID spec?, Joe Madia, 2003/08/08
This algorithm does not appear to generate correct UUIDs. UUIDs are officially and specifically defined as part of the ISO-11578 standard [1]. The WebDAV spec [2] also defines, in section 6.4.1, a safe way to calculate the 'node' data required by the UUID algorithm in situations where a network address is either not available or could be a security risk. I have not seen ISO-11578 but I understand that it is very similar to the algorithm defined in the UUIDs and GUIDs Internet Draft [3] which does not appear to be similar to this code at all.
Am I missing something obvious here?
[1] ISO/IEC 11578 - Remote Procedure Call (RPC)
http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=2229
[2] HTTP Extensions for Distributed Authoring -- WEBDAV
http://www.ietf.org/rfc/rfc2518.txt
[3] UUIDs and GUIDs
http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt
Add comment
If you are on a UNIX system with the uuidgen command, Michael Hoffman, 2003/11/05
You can use:
import commands
def uuidgen():
return commands.getoutput('uuidgen')
Add comment
That should be, Michael Hoffman, 2003/11/05
import commands
def uuidgen():
return commands.getoutput('uuidgen')
Add comment
A UUID module is now included in the standard distribution, Jon Marshall, 2007/04/04
A uuid module is included in python 2.5+. 2.3 and 2.4 users can get the module from http://zesty.ca/python/
Add comment
|
|
|
|
|
 |
|