Skip to main content

Timestamp-orderable UUIDs for Python, written in Rust.

Project description

UUIDT

Timestamp-orderable UUIDs for Python, written in Rust.

Installation

pip install uuidt

Usage

import uuidt

# Create a new UUIDT
u = uuidt.new('my-namespace')

# Print the UUIDT properties
print(u.namespace)
print(u.timestamp)
print(u.hostname)
print(u.random_chars)

# Convert to a string
print(str(u))

# Extract the timestamp from a UUIDT string
print(uuidt.extract_timestamp("cr3su3qh-4ium-00bk-00ip-vqlpgpomk3dv"))
# 1678562753992474990

Motivation

UUIDs are great for generating unique identifiers, but they are not necessarily time-orderable. This is a problem if you want to generate a UUID for a new record in a database, and then use that UUID to order the records by creation time.

Many databases avoid this problem using auto-incrementing integer IDs, but this isn't possible in distributed databases like CockroachDB, so a UUID is typically used as the primary key instead.

This library generates UUIDs that are time-orderable. The first 12 alphanumeric characters of the UUID are a nanosecond-precision timestamp which has been base-36 encoded, so they can be sorted lexicographically. The remaining 20 characters are a combination of a namespace, the hostname of the machine that generated the UUID, and a random string.

Technically, UUID1s are also time-orderable, but they are not guaranteed to be ordered by creation time, and it can be difficult to extract the timestamp from a UUID1.

Why Rust?

Mostly as a learning opportunity for me, though also for speed. The Rust implementation is significantly faster than the Python implementation, which used Numpy to convert to base-36.

What if I don't want the Rust implementation?

UUIDT should be installable as a wheel on most systems, but if you hate Ferris and want to use the Python implementation instead, here's some equivalent code:

import random
import socket
import time

import numpy as np

BASE = 36
DIVISOR = BASE - 1
CHARACTERS = list('0123456789abcdefghijklmnopqrstuvwxyz')[:BASE]


class UUIDT:
    def __init__(self, namespace: str, timestamp: int, hostname: str, random_chars: str):
        self.namespace = namespace
        self.timestamp = timestamp
        self.hostname = hostname
        self.random_chars = random_chars

    def __str__(self):
        hostname_enc = sum(self.hostname.encode('utf-8'))
        namespace_enc = sum(self.namespace.encode('utf-8'))

        timestamp_str = np.base_repr(self.timestamp, 36).lower()
        hostname_str = np.base_repr(hostname_enc, 36).lower()
        namespace_str = np.base_repr(namespace_enc, 36).lower()

        return (
            f'{timestamp_str[:8]}-{timestamp_str[8:]}-{hostname_str:0>4}-'
            f'{namespace_str:0>4}-{self.random_chars}'
        )


def new(namespace: str) -> UUIDT:
    timestamp = time.time_ns()
    hostname = socket.gethostname()
    random_chars = ''.join(random.choices(CHARACTERS, k=4))

    return UUIDT(namespace, timestamp, hostname, random_chars)

License

MIT

Using UUIDT in your project

While UUIDT is MIT licensed, I'm really curious to seeing the projects that use it! If you use UUIDT in your project, I'd love to hear about it! Please let me know by either opening an issue or sending me an email at the address in the pyproject.toml file.

Contributing

Contributions are welcome! Just open an issue or a pull request.

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

uuidt-0.1.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distributions

uuidt-0.1.1-cp37-abi3-win_amd64.whl (158.3 kB view details)

Uploaded CPython 3.7+ Windows x86-64

uuidt-0.1.1-cp37-abi3-win32.whl (150.5 kB view details)

Uploaded CPython 3.7+ Windows x86

uuidt-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

uuidt-0.1.1-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.3 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ s390x

uuidt-0.1.1-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.2 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64le

uuidt-0.1.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.1 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARMv7l

uuidt-0.1.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

uuidt-0.1.1-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.5+ i686

