uuid Python standard library - UUID module (generate a unique identity)

Python standard library - UUID module (generate a unique identity)

What is the UUID:

  UUID: Universally Unique Identifier (Universally Unique Identifier), all of which can be guaranteed to be unique UUID in space and time, also called the GUID, full name:

  • UUID - Universally Unique IDentifier Python called UUID
  • GUID - Globally Unique IDentifier C # called GUID

It is the MAC address, time stamp, a namespace, a random number, pseudo-random numbers to guarantee the uniqueness ,, has a fixed size (128 bit position), the string is typically generated by a 32-byte ID (in hexadecimal ) represents.

        Its unique characteristics and consistency, so that the registration process may not need to be able to generate a new UUID; UUID can be used for many purposes, may be used to mark an object in a short time and to be reliable in discriminating network persistent objects.

 

UUID what's the use?

  Many scenarios require a id, but this id has not requested a specific meaning, merely used to identify an object. Common use have an id field in the database table; Another example is the tip of the various UI libraries, because they usually need to dynamically create various UI elements that requires a unique id, this time you need to use a UUID. For example: a Web site when the file is stored video, pictures and other formats, naming these files can be generated using random UUID identifier to avoid duplicate names.

 

UUID UUID classes and functions provided by the module

        UUID classes and functions uuid1 python is uuid module provides (), uuid3 (), uuid4 (), uuid5 () is generated, 3, 4, 5 of each version of the UUID. 1 (Note that: python No UUID2 () this function).

Uuid module for several functions most commonly used are summarized below:

1. uuid.uuid1 ([node [, clock_seq]]) - based on the timestamps

, The current time stamp, a random number generated by the MAC address (host physical address). Can guarantee the uniqueness of the globe,
but the MAC used simultaneously introduce a security issue, you can use the LAN IP instead of MAC.

This function takes two parameters, if the node parameter is not specified, the system will automatically call getNode () function to obtain the hardware address of the host system 14 will use a randomly generated sequence number clock_seq If the parameter is not specified instead.

Note: UUID1 no ordinary string returned (), but a uuid object within its rich member functions and variables.

2. uuid.uuid2 () - based distributed computing environment DCE (Python not function)

Same algorithm and uuid1, except that the former 4 is changed to the position of the time stamp of the UID POSIX.
This method is rarely used in practice.

3. uuid.uuid3 (namespace, name) - Name-based MD5 hash value

By calculating the MD5 hash name and namespace worth to ensure that the same namespace uniqueness, different names
unique and different namespaces, but the same name in the same namespace generate the same uuid.

4. uuid.uuid4 () - based on a random number

Obtained from the pseudo-random number, there is a certain probability of repetition, the probabilities can be calculated.

5. uuid.uuid5 () - based on the name of the SHA-1 hash values

Same algorithm and uuid3, except that Secure Hash Algorithm 1 Algorithm

 

Several methods use the above-described function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
>>> import uuid # 导入UUID模块

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

Example:

First of all, Python is not DCE-based, so uuid2 can be ignored;
secondly, uuid4 existence probability of repetition, not by the mapping of, not the best;
again, if the computing environment in Global distributed, preferably with uuid1;
and finally, if the name unique requirements, preferably with uuid3 or uuid5.

1
2
3
4
5
6
7
8
import uuid
name = "test_name"
namespace = "test_namespace"

print uuid.uuid1()  # 带参的方法参见Python Doc
print uuid.uuid3(namespace, name)
print uuid.uuid4()
print uuid.uuid5(namespace, name)

 

uuid module documentation: https: //docs.python.org/2/library/uuid.html

What is the UUID:

  UUID: Universally Unique Identifier (Universally Unique Identifier), all of which can be guaranteed to be unique UUID in space and time, also called the GUID, full name:

  • UUID - Universally Unique IDentifier Python called UUID
  • GUID - Globally Unique IDentifier C # called GUID

It is the MAC address, time stamp, a namespace, a random number, pseudo-random numbers to guarantee the uniqueness ,, has a fixed size (128 bit position), the string is typically generated by a 32-byte ID (in hexadecimal ) represents.

        Its unique characteristics and consistency, so that the registration process may not need to be able to generate a new UUID; UUID can be used for many purposes, may be used to mark an object in a short time and to be reliable in discriminating network persistent objects.

 

UUID what's the use?

  Many scenarios require a id, but this id has not requested a specific meaning, merely used to identify an object. Common use have an id field in the database table; Another example is the tip of the various UI libraries, because they usually need to dynamically create various UI elements that requires a unique id, this time you need to use a UUID. For example: a Web site when the file is stored video, pictures and other formats, naming these files can be generated using random UUID identifier to avoid duplicate names.

 

UUID UUID classes and functions provided by the module

        UUID classes and functions uuid1 python is uuid module provides (), uuid3 (), uuid4 (), uuid5 () is generated, 3, 4, 5 of each version of the UUID. 1 (Note that: python No UUID2 () this function).

Uuid module for several functions most commonly used are summarized below:

1. uuid.uuid1 ([node [, clock_seq]]) - based on the timestamps

, The current time stamp, a random number generated by the MAC address (host physical address). Can guarantee the uniqueness of the globe,
but the MAC used simultaneously introduce a security issue, you can use the LAN IP instead of MAC.

This function takes two parameters, if the node parameter is not specified, the system will automatically call getNode () function to obtain the hardware address of the host system 14 will use a randomly generated sequence number clock_seq If the parameter is not specified instead.

Note: UUID1 no ordinary string returned (), but a uuid object within its rich member functions and variables.

2. uuid.uuid2 () - based distributed computing environment DCE (Python not function)

Same algorithm and uuid1, except that the former 4 is changed to the position of the time stamp of the UID POSIX.
This method is rarely used in practice.

3. uuid.uuid3 (namespace, name) - Name-based MD5 hash value

By calculating the MD5 hash name and namespace worth to ensure that the same namespace uniqueness, different names
unique and different namespaces, but the same name in the same namespace generate the same uuid.

4. uuid.uuid4 () - based on a random number

Obtained from the pseudo-random number, there is a certain probability of repetition, the probabilities can be calculated.

5. uuid.uuid5 () - based on the name of the SHA-1 hash values

Same algorithm and uuid3, except that Secure Hash Algorithm 1 Algorithm

 

Several methods use the above-described function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
>>> import uuid # 导入UUID模块

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

Example:

First of all, Python is not DCE-based, so uuid2 can be ignored;
secondly, uuid4 existence probability of repetition, not by the mapping of, not the best;
again, if the computing environment in Global distributed, preferably with uuid1;
and finally, if the name unique requirements, preferably with uuid3 or uuid5.

1
2
3
4
5
6
7
8
import uuid
name = "test_name"
namespace = "test_namespace"

print uuid.uuid1()  # 带参的方法参见Python Doc
print uuid.uuid3(namespace, name)
print uuid.uuid4()
print uuid.uuid5(namespace, name)

 

uuid module documentation: https: //docs.python.org/2/library/uuid.html

Guess you like

Origin www.cnblogs.com/plusUltra/p/11348077.html