1. Knowledge background
The UUID is a globally unique identifier of 128 bits, usually with a 32-byte letter representation. It can guarantee the uniqueness of time and space.
Uuid--universally Unique identifier is called UUID in Python and becomes a GUID in C # (globally unique identifier)
It guarantees the uniqueness of the generation ID by MAC address, timestamp, namespace, random number, pseudo-random number.
2. Why should I use UUID?
Many scenarios require an ID, but do not require this ID to have a specific meaning, just to identify an object. A common example is the ID field of a database table. Another example is the various UI libraries at the front end, because they often need to dynamically create various UI elements that require a unique ID, which requires the use of a UUID.
3. Python3 's UUID module: This module provides immutable UUID objects (UUID classes) and Functions Uuid1 (), UUID3 (), Uuid4 (), UUID5 () to generate 1, 3,4,5 each version of the UUID (Python does not uuid2 this function).
Some of the most commonly used methods in the UUID module are summarized as follows:
UUID1 ()--based on timestamp
UUID.UUID1 ([node[, Clock_seq]])
Using the host ID, serial number, and the current time to generate the UUID can guarantee global uniqueness, which can be a danger of privacy when the UUID generated by the method contains the host's network address. The function's node parameter is specified, and the system automatically calls the GetNode () function to obtain the host's hardware address. If the parameter clock_seq is unspecified, the system replaces it with a randomly generated 14-bit sequence number.
UUID3 ()--based on name and MD5 hash value
Uuid.uuid3 (namespace, name)
By calculating the MD5 hash value of the namespace and the name to generate the UUID, you can guarantee the uniqueness of different names in the same namespace and the uniqueness of different namespaces, with the same name generating the same UUID for the same namespace.
Uuid4 ()--based on random numbers
Uuid.uuid4 ()
The use of random numbers to generate UUID, using pseudo-random number, there is a certain repetition probability
UUID5 ()--SHA-1 hash value based on name
Uuid.uuid5 (namespace, name)
The UUID is generated by calculating the SHA-1 hash value of the namespace and the name, which is the same as UUID.UUID3 ().
Sample code
import uuid print (UUID.UUID1 ()) print (UUID.UUID3 (UUID). Namespace_dns, " testme " print (Uuid.uuid4 ()) print (The UUID.UUID5 (UUID). Namespace_dns, " testme " )) x = uuid. UUID ( " {00010203-0405-0607-0809-0a0b0c0d0e0f} " ) print (str (x))
Run results (XXX is manually changed):
/usr/bin/python3.5/home/rxf/python3_1000/1000/python3_server/python3_uuid/demo1.pyxxxx- c456-11e7-923c-xxxxxxxxxx-50fd-36f7-bbeb-1c739ab40b8cxxxxx-258a-4207-b52d- B01b1cec0985xxxxx-a443-578c-8754-856842a2f98d00010203-0405-0607-0809-0a0b0c0d0e0f
Process finished with exit code 0
See there are suggestions for the use of predecessors: first of all, Python is not based on DCE, so UUID2 can be ignored, and Secondly, there is a probabilistic repetition of uuid4, by no mapping, preferably not, again, if in the global Distributed computing environment, It is best to use UUID1; Finally, if the uniqueness of the name requires, it is best to use UUID3 or UUID5.
Reference:
UUID algorithm: https://tools.ietf.org/html/rfc4122.html
Python_uuid Module Reference manual: Https://docs.python.org/3/library/uuid.html
Python3_uuid Module Detailed