UUID (Universally Unique Identifier) is a 128-bit unique identifier that is used to uniquely identify information in systems and software applications. They are especially applied widely in cryptography and hashing algorithms as well as generally where where unique ids are needed.
UUIDs are normally represented as a string of 32 hexadecimal digits separated by hyphens, such as "2a3f1c3e-7d44-4e27-8b1c-9915b801d1de".
The uuid
module in the standard library provide tools for generating UUIDs.
uuid.uuid1()
uuid1
(uuid version 1) generates UUIDs based on MAC/hardware address of the computer. To view the MAC address of the computer, we can use the uuid.getnode()
function as shown below:
import uuid
print(uuid.getnode())
The uuid1()
function uses the MAC address with a combination of letters and current time to generate the UUID. Consider the following example:
import uuid
u = uuid.uuid1()
print(u)
print("version: ", u.version)
The MAC Adress is included as the last part of the UUID in its hexadecimal form. The function allows an argument called node
which if specified, will be used instead of the MAC address.
with a custom node value
import uuid
print(uuid.uuid1(node = 0))
As you can see above, the last part of the UUID is just zeros, because we specified 0 as the node value.
UUID objects contains some useful attribute. For example, we can retrieve the MAC address, the time that was used to create th UUID and other useful attributes. All the attributes are as listed below:
Fields of uuid1()
- time_low : The first 32 bits of id.
- time_mid : next 16 bits of id.
- time_hi_version : next 16 bits of id.
- clock_seq_hi_variant : next 8 bits of id.
- clock_seq_low : next 8 bits of id.
- node : Last 48 bits of id.
- time : time used for creation of the uuid.
- clock_seq : 14 bit sequence number.
UUID attributes
import uuid
u = uuid.uuid1()
#uuid fragments
print(u.time_low)
print(u.time_mid)
print(u.time_hi_version)
print(u.clock_seq_hi_variant)
print(u.clock_seq_low)
print(u.node) #last bit
Apart from the above attributes, UUIDs stores other attributes for the representation of the uuid in other formats such as integer, bytes and hex.
import uuid
u = uuid.uuid1()
print('hex: ', u.hex)
print('int: ', u.int)
print('bytes: ', u.bytes)
To turn the UUID to string, we simply need to use the builtin str()
function with the uuid as the argument.
import uuid
u = uuid.uuid1()
print(str(u))
uuid.uuid4()
As we have seen from above examples, uuid1
is not entirely random as it uses well defined values to create the UUIDS. On the other hand, uuid4()
is entirely random. This is the method that should be used when we need random UUIDs that are not based on any pre-defined format.
import uuid
u = uuid.uuid4()
print(u)
Similarly, we can access attributes from the returned UUID as shown below.
import uuid
u = uuid.uuid4()
print('Version: ', u.version)
print('hex: ', u.hex)
print('int: ', u.int)
print('bytes: ', u.bytes)
To turn the UUID to string, simply use the builtin str()
function.
import uuid
u = uuid.uuid4()
print(u)
print(str(u))