Skip to main content

Compact UUID library with efficient serialization and encoding

Project description

cuuid - Compact UUID Library

Python Version License: MIT

Python 3.12+ library for efficient UUID generation, serialization, and encoding with compact representation.

Features

  • Compact Serialization: Reduces UUID storage from 16 bytes to 2-17 bytes (up to 87% space savings)
  • Multiple UUID Versions: Supports UUID v1, v3, v4, and v5
  • Custom Encoding: Base59 encoding for human-readable representations
  • Type Safe: Comprehensive type hints for static analysis
  • Zero Dependencies: No external dependencies required

Requirements

  • Python 3.12 or higher

Installation

pip install cuuid

Or install from source:

git clone https://github.com/dubalu/cuuid.git
cd cuuid
pip install .

Quick Start

from cuuid import uuid1, uuid4, uuid5, NAMESPACE_DNS

# Generate UUID v4 (random)
u4 = uuid4()
print(u4)  # e.g., '73e94ff7-45db-4f9f-bd20-e751e221baa3'

# Generate UUID v1 (timestamp-based)
u1 = uuid1()
print(u1)  # e.g., '16fdd0ac-f98c-11f0-a5c5-34363b644ed4'

# Generate UUID v5 (SHA-1 hash)
u5 = uuid5(NAMESPACE_DNS, 'example.com')
print(u5)  # 'cfbff0d1-9375-5685-968c-48ce8b15ae17'

# Compact serialization
serialized = u4.serialise()
print(f"Size: {len(serialized)} bytes")  # Much smaller than 16 bytes!

# Encoding
encoded = u4.encode()
print(encoded)  # Base59 encoded representation

API Reference

UUID Generation

from cuuid import uuid, uuid1, uuid3, uuid4, uuid5, NAMESPACE_DNS

# Create new UUID with optional compaction
u = uuid()  # Creates UUID v1 with auto-compaction

# UUID v1: Timestamp and MAC address
u1 = uuid1(node=None, clock_seq=None)

# UUID v3: MD5 hash of namespace and name
u3 = uuid3(NAMESPACE_DNS, "example.com")

# UUID v4: Random UUID
u4 = uuid4()

# UUID v5: SHA-1 hash of namespace and name
u5 = uuid5(NAMESPACE_DNS, "example.com")

Serialization

from cuuid import UUID, encode, decode, unserialise

# Serialize UUID to compact bytes
u = uuid4()
serialized = u.serialise()

# Deserialize bytes back to UUID
uuid_list = unserialise(serialized)

# Encode to different formats
encoded_default = encode(serialized)  # Default encoding
encoded_guid = encode(serialized, 'guid')  # GUID format
encoded_urn = encode(serialized, 'urn')  # URN format

# Decode back to bytes
decoded = decode(encoded_default)

Compact UUIDs

from cuuid import uuid1

# Generate UUID v1
u = uuid1()

# Check if UUID can be compacted
compacted = u.compact_crush()
if compacted:
    print(f"Original: {u}")
    print(f"Compacted: {compacted}")
    print(f"Is compact: {compacted.iscompact()}")

Migration from Python 2.7

Version 1.0.0 is a complete rewrite for Python 3.12+. If you're upgrading from the Python 2.7 version:

Breaking Changes

  • Python 2.7 no longer supported - Minimum version is now Python 3.12
  • Removed six dependency - All compatibility shims removed
  • Type hints added - All public and private APIs now have type annotations

API Compatibility

The public API remains 100% compatible. All existing code using the following functions will continue to work:

  • UUID, uuid(), uuid1(), uuid3(), uuid4(), uuid5()
  • encode(), decode(), encode_uuid(), decode_uuid()
  • unserialise()
  • Namespace constants: NAMESPACE_DNS, NAMESPACE_URL, NAMESPACE_OID, NAMESPACE_X500

What Changed Internally

  1. Removed Python 2 compatibility layer (six library)
  2. Fixed integer division for Python 3 semantics
  3. Modernized bytes/string handling for Python 3
  4. Added comprehensive type hints (PEP 695)
  5. F-strings for all string formatting (PEP 701)
  6. Modern packaging with pyproject.toml

Technical Details

Compact Serialization Format

The library uses a variable-length encoding scheme:

  • Full format: 17 bytes (1 byte marker + 16 bytes UUID)
  • Condensed format: 4-16 bytes (optimized based on UUID content)
  • Compacted format: 2-13 bytes (for timestamp-based UUIDs with predictable node)

UUID Layout

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           time_low                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           time_mid            |       time_hi_and_version     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|clk_seq_hi_res |  clk_seq_low  |          node (0-1)           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         node (2-5)                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

time = 60 bits
clock = 14 bits
node = 48 bits

Development

Type Checking

The library includes comprehensive type hints. Use mypy for static type checking:

mypy cuuid/ --strict

Linting

Use ruff for linting:

ruff check cuuid/

Testing

Run the embedded test suite:

python -m cuuid

License

MIT License - Copyright (c) 2026 Dubalu International LLC

See LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

Changelog

Version 1.0.0 (2026-01-24)

Major Release - Python 3.12+ Only

Breaking Changes

  • Dropped Python 2.7 support
  • Minimum Python version is now 3.12
  • Removed six compatibility library

Added

  • Comprehensive type hints (PEP 695)
  • Modern packaging with pyproject.toml
  • F-string formatting (PEP 701)
  • Type marker file (py.typed)

Changed

  • Migrated from _random to public random module
  • Fixed integer division for Python 3 semantics
  • Improved bytes/string handling
  • Modernized code style and documentation

Fixed

  • Exception handling (removed deprecated .message attribute)
  • Character encoding/decoding for Python 3
  • BaseX encoding algorithm integer division

Made with ❤️ by Dubalu International LLC

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cuuid-1.0.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cuuid-1.0.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file cuuid-1.0.0.tar.gz.

File metadata

  • Download URL: cuuid-1.0.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cuuid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a0fea532489aa8880347335d159c9abf481b29074477c0bf840bacac1d993b37
MD5 9e5a2d445acab123d05296a071ef8ae0
BLAKE2b-256 0b6f2f94b5ba451bb094e99750c01a30a901b2377d5cf18cc16297fe6099c516

See more details on using hashes here.

File details

Details for the file cuuid-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cuuid-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cuuid-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00f29e9255fbed74fb1be90ef107c571dc4354dd4fe97f1e610f1a469f311bdc
MD5 070a43d2bb92a7a301be83231e6061ec
BLAKE2b-256 a5e3d45eb7f2f642d5b85a3e699fe724d3bda82e0081a5c7fa708ba40a9d1f38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page