GUID.py

From PeformIQ Upgrade
Jump to navigation Jump to search

From - http://code.activestate.com/recipes/163604/

#!/usr/bin/env python

# GUID.py
# Version 2.6
#
# Copyright (c) 2006 Conan C. Albrecht
#
# Permission is hereby granted, free of charge, to any person obtaining a copy 
# of this software and associated documentation files (the "Software"), to deal 
# in the Software without restriction, including without limitation the rights 
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is furnished 
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all 
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.



##################################################################################################
###   A globally-unique identifier made up of time and ip and 8 digits for a counter: 
###   each GUID is 40 characters wide
###
###   A globally unique identifier that combines ip, time, and a counter.  Since the 
###   time is listed first, you can sort records by guid.  You can also extract the time 
###   and ip if needed.  
###
###   Since the counter has eight hex characters, you can create up to 
###   0xffffffff (4294967295) GUIDs every millisecond.  If your processor
###   is somehow fast enough to create more than that in a millisecond (looking
###   toward the future, of course), the function will wait until the next
###   millisecond to return.
###     
###   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.
###
###   There is an Internet Draft for UUIDs, but this module does not implement it.
###   If the draft catches on, perhaps I'll conform the module to it.
###


# Changelog
# Sometime, 1997     Created the Java version of GUID
#                    Went through many versions in Java
# Sometime, 2002     Created the Python version of GUID, mirroring the Java version
# November 24, 2003  Changed Python version to be more pythonic, took out object and made just a module
# December 2, 2003   Fixed duplicating GUIDs.  Sometimes they duplicate if multiples are created
#                    in the same millisecond (it checks the last 100 GUIDs now and has a larger random part)
# December 9, 2003   Fixed MAX_RANDOM, which was going over sys.maxint
# June 12, 2004      Allowed a custom IP address to be sent in rather than always using the 
#                    local IP address.  
# November 4, 2005   Changed the random part to a counter variable.  Now GUIDs are totally 
#                    unique and more efficient, as long as they are created by only
#                    on runtime on a given machine.  The counter part is after the time
#                    part so it sorts correctly.
# November 8, 2005   The counter variable now starts at a random long now and cycles
#                    around.  This is in case two guids are created on the same
#                    machine at the same millisecond (by different processes).  Even though
#                    it is possible the GUID can be created, this makes it highly unlikely
#                    since the counter will likely be different.
# November 11, 2005  Fixed a bug in the new IP getting algorithm.  Also, use IPv6 range
#                    for IP when we make it up (when it's no accessible)
# November 21, 2005  Added better IP-finding code.  It finds IP address better now.
# January 5, 2006    Fixed a small bug caused in old versions of python (random module use)

import math
import socket
import random
import sys
import time
import threading



#############################
###   global module variables

#Makes a hex IP from a decimal dot-separated ip (eg: 127.0.0.1)
make_hexip = lambda ip: ''.join(["%04x" % long(i) for i in ip.split('.')]) # leave space for ip v6 (65K in each sub)
  
MAX_COUNTER = 0xfffffffe
counter = 0L
firstcounter = MAX_COUNTER
lasttime = 0
ip = ''
lock = threading.RLock()
try:  # only need to get the IP addresss once
  ip = socket.getaddrinfo(socket.gethostname(),0)[-1][-1][0]
  hexip = make_hexip(ip)
except: # if we don't have an ip, default to someting in the 10.x.x.x private range
  ip = '10'
  rand = random.Random()
  for i in range(3):
    ip += '.' + str(rand.randrange(1, 0xffff))  # might as well use IPv6 range if we're making it up
  hexip = make_hexip(ip)

  
