UUID Generator

Generate UUIDs in different versions and formats

python (3.9) 2025-11-12 uuid identifier random generator

Description

A utility for generating UUIDs in various versions (v1, v4) and formats. Supports different UUID standards and formatting options.

Features

  • UUID v1 (time-based)
  • UUID v4 (random)
  • Format conversion
  • Validation

Code

RAW
import uuidfrom typing import Optionalclass UUIDGenerator:    @staticmethod    def v1() -> str:        """Generate UUID v1 (time-based)."""        return str(uuid.uuid1())        @staticmethod    def v4() -> str:        """Generate UUID v4 (random)."""        return str(uuid.uuid4())        @staticmethod    def generate(version: int = 4) -> str:        """Generate UUID of specified version."""        if version == 1:            return UUIDGenerator.v1()        elif version == 4:            return UUIDGenerator.v4()        else:            raise ValueError(f"Unsupported UUID version: {version}")        @staticmethod    def validate(uuid_string: str) -> bool:        """Validate UUID format."""        try:            uuid.UUID(uuid_string)            return True        except (ValueError, AttributeError):            return False        @staticmethod    def to_hex(uuid_string: str) -> Optional[str]:        """Convert UUID to hex format (without dashes)."""        try:            return uuid.UUID(uuid_string).hex        except (ValueError, AttributeError):            return None
RAW
from uuid_generator import UUIDGenerator# Generate UUID v4uuid_v4 = UUIDGenerator.v4()print(f'UUID v4: {uuid_v4}')# Generate UUID v1uuid_v1 = UUIDGenerator.v1()print(f'UUID v1: {uuid_v1}')# Generate with version parameteruuid_custom = UUIDGenerator.generate(4)print(f'UUID 4: {uuid_custom}')# Validateis_valid = UUIDGenerator.validate('550e8400-e29b-41d4-a716-446655440000')print(f'Valid: {is_valid}')# Convert to hexhex_uuid = UUIDGenerator.to_hex('550e8400-e29b-41d4-a716-446655440000')print(f'Hex: {hex_uuid}')

Comments

No comments yet. Be the first to comment!