Skip to content

UUID API

This module defines a custom UUID class that validates 16-digit hexadecimal strings and provides an associated utility function to generate random UUIDs.

Classes:

Name Description
UUID

Inherits from str and validates that a given input is a 16-character hexadecimal string. It includes a strict regular expression pattern for validation.

Methods (of UUID): new(cls, value: str) -> UUID: Validates and constructs a UUID object from the given 16-character hexadecimal string.

Attributes (of UUID): _pattern (Pattern): A compiled regex pattern for validating 16-character hexadecimal strings.

Functions:

Name Description
generate_random_uuid

Generates a random 16-character hexadecimal UUID object.

Dependencies
  • re: Provides regular expression support for UUID validation.
  • random.choices: Used for generating random hexadecimal strings.

UUID

Bases: str

Represents a 16-digit hexadecimal string with validation.

This class is used to validate and encapsulate a string that represents a 16-digit hexadecimal value. It provides strict checks to ensure only valid 16-character hexadecimal strings are accepted. It inherits from the built-in str class.

Attributes:

Name Type Description
_pattern Pattern

Regular expression pattern used to validate if the provided string is a valid 16-digit hexadecimal string.

generate_random_uuid

generate_random_uuid() -> UUID

Generates a random UUID using a pseudo-random selection of hexadecimal digits.

The function constructs a UUID object by randomly selecting 16 hexadecimal characters. This is achieved through the choices function, which ensures high entropy selection. The resulting string is then converted into a UUID object.

Returns:

Name Type Description
UUID UUID

A UUID object generated from the random hexadecimal string.