#################################
###   Public module functions

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() # can't generate two guids at the same time
  try:
    parts = []

    # do we need to wait for the next millisecond (are we out of counters?)
    now = long(time.time() * 1000)
    while lasttime == now and counter == firstcounter: 
      time.sleep(.01)
      now = long(time.time() * 1000)

    # time part
    parts.append("%016x" % now)

    # counter part
    if lasttime != now:  # time to start counter over since we have a different millisecond
      firstcounter = long(random.uniform(1, MAX_COUNTER))  # start at random position
      counter = firstcounter
    counter += 1
    if counter > MAX_COUNTER:
      counter = 0
    lasttime = now
    parts.append("%08x" % (counter)) 

    # ip part
    parts.append(hexip)

    # put them all together
    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'''
  # there's probably a more elegant way to do this
  thisip = []
  for index in range(24, 40, 4):
    thisip.append(str(int(guid[index: index + 4], 16)))
  return '.'.join(thisip)



### TESTING OF GUID CLASS ###
if __name__ == "__main__":
  guids = []

  for i in range(10):  # calculate very fast so people can see the counter in action
    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)


# 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 < 32:
        ip.append(str(int(self.guid[index: index + 4], 16)))
        index += 4
    return '.'.join(ip)


    def random(self):
        '''Extracts the random bits from the guid (returns the bits in decimal)'''
        return int(self.guid[32:], 16)

### TESTING OF GUID CLASS ###
if __name__ == "__main__":
    guid = GUID()
    print "GUID:", guid
    print "Time:", time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime(guid.time()))
    print "IP:  ", guid.ip()
    print "Rand:", guid.random()

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/env python
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought util package component>
#------------------------------------------------------------------------------
import warnings
warnings.warn("Module is deprecated.", DeprecationWarning)
# GUID.py
# Version 2.1.
#
# Copyright (C) 2003  Dr. Conan C. Albrecht <conan_albrechtATbyu.edu>
#
# 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



##################################################################################################
###   A globally-unique identifier made up of time and ip and 8 random digits: 40 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.
###
###   There is an Internet Draft for UUIDs, but this module does not implement it.
###   If the draft catches on, perhaps I'll conform the module to it.
###


# Changelog
# Sometime, 1997     Created the Java version of GUID
#                    Went through many versions in Java
# Sometime, 2002     Created the Python version of GUID, mirroring the Java version
# November 24, 2003  Changed Python version to be more pythonic, took out object and made just a module
# December 2, 2003   Fixed duplicating GUIDs.  Sometimes they duplicate if multiples are created
#                    in the same millisecond (it checks the last 100 GUIDs now and has a larger random part)
# December 9, 2003   Fixed MAX_RANDOM, which was going over sys.maxint
#

import random
import socket
import sys
import time
import threading

# The size of the circular queue.  Larger sizes give more assurance for uniqueness.
# Smaller sizes take less memory and are a tiny bit faster
QUEUE_SIZE = 100


#############################
###   global module variables

MAX_RANDOM = sys.maxint # converted to hex goes to 8 chars (at least, in Python 2.3)
rand = random.Random()
ip = ''
lock = threading.RLock()
lastguid = ''
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)


#######################################
###   A simple circular set
###   to ensure we don't duplicate
###   GUIDs in the same millisecond

class CircularSet:
  '''A circular set.  A set that maxes at a given size, replacing the oldest element after maximum size.
     This implementation is NOT thread safe.  (generate() below is thread safe, though)
  '''
  def __init__(self):
    self.queue = []
    self.queue_map = {} # for efficiency, we keep a map of everything
    self.queueindex = 0

  def add(self, val):
    '''Adds a value to the queue'''
    # check to see if we have this value.  If so, throw an exception
    assert not self.queue_map.has_key(val), 'This value is already in the set!'

    # add the new one to the list
    if len(self.queue) > self.queueindex:
      # first remove the previous key at this location
      del self.queue_map[self.queue[self.queueindex]]
      self.queue[self.queueindex] = val
    else:
      self.queue.append(val)

    # now add to the map for efficiency
    self.queue_map[val] = val

    # increment the queue index
    self.queueindex += 1
    if self.queueindex >= QUEUE_SIZE:
      self.queueindex = 0

queue = CircularSet()

#################################
###   Public module functions

def generate():
  '''Generates a new guid'''
  global lock, queue  # since we modify the module variable
  try:
    lock.acquire() # can't generate two guids at the same time
    while 1:
      parts = []
      # time part
      parts.append("%016x" % (long(time.time() * 1000)))
      # ip part
      parts.append(hexip)
      # random part
      parts.append(("%08x" % (rand.random() * MAX_RANDOM))[:8]) # limit to 8 chars, just in case maxint goes up in future Pythons
      # put them all together
      guid = ''.join(parts)
      try:
        queue.add(guid)  # throws the AssertionError if this GUID is a duplicate of the queue
        return guid
      except AssertionError: # signals we already have this GUID in the queue
        pass
  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


def extract_ip(guid):
  '''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 < 32:
    if ip != '':
      ip += "."
    ip += str(int(guid[index: index + 4], 16))
    index += 4
  return ip


def extract_random(guid):
  '''Extracts the random bits from the guid (returns the bits in decimal)'''
  return int(guid[32:], 16)


### TESTING OF GUID CLASS ###
if __name__ == "__main__":
  guid = generate()
  print "GUID:", guid
  print "Time:", time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime(extract_time(guid)))
  print "IP:  ", extract_ip(guid)
  print "Rand:", extract_random(guid)