UUID yes Universal unique identification code (Universally Unique Identifier) Abbreviation , It's a standard for software construction , It is also part of the Open Software Foundation's organization in the field of distributed computing environments .
**UUID It's a sixteen digit number , In the form of
**550E8400-E29B-11D4-A716-446655440000
UUID Use... When uploading files in the project
Prefix used to generate a file name , The prefix generated for each request will be different , Thus, the newly uploaded file will not overwrite the old file because of the same name .
Because the interface in my work needs to be transmitted uuid, So you need to encapsulate an acquisition uuid Methods
stay python Can be found in UUID How to use
python Medium uuid Modules are based on information such as MAC Address 、 Time stamp 、 Namespace 、 random number 、 Pseudo random numbers uuid.
See the following methods in the source code :
uuid.uuid1()
be based on MAC Address , Time stamp , Random numbers to generate unique uuid, It can guarantee the uniqueness in the world .
uuid.uuid2()
Algorithm and uuid1 identical , The difference is to put the time stamp before 4 Change the position to POSIX Of UID. But here's the thing python There is no basis for DCE The algorithm of , therefore python Of uuid Not in module uuid2 This method .
uuid.uuid3(namespace,name)
By calculating a namespace and name of md5 Hash value to give a uuid, So you can make sure that different names in the namespace have different uuid, But the same name is the same uuid 了 .
namespace It's not a string or other quantity that you manually specify , But in uuid Some values given in the module itself . such as uuid.NAMESPACE_DNS,uuid.NAMESPACE_OID,uuid.NAMESPACE_OID These values . These values themselves are UUID object , Calculated according to certain rules .
uuid.uuid4()
By pseudo-random number uuid, There is a certain probability of repetition
uuid.uuid5(namespace,name)
and uuid3 Basically the same , But the hash algorithm used is sha1
generally speaking , In the face of uuid When your needs are not very complex ,uuid1 The method is enough , How to use it is as follows :
import uuid
class UUID:
def get_uuid(self):
# name = 'test_name'
# namespace = 'test_namespace'
#namespace = uuid.NAMESPACE_URL
return uuid.uuid1()
Back to UUID Code similar to this