Advertisement
iLq

create_guid in SugarCRM

iLq
May 22nd, 2013
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. def create_guid():
  2.     microTime = microtime()
  3.     a_dec, a_sec = microTime.split(' ')
  4.  
  5.     dec_hex = '%x' % float(a_dec) * 1000000
  6.     sec_hex = '%x' % int(a_sec)
  7.  
  8.     dec_hex = ensure_length(dec_hex, 5)
  9.     sec_hex = ensure_length(sec_hex, 6)
  10.  
  11.     guid = ''
  12.     guid += dec_hex
  13.     guid += create_guid_section(3)
  14.     guid += '-'
  15.     guid += create_guid_section(4)
  16.     guid += '-'
  17.     guid += create_guid_section(4)
  18.     guid += '-'
  19.     guid += create_guid_section(4)
  20.     guid += '-'
  21.     guid += sec_hex
  22.     guid += create_guid_section(6)
  23.     return guid
  24.  
  25.  
  26. def microtime(get_as_float = False) :
  27.     if get_as_float:
  28.         return time.time()
  29.     else:
  30.         return '%f %d' % math.modf(time.time())
  31.  
  32.  
  33. def ensure_length(string, length):
  34.     strlen = len(string)
  35.     if strlen < length:
  36.         string = string.ljust(length, '0')
  37.     else:
  38.         string = string[:length]
  39.     return string
  40.  
  41.  
  42. def create_guid_section(characters):
  43.     return ''.join(['%x' % random.randint(0, 15) for el in range(characters)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement