2

am planning to create a unique ID from characters and numbers as well. The ID should look something like this

XXXX-XXXX-XXXX-XXXX

where X is a combination of numbers, small and cap letters. This Id will be stored in a mysql database and will be incremented to maintain consistency of data.

Any advice on how to start?

ismail
  • 46,010
  • 9
  • 86
  • 95
Mo J. Mughrabi
  • 6,747
  • 16
  • 85
  • 143
  • This looks similar to http://stackoverflow.com/questions/534839/how-to-create-a-guid-in-python and the next logical question might be related to http://stackoverflow.com/questions/412341/how-should-i-store-guid-in-mysql-tables regarding storing GUIDs in MySQL. – Demitrius Nelon Dec 29 '10 at 15:48

2 Answers2

15

Use the uuid module;

>>> import uuid

# make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

# make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

# make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

# make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

Your 4-character requirement can be emulated with;

>>> "-".join([x[:4].upper() for x in str(uuid.uuid4()).split("-")])
'C984-EE70-4BFD-9963-9BB9'
>>> "-".join([x[:4].upper() for x in str(uuid.uuid4()).split("-")])
'30D7-4356-493E-B887-2671
ismail
  • 46,010
  • 9
  • 86
  • 95
3

Though it's a different format than you specified, have you considered using a UUID? Python has a uuid module, starting in 2.5.

http://docs.python.org/release/2.5.2/lib/module-uuid.html

Brian Clapper
  • 25,705
  • 7
  • 65
  • 65