Python Standard Library --UUID

Original link: https://www.jianshu.com/u/8f2987e2f9fb

UUID (Universally Unique Identifier) ​​is a 128-bit universal unique identifier, typically represented by a 32-byte string. It can guarantee the uniqueness of time and space, also known as GUID, full name: UUID - Universally Unique IDentifier, Python called the UUID.

Logo used in many fields, such as our common database can use it as the primary key, in principle it is possible to carry out a unique code for anything.

It MAC address, time stamp, a namespace, a random number, pseudo-random numbers to guarantee the uniqueness of the generated ID.
Here is a brief description of how to generate a UUID python:
python has a module called the uuid, you can use it to import the four methods of. Note that these four methods followed uuid1 (), uuid3 (), uuid4 (), uuid5 (),
however, did not uuid2 ().

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#! coding:utf-8
import uuid
print u"uuid1  生成基于计算机主机ID和当前时间的UUID"
print uuid.uuid1() # UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
  
print u"\nuuid3  基于命名空间和一个字符的MD5加密的UUID"
print uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') #UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
  
print u"\nuuid4  随机生成一个UUID"
print uuid.uuid4()       #'16fd2706-8baf-433b-82eb-8c7fada847da'
  
print u"\nuuid5  基于命名空间和一个字符的SHA-1加密的UUID"
uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') #UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
  
print u"\n根据十六进制字符生成UUID"
x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
print u"转换成十六进制的UUID表现字符"
print str(x)       # '00010203-0405-0607-0809-0a0b0c0d0e0f'

At first glance all 36 characters, then they What is the difference in the end, one analysis below.

uuid1 (): This is generated based on the current timestamp and MAC address, the last 12 characters 408d5c985711 corresponding MAC address is, because it is the MAC address, then the uniqueness should not have said. But after exposure to generate a MAC address which is very bad.

uuid3 (): namespace specific strings inside and we are specified, then what ??? should be generated by the MD5, which we rarely used, inexplicable feeling.

uuid4 (): This is based on uuid random number, since it is random, there may really encounter the same, but this is like winning like, ultra-small probability, because it is random but also easy to use, so the use of this , or more.

uuid5 (): This looks uuid3 () looks no different, as written, is to specify the namespace and strings by the user, but not here with hash MD5, but SHA1.

Here again it is about a simple process, UUID middle '-' is a rather strange character, you should get rid of it, this is actually super simple:

uid = str(uuid.uuid4())
suid = ''.join(uid.split('-'))

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/100541975
Recommended