Python Standard library--uuid (Generate unique ID) __python

Source: Internet
Author: User

Sometimes we post bar in Baidu, in a website, save some pictures on the page, the picture name is sometimes a string of very long numbers and letters, but without exception, the image will not appear duplicate. This unique ID, generally obtained by means of a UUID, is based on the number of seconds experienced from January 1, 1970 to the present. 1. The basic

The UUID is a 128-bit globally unique identifier, typically represented by a 32-byte string (hexadecimal).

It can guarantee the uniqueness of time and space, also known as GUIDs, which are all called: uuid--universally unique IDentifier Python called UUID guid--globally unique IDentifier C # Guid

It guarantees the uniqueness of the generation ID by MAC address, timestamp, namespace, random number, pseudo-random number.

The UUID has five main algorithms, namely five methods to achieve:

1, UUID1 ()--based on time stamp

Generated by MAC address (host Physical address), current timestamp, random number. can guarantee the uniqueness of the world,
But the use of Mac also brings security problems, the LAN can use IP to replace the Mac.

Note that uuid1 () returns not a normal string, but rather a UUID object that contains rich member functions and variables.

2, Uuid2 ()--DCE (without this function in Python) based on distributed computing environment

The algorithm is the same as UUID1, which replaces the first 4 bits of the timestamp with the POSIX UID.
This method is rarely used in practice.

3, UUID3 ()--MD5 hash value based on name

The MD5 hash of the name and namespace is worthwhile, guaranteeing the uniqueness of different names in the same namespace,
and uniqueness of different namespaces, but the same name of the same namespace generates the same UUID.

4, Uuid4 ()--based on random number

by pseudo random number, there is a certain probability of repetition, which can be calculated.

5, UUID5 ()--SHA-1 hash value based on name

The algorithm is the same as the UUID3, and uses the Secure Hash algorithm 1 algorithm 2. Actual Combat

Use aspect:

First of all, there is no DCE based in Python, so uuid2 can be ignored;
Secondly, there is a probability of duplication, by uuid4, preferably not;
Thirdly, if the global distributed computing environment, it is best to use UUID1;
Finally, it is best to use UUID3 or UUID5 if the uniqueness of the name is required.

Import UUID

name = "Test_name"
namespace = "Test_namespace"

print Uuid.uuid1 ()  # method with parameters see Python Doc Print
uuid.uuid3 (namespace, name)
print uuid.uuid4 ()
print uuid.uuid5 (namespace, name)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.