|
|
 |
|
Title: GUID
Submitter: Conan Albrecht
(other recipes)
Last Updated: 2006/01/05
Version no: 2.3
Category:
Databases
|
|
6 vote(s)
|
|
|
|
Description:
A globally unique identifier that combines ip, time, and random bits. Since the time is listed first, you can sort records by guid. You can also extract the time and ip if needed. GUIDs make wonderful database keys. They require no access to the database (to get the max index number), they are extremely unique, and they sort automatically by time.
Source: Text Source
import math
import socket
import random
import sys
import time
import threading
make_hexip = lambda ip: ''.join(["%04x" % long(i) for i in ip.split('.')])
MAX_COUNTER = 0xfffffffe
counter = 0L
firstcounter = MAX_COUNTER
lasttime = 0
ip = ''
lock = threading.RLock()
try:
ip = socket.getaddrinfo(socket.gethostname(),0)[-1][-1][0]
hexip = make_hexip(ip)
except:
ip = '10'
rand = random.Random()
for i in range(3):
ip += '.' + str(rand.randrange(1, 0xffff))
hexip = make_hexip(ip)
def generate(ip=None):
'''Generates a new guid. A guid is unique in space and time because it combines
the machine IP with the current time in milliseconds. Be careful about sending in
a specified IP address because the ip makes it unique in space. You could send in
the same IP address that is created on another machine.
'''
global counter, firstcounter, lasttime
lock.acquire()
try:
parts = []
now = long(time.time() * 1000)
while lasttime == now and counter == firstcounter:
time.sleep(.01)
now = long(time.time() * 1000)
parts.append("%016x" % now)
if lasttime != now:
firstcounter = long(random.uniform(1, MAX_COUNTER))
counter = firstcounter
counter += 1
if counter > MAX_COUNTER:
counter = 0
lasttime = now
parts.append("%08x" % (counter))
parts.append(hexip)
return ''.join(parts)
finally:
lock.release()
def extract_time(guid):
'''Extracts the time portion out of the guid and returns the
number of seconds since the epoch as a float'''
return float(long(guid[0:16], 16)) / 1000.0
def extract_counter(guid):
'''Extracts the counter from the guid (returns the bits in decimal)'''
return int(guid[16:24], 16)
def extract_ip(guid):
'''Extracts the ip portion out of the guid and returns it
as a string like 10.10.10.10'''
thisip = []
for index in range(24, 40, 4):
thisip.append(str(int(guid[index: index + 4], 16)))
return '.'.join(thisip)
if __name__ == "__main__":
guids = []
for i in range(10):
guid = generate()
guids.append(guid)
for guid in guids:
print "GUID:", guid
guidtime = extract_time(guid)
print "\tTime: ", time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime(guidtime)), '(millis: ' + str(round(guidtime - long(guidtime), 3)) + ')'
print "\tIP: ", extract_ip(guid)
print "\tCounter:", extract_counter(guid)
Discussion:
This version uses a counter instead of random bits and is released under the MIT license. It is faster and should make for better GUIDs. I did not incorporate using a class as is done in the example below because I like GUIDs to remain simple strings. However, you could easily modify this new version to be a class if you prefer that method.
|
|
Add comment
|
|
Number of comments: 14
Very good but I would add format checking and __eq__..., Rodrigo Oliveira, 2002/11/22
Here's a possibly improved version with basic GUID format checking, __eq__ implementation and a more pythonic ip implementation. Hope you like it.
thanks for sharing.
#!/usr/bin/python
# A globally unique identifier made up of time and ip
# Copyright (C) 2002 Dr. Conan C. Albrecht
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import random
import socket
import time
class GUID:
'''A globally-unique identifier made up of time and ip and 3 random digits: 35 characters wide
A globally unique identifier that combines ip, time, and random bits. Since the
time is listed first, you can sort records by guid. You can also extract the time
and ip if needed.
GUIDs make wonderful database keys. They require no access to the
database (to get the max index number), they are extremely unique, and they sort
automatically by time. GUIDs prevent key clashes when merging
two databases together, combining data, or generating keys in distributed
systems.
'''
rand = random.Random()
ip = ''
try:
ip = socket.gethostbyname(socket.gethostname())
except (socket.gaierror): # if we don't have an ip, default to someting in the 10.x.x.x private range
ip = '10'
for i in range(3):
ip += '.' + str(rand.randrange(1, 254))
hexip = ''.join(["%04x" % long(i) for i in ip.split('.')]) # leave space for ip v6 (65K in each sub)
lastguid = ''
def __init__(self, guid=None):
'''Constructor. Use no args if you want the guid generated (this is the normal method)
or send a string-typed guid to generate it from the string'''
if guid is None:
self.guid = self.__class__.lastguid
while self.guid == self.__class__.lastguid:
# time part
now = long(time.time() * 1000)
self.guid = ("%016x" % now) + self.__class__.hexip
# random part
self.guid += ("%03x" % (self.__class__.rand.randrange(0, 4095)))
self.__class__.lastguid = self.guid
elif type(guid) == type(self): # if a GUID object, copy its value
self.guid = str(guid)
else: # if a string, just save its value
assert self._check(guid), guid + " is not a valid GUID!"
self.guid = guid
def __eq__(self, other):
'''Return true if both GUID strings are equal'''
if isinstance(other, self.__class__):
return str(self) == str(other)
return 0
def __str__(self):
'''Returns the string value of this guid'''
return self.guid
def time(self):
'''Extracts the time portion out of the guid and returns the
number of milliseconds since the epoch'''
return long(self.guid[0:16], 16)
def ip(self):
'''Extracts the ip portion out of the guid and returns it
as a string like 10.10.10.10'''
# there's probably a more elegant way to do this
ip = []
index = 16
while index Here's a possibly improved version with basic GUID format checking, __eq__ implementation and a more pythonic ip implementation. Hope you like it.
thanks for sharing.
#!/usr/bin/python
# A globally unique identifier made up of time and ip
# Copyright (C) 2002 Dr. Conan C. Albrecht
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import random
import socket
import time
class GUID:
'''A globally-unique identifier made up of time and ip and 3 random digits: 35 characters wide
A globally unique identifier that combines ip, time, and random bits. Since the
time is listed first, you can sort records by guid. You can also extract the time
and ip if needed.
GUIDs make wonderful database keys. They require no access to the
database (to get the max index number), they are extremely unique, and they sort
automatically by time. GUIDs prevent key clashes when merging
two databases together, combining data, or generating keys in distributed
systems.
'''
rand = random.Random()
ip = ''
try:
ip = socket.gethostbyname(socket.gethostname())
except (socket.gaierror): # if we don't have an ip, default to someting in the 10.x.x.x private range
ip = '10'
for i in range(3):
ip += '.' + str(rand.randrange(1, 254))
hexip = ''.join(["%04x" % long(i) for i in ip.split('.')]) # leave space for ip v6 (65K in each sub)
lastguid = ''
def __init__(self, guid=None):
'''Constructor. Use no args if you want the guid generated (this is the normal method)
or send a string-typed guid to generate it from the string'''
if guid is None:
self.guid = self.__class__.lastguid
while self.guid == self.__class__.lastguid:
# time part
now = long(time.time() * 1000)
self.guid = ("%016x" % now) + self.__class__.hexip
# random part
self.guid += ("%03x" % (self.__class__.rand.randrange(0, 4095)))
self.__class__.lastguid = self.guid
elif type(guid) == type(self): # if a GUID object, copy its value
self.guid = str(guid)
else: # if a string, just save its value
assert self._check(guid), guid + " is not a valid GUID!"
self.guid = guid
def __eq__(self, other):
'''Return true if both GUID strings are equal'''
if isinstance(other, self.__class__):
return str(self) == str(other)
return 0
def __str__(self):
'''Returns the string value of this guid'''
return self.guid
def time(self):
'''Extracts the time portion out of the guid and returns the
number of milliseconds since the epoch'''
return long(self.guid[0:16], 16)
def ip(self):
'''Extracts the ip portion out of the guid and returns it
as a string like 10.10.10.10'''
# there's probably a more elegant way to do this
ip = []
index = 16
while index
Add comment
Why I don't use the class version, Conan Albrecht, 2005/11/04
Rodrigo -- nice class version. I just modified my original code recipe so yours needs to be updated if you like my new changes. The reason I don't use a class as you suggest is because potentially thousands of GUIDs are loaded from the DB at a time. Your class constructor has to parse the GUID every time it is created, which adds processing time. In addition, you then convert it back to a string to compare, which adds more time. Keeping it as a string doesn't require any additional processing time at create. Yours could actually be modified to compare the raw numbers instead of the string representation (which would be more efficient), but I still think it's easier to keep it as a string from the start. In the end it's just preference.
Add comment
Changes, Conan Albrecht, 2002/11/25
Rodrigo -- Your code was cut off in the posting. Please send me the code to conan_albrecht@byu.edu and I'll integrate your changes back into the original and repost a new version. Thanks.
In fact, if others have more "pythonic" ways of doing things, let me know. I'm much more mature in other languages and relatively new to python.
Add comment
Updated the code, Conan Albrecht, 2003/11/24
I updated the code based upon reviews from many users. The main difference is the new GUID code simply generates a regular String object, rather than a GUID object. Why did I create a separate object? It seemed everyone was using GUIDs as strings anyway, so I removed the class and made functions instead.
The class now respects multiple threads, and it has been simplified somewhat.
Add comment
int overflow under Python 2.2, Ulrich Hoffmann, 2003/12/06
Under Python 2.2 the phrase import random; random.Random().randrange(0, 4294967296L)
fails with
Traceback (most recent call last):
File "", line 1, in ?
File "/usr/local/lib/python2.2/random.py", line 294, in randrange
istop = int(stop)
OverflowError: long int too large to convert to int
while import random; random.Random().randrange(0, 4294967296L/2-1) succeeds.
Python 2.3 runs both successfully.
Maybe line 127
guid += ("%08x" % rand.randrange(0, 4294967296L)))
should better read
guid += ("%08x" % rand.randrange(0, 2147483647L))
to enable Python cross-version compatibility.
Add comment
Alternate version using descending sequence instead of random, Robert Brewer, 2003/12/08
Instead of random bits, I use a descending sequence. This uses a simple generator to start that segment at sys.maxint, decrementing by one on each call. 2.2 or later.
import math, sys
def _unique_sequencer():
_XUnit_sequence = sys.maxint
while 1:
yield _XUnit_sequence
_XUnit_sequence -= 1
if _XUnit_sequence <= 0:
_XUnit_sequence = sys.maxint
_uniqueid = _unique_sequencer()
def uniqueid(prefix=''):
frac, secs = math.modf(time.time())
days, remain = divmod(secs, 86400)
id = _uniqueid.next()
return u"%s%s%s_%s" % (prefix, hex(int(days))[2:],
hex(int(remain))[2:], hex(id)[2:])
Add comment
Recipe now uses counter, Conan Albrecht, 2005/11/04
The recipe above now uses a variation of your method here. I use an increasing counter so its easier to sort them (why use a decreasing counter?), but otherwise I think this idea is now in the main recipe.
Add comment
Another implementation with MIT-license is part of Pyro, Irmen de Jong, 2004/05/08
For another GUID-generator, but licensed under a very liberal MIT software license, look in the Pyro.util package of Pyro.
(http://pyro.sourceforge.net).
Add comment
Random GUIDs, Oren Tirosh, 2005/06/06
16 bytes from a cryptographic-quality random number source like os.urandom() are just as good as a method of generating GUIDs.
Add comment
Best way to store in PostgreSql, Troy Kruthoff, 2005/11/30
Is it possible to convert the string to a number value and store in a NUMERIC type field in PostgreSql?
Add comment
If you have pywin32, Wai Yip Tung, 2006/01/13
>>> import pythoncom
>>> print pythoncom.CreateGuid()
{600AFA7C-E537-424B-8EE2-A54A18102EFA}
Add comment
Alex Greif, 2006/09/06
the ip arument in the generate() method is never used. could you please fix it?
Add comment
"GUID.py:92: FutureWarning: hex/oct constants > sys.maxint...", Daryl Spitzer, 2007/02/21
I get "GUID.py:92: FutureWarning: hex/oct constants > sys.maxint will return positive values in Python 2.4 and up" from Python 2.3.5 (which is pre-installed on Mac OS X 10.4), and the counter seems to always be 0!
I changed "MAX_COUNTER = 0xfffffffe" to "MAX_COUNTER = sys.maxint" which fixes the problem.
Add comment
Use the uuid module instead, George Reilly, 2008/07/02
An industrial-strength uuid module is included in python 2.5+. 2.3 and 2.4 users can get the module from http://zesty.ca/python/uuid.html
Add comment
|
|
|
|
|
 |
|