tags:

views:

6643

answers:

4

How do I create a GUID in Python that is platform independent? I hear there is a method using ActivePython on Windows but it's Windows only because it uses COM. Is there a method using plain Python?

+39  A: 

"The uuid module, in Python 2.5 and up, provides RFC compliant UUID generation. See the module docs and the RFC for details."

http://mail.python.org/pipermail/python-list/2007-November/1106916.html

Stuart Dunkeld
Ah, fantastic. During my initial search I had looked for 'GUID' instead of 'UUID'. Thanks! :)
Jonathon Watney
hah! beat me to it by mere seconds :-D
Jay
the link is broken...
deostroll
fixed link, thanks
Stuart Dunkeld
+29  A: 

If you're using Python 2.5 or later, the uuid module is already included with the Python standard distribution.

Ex:

>>> import uuid
>>> uuid.uuid1()
UUID('5a35a426-f7ce-11dd-abd2-0017f227cfc7')
Jay
+1  A: 

Pre python 2.5 you could use something like this:

def guid( *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:
        # if we can't get a network address, just imagine one
        a = random.random()*100000000000000000L
    data = str(t)+' '+str(r)+' '+str(a)+' '+str(args)
    data = hashlib.md5(data).hexdigest()

    return data
mluebke
Just a note: hashlib exists since Python 2.5.So your pre Python 2.5 code should use data = md5.new(data).hexdigest() instead of hashlib.
Catalin Iacob
This is wrong. UUIDs have a special format; you need to set the format and version bits correctly. You can't just return random data. Also, that is a rather bad and complex random number generator; if you have Python 2.4 then I suggest you use random.SystemRandom() instead of trying to make a "random number" using IP address, time (with microsecond part thrown away), and a 56-bit pseudo-random number. UUIDs need 122 bits of good randomness, or else the chance of collisions goes up (i.e. they might not be Unique).
user9876
A: 

If all you want is a number that's extremely unlikely to be repeated, just concatenate the date, time (with microseconds) and a number from random.SystemRandom. The chances that another item was created at the exact same microsecond of the exact same day as yours is low. Add a nice long random number generated at that time, and you have some good uniqueness, imho. But, I'm no expert on randomness. I'd be interested in feedback from someone who is an expert.

Eric
actually you're wrong. concatenating date, time etc. is NOT a proper way of calculating UUID - the chance of getting the same "unique" ID is pretty high.
MaciekTalaska
Notice I also said "and a number from random.SystemRandom." The chances of CREATING THE SAME PSEUDO-RANDOM NUMBER at the same microsecond of a particular day are pretty low, assuming that number is of decent length.
Eric
I am not really convinced. We do not know what is he going to use the Guid for. Using Python's uuid.uuid1() function you get 16**8 (i.e. 16^8) combinations of Guids (4294967296). Guids generated by uuid.uuid4 have all the 32 characters 'unique' - so the total number of combinations is 'slightly' higher: 16^32 - which is 340282366920938463463374607431768211456. Now, if the Guid is to be used in a system that performs millions or even billions transactions per day the first attempt may not be sufficient. And... of course it may compromise privacy (another question of how the Guid is to be used).
MaciekTalaska