UUID Generator in Python

A universally unique identifier (UUID) is a 128-bit number used in software systems. This article will discuss UUID generators in Python.

UUID

Let’s understand universally unique identifiers (UUID).

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. It is a standardized format, defined in the Internet Engineering Task Force’s (IETF) RFC 4122. UUIDs are unique across space and time and are designed to be unique even in the face of network and hardware changes.

UUIDs are often used as unique identifiers for resources in distributed systems, such as database records, files, and more. They are also helpful in generating unique keys for authentication, session management, and other purposes.

UUIDs are made up of four parts:

  • Time
  • Clock sequence
  • Node
  • Variant

The time is the timestamp, the clock sequence is a value used to help avoid duplicates, the node is either the MAC address of the host or a random value if the host does not have a MAC address, and the variant and version field is used to indicate the UUID version.

There are several different versions of UUIDs, each with different properties and use cases.

  • Version 1 UUIDs use the current time and the MAC address of the computer to generate the UUID.
  • The Version 4 UUIDs use random numbers to generate the UUID.
  • Version 3 and Version 5 UUIDs use a combination of a namespace and a name to generate the UUID.

Example UUID

e4eaaaf2-d142-11e1-b3e4-0800200c9a66

As you can see, a UUID is represented as a string of hexadecimal characters separated by dashes. Each UUID is made up of 32 hexadecimal digits (16 bytes) arranged into five groups separated by dashes. The groups are:

  1. 8 digits for the time_low field
  2. Next 4 digits for the time_mid field
  3. 4 digits for the time_hi_and_version field
  4. 4 digits for the clock_seq_hi_and_reserved field
  5. 12 digits for the node id

UUID generation using Python

In Python, the uuid module provides the necessary functions to work with UUIDs.

import uuid

# Generate a random UUID
random_uuid = uuid.uuid4()
print(random_uuid)

Output

8031df11-32f8-4cbe-9baf-e84b175109d3

The uuid4() function generates a random UUID.

Here are some commonly used methods for UUID generation from the uuid module:

The uuid1() function

This function generates a UUID based on the current time and the MAC address of the computer. It is guaranteed to be unique across all space and time, as long as the MAC address is unique and the clock is never set back.

import uuid

# Generate a UUID based on current time and computer's MAC address
uuid1 = uuid.uuid1()
print(uuid1)

The uuid4() function

This function generates a random UUID. The probability of generating the same UUID twice is extremely low, making it suitable for many use cases.

The uuid3(namespace, name) function

This function generates a UUID based on the specified namespace and name. The namespace should be a UUID and the name should be a string. This function is useful for generating UUIDs for specific resources, such as database records.

# Generate a UUID based on namespace and name
namespace = uuid.uuid3(uuid.NAMESPACE_URL, 'example.com')
name = 'my_resource'
uuid3 = uuid.uuid3(namespace, name)
print(uuid3)

The uuid5(namespace, name) function

This function works similarly to uuid3(), but it uses the SHA-1 algorithm to generate the UUID, which is more secure than the MD5 algorithm used by uuid3().

# Generate a UUID based on namespace and name
namespace = uuid.uuid5(uuid.NAMESPACE_URL, 'example.com')
name = 'my_resource'
uuid5 = uuid.uuid5(namespace, name)
print(uuid5)

UUID to string conversion

We can convert UUID to string as shown below.

# Convert UUID to string
string_uuid = str(uuid4)
print(string_uuid)

String to UUID conversion

We can convert the string to UUID as shown below.

# Convert string to UUID
converted_uuid = uuid.UUID(string_uuid)
print(converted_uuid)

Conclusion

In conclusion, the uuid module in Python provides several ways to generate UUIDs, each with different properties and use cases. You can choose the appropriate method based on your specific requirements, whether it’s based on the current time and MAC address, a random number, or a specific namespace and name.