fastuuid

repository·master·Indexed 41 days ago

https://github.com/fastuuid/fastuuid

High-performance CPython bindings to the Rust UUID library, providing a faster and stricter alternative to Python's built-in uuid module. Supports UUID v1, v3, v4, v5, and v7. Features an API compatible with the standard uuid class and includes bulk generation functions like uuid4_bulk() and uuid7_bulk() that release the GIL for improved multi-threaded performance. Version 0.14.0.

Tokens
1.4K
Snippets
2
Records
6
Agent score
2%

What's inside fastuuid

  1. Overview of fastuuid

    master

    fastuuid is a library providing CPython bindings to Rust's UUID library. It is designed to be a high-performance replacement for Python's built-in uuid module.

    Key features:

    • API Compatibility: The API is identical to Python's built-in uuid class.
    • Supported Versions: Supports UUID v1, v3, v4, v5, and v7.
    • Performance: Significantly faster than the pure-Python implementation and features stricter hexadecimal parsing.
    • Concurrency: Provides a uuid4_bulk() function that releases the Global Interpreter Lock (GIL) during generation, allowing other Python threads to run concurrently.
  2. Use uuid4_bulk() for high-performance UUID generation

    master
    If your application requires generating a large volume of random UUIDs (v4), use the uuid4_bulk() function. Unlike standard generation methods, uuid4_bulk() releases the GIL for the entire duration of the generation process. This enables true multi-threaded performance in Python by allowing other threads to execute while the Rust backend generates the UUIDs.
  3. Generate UUIDs with fastuuid

    master

    The fastuuid module provides high-performance UUID generation for Python, including support for various RFC 4122 versions (v1, v3, v4, v5, v7) and bulk generation methods.

    Available Generation Functions:

    • uuid4(): Generates a random UUID (v4).
    • uuid7(): Generates a time-ordered UUID (v7).
    • uuid1(node=..., clock_seq=...): Generates a v1 UUID. You can optionally provide a 48-bit node ID or a 16-bit clock_seq.
    • uuid_v1mc(): A fast path for v1 UUIDs using a randomly generated multicast MAC address (similar to PostgreSQL's uuid-ossp).
    • uuid3(namespace, name): Generates a v3 UUID using MD5 hashing.
    • uuid5(namespace, name): Generates a v5 UUID using SHA-1 hashing.

    Bulk Generation (High Performance):

    For scenarios requiring many UUIDs at once, use the bulk functions to minimize Python overhead:

    • uuid4_bulk(n): Returns a list of n UUID objects.
    • uuid4_as_strings_bulk(n): Returns a list of n UUID strings.
    • uuid7_bulk(n): Returns a list of n UUID objects.
    • uuid7_as_strings_bulk(n): Returns a list of n UUID strings.
    import fastuuid
    
    # Single UUIDs
    u1 = fastuuid.uuid4()
    u7 = fastuuid.uuid7()
    
    # Bulk UUIDs
    uids = fastuuid.uuid4_bulk(1000)
    strings = fastuuid.uuid4_as_strings_bulk(1000)
  4. Use the UUID class

    master

    The UUID class represents a generated UUID and provides several ways to inspect or convert its data.

    Creating a UUID from existing data

    You can instantiate a UUID object using the UUID(..., ...) constructor with one of the following arguments (only one is required):

    • hex: A hexadecimal UUID string.
    • bytes: 16 bytes of data.
    • bytes_le: 16 bytes of data in little-endian format.
    • fields: A 6-tuple of (time_low, time_mid, time_hi_version, clock_seq_hi_variant, clock_seq_low, node).
    • int: A 128-bit integer.
    • version: (Optional) An integer specifying the version (1, 2, 3, 4, or 5).

    Accessing UUID properties

    • .int: Returns the 128-bit integer representation.
    • .bytes: Returns the 16 bytes in big-endian order.
    • .bytes_le: Returns the 16 bytes in little-endian order.
    • .hex: Returns the lowercase hexadecimal string.
    • .urn: Returns the UUID in URN format.
    • .version: Returns the version number (e.g., 4) if it is an RFC 4122/9562 UUID.
    • .variant: Returns a string describing the variant (e.g., 'specified in RFC 4122').
    • .fields: Returns a 6-tuple of the UUID components.
    • .time: Returns the timestamp associated with the UUID.
    • .node: Returns the 48-bit node ID.
    import fastuuid
    
    # Create from hex
    uid = fastuuid.UUID(hex='550e8400-e29b-41d4-a716-446655440000')
    
    # Access properties
    print(uid.hex)      # '550e8400-e29b-41d4-a716-446655440000'
    print(uid.int)      # 113036677011111111111111111111111111111
    print(uid.version)  # 4
  5. Reference: UUID class properties and methods

    master

    The following properties and methods are available on the fastuuid.UUID class:

    Property/MethodTypeDescription
    .intint128-bit integer representation
    .bytesbytes16-byte big-endian representation
    .bytes_lebytes16-byte little-endian representation
    .hexstrLowercase hexadecimal string
    .urnstrURN representation
    .versionint | NoneRFC 4122 version number
    .variantstr | NoneVariant description
    .fieldstuple6-tuple: (time_low, time_mid, time_hi_version, clock_seq_hi_variant, clock_seq_low, node)
    .time_lowint32-bit time_low field
    .time_midint16-bit time_mid field
    .time_hi_versionint16-bit time_hi_version field
    .clock_seq_hi_variantint8-bit clock_seq_hi_variant field
    .clock_seq_lowint8-bit clock_seq_low field
    .nodeint48-bit node ID
    .timeint128-bit timestamp value
    __str__strHyphenated string representation
    __repr__strPython representation UUID('...')