uuidt-0.1.1-cp37-abi3-macosx_11_0_arm64.whl (275.2 kB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

uuidt-0.1.1-cp37-abi3-macosx_10_7_x86_64.whl (285.6 kB view details)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

File details

Details for the file uuidt-0.1.1.tar.gz.

File metadata

  • Download URL: uuidt-0.1.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.14.15

File hashes

Hashes for uuidt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 56be7cedce79cb2837dd023659f11baabb240fb9506ab10c6dee97e1ea350b16
MD5 ff8368ca2724351ef473b6d93ecf0ebe
BLAKE2b-256 a69e4e030060cdf578d402250d89829879c1935cf0623bb22571d196dd600b1a

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: uuidt-0.1.1-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 158.3 kB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.14.15

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ce7b289ff7cd697f7893cb0c94aae9d35ff427f8d5dd28b7e04c95aeefad0ced
MD5 174369ecf2c9c8cae9633874bc7938f1
BLAKE2b-256 ef203e64087602b8a5826d4e99233e85680473ed424176b2d3c8e547477cc419

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-win32.whl.

File metadata

  • Download URL: uuidt-0.1.1-cp37-abi3-win32.whl
  • Upload date:
  • Size: 150.5 kB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.14.15

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 b43ed749efe14582cfa655a4fdb8238a05825729865dfe36d31fd55328a623d3
MD5 1b02500efd06570dc37d51ff0a0fff8a
BLAKE2b-256 63bdb4c801595b8639ac02280fcc8cd53abac5d1baa41cb497aac55fe0bebdde

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d476cc3c740ed0314c96e621cacb5f8de92234eb90d5e432d344b0945e9c8488
MD5 0dd7e6a5ec7bf081514920d41118cd5b
BLAKE2b-256 da19349432165528d80960909aec0bb97bc4bf1fb44e459ddc27c45f33548001

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 9e34ba7e23e25c26be62d67f790c565b5a20ef63b53f8b25959a22cc703f3f52
MD5 7bf82c71eb4dabe4221970992040883b
BLAKE2b-256 b280b870aa34ed26b305947807c9ce06940b5f6f3f7b4d1c5eb1ae1870b34c43

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 60ce71588368cc7da90df05ec0f42e9c79182e5e51bfea5c85055791911c3989
MD5 0f5634a9ba266a6722f3ba075c690ef0
BLAKE2b-256 3ee63cbd9bfd3196977cc2d177d13a7e9d7661811d795efca1ac82bf0c858e9b

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 dd7b7116c2b59282ea96c889597c696d6659d74715bb19b9aa8379ed98bb67f2
MD5 8f354466ae135f78dc48d8205baf2432
BLAKE2b-256 64178e25091aa66aa7413fb58c0266d699c56bbb5891632141aa80f78d5c29f6

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 78001bba9c4740594385b575f67430b1386d9b660d92a2807dd4dcba6c20e714
MD5 1596184fd774215e2bbea56b882460d8
BLAKE2b-256 b758fd6020da57e96fc6d690ae84ab35a2eb41a6677dd78324bc557e0444a6c9

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 7464a5ae99046acdb08f53867ffdd8dae81a99d5ca8a1098856ff664671fbe12
MD5 764caedabc123b5b31c072bd20ce1b2c
BLAKE2b-256 e1f8273b2bbeead46bfe53878022341de361d867a2e9a56d07e5faf63ebf46f8

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 30b4fed954c6694fbcabc438b279f8a4371a3fee8587a78d0bddd245d1ab62e3
MD5 b278de14415dd0cebd6ee6aee66ca346
BLAKE2b-256 078c4f24a7a525fd2c7dcbbd26416a1a67eb2ff5f37d8c46e02afb6e2e6669e8

See more details on using hashes here.

File details

Details for the file uuidt-0.1.1-cp37-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for uuidt-0.1.1-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e413feedd7d5bf5c517d0e943f80569dd71a586a24fdb2eef58e62d92e6dda84
MD5 3e92b08e81d2a8291295d2dff043e2db
BLAKE2b-256 d42a4efe0623fa1f22e495cd444291352177600fbf6a76bff93309f0a0abe7a0

See more details on using hashes here.

Supported by

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