Python Tutorial

Showing posts with label UUID. Show all posts
Showing posts with label UUID. Show all posts

Sunday, January 20, 2013

Python UUID

Python uuid generates unique id.
uuid1 and uuid4 generates random id. uuid3 and uuid 5 generate a UUID based on the SHA-1 hash of a namespace identifier (UUID) and a name (string).

All source code available on github

import uuid

print uuid.uuid1()
print "[Hex]",uuid.uuid1().hex
print uuid.uuid4()
print "[Hex]",uuid.uuid4().hex
print uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
print "[Hex]",uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org').hex
print uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
print "[Hex]",uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org').hex

Output:
9bfb379e-62ae-11e2-81fb-4c809312d053
[Hex] 9bfc702162ae11e2a2504c809312d053
cf0be0b2-8eed-4b87-8b42-03679d8c1c94
[Hex] dd3162cb5e454dc3976a1769ef604742
6fa459ea-ee8a-3ca4-894e-db77e160355e
[Hex] 6fa459eaee8a3ca4894edb77e160355e
886313e1-3b8a-5372-9b90-0c9aee199e5d
[Hex] 886313e13b8a53729b900c9aee199e5d