Skip to main content

uuid0 - better timestamped UUIDs in Python

V4 UUIDs are often used as primary keys or as part of database indexes. However, their random and non-sequential nature can cause locality and performance issues.

This Python 3 package mitigates these issues by using encoding a UNIX timestamp in the first 6 bytes of the UUID, and filling the remaining bytes with random data. The uuid0.UUID class inherits from the standard uuid.UUID class so it’s safe to use all the usual properties (hex, int, bytes, etc.)

The package also contains uuid0.django which provides Django form and model fields based on the UUID0 type, as well as an abstract UUID0Model.

Status

This package is in alpha status, use at your own risk.

Usage

Install by running:

pip install uuid0

Example usage:

>>> import uuid0

# make a UUID based on the current time
>>> uuid0.generate()
UUID('0dc7ef03-c534-d288-67b7-34cf4dfa9350')

# make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid0.UUID('{0b7dd8d0-7e40-8360-9322-4a361d7b573f}')

# extract the datetime from a UUID
>>> str(x.datetime)
'2010-01-15 00:00:36'

# convert a UUID to a string of hex digits in standard form
>>> str(x)
'0b7dd8d0-7e40-8360-9322-4a361d7b573f'

# convert a UUID to a base62 string
>>> x.base62
'LgQWTxkOpLyTaEuRAav9D'

# make a UUID from a base62 string
>>> uuid0.UUID(base62=x.base62)
UUID('0b7dd8d0-7e40-8360-9322-4a361d7b573f')

# get the raw 16 bytes of the UUID
>>> x.bytes
b'\x0b}\xd8\xd0~@\x83`\x93"J6\x1d{W?'

# make a UUID from a 16-byte string
>>> uuid0.UUID(bytes=x.bytes)
UUID('0b7dd8d0-7e40-8360-9322-4a361d7b573f')

What about collisions?

Collisions are a concern, of course, but only if you are generating really large amounts of UUIDs in a short timespan.

By default, uuid0 only uses 6 bytes to encode the time and does not encode any version bits, leaving 10 bytes (80 bits) of random data.

The probability of collision is near zero for most use cases since for each 1/10000s period, there are 280 possible UUIDs.

Performance

Generating a UUID0 is slower than generating a UUID v4, but faster than a UUID v1.

Below are results for 500k iterations on an i7-6700HQ and Python 3.6.2 (generated using the benchmarks/generate.py script):

method

it/s

?s/it

% slower

uuid.uuid4()

453043

2.207

0.0%

uuid0.generate()

311184

3.214

31.31%

uuid.uuid1()

230163

4.345

49.2%

In terms of database performance, indexes are approximately 18% smaller and inserts about 23% faster on PostgreSQL with a uuid type column, but YMMV depending on your use case.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for uuid0 0.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uuid0 0.2.7
File Size Uploaded Provenance Actions
uuid0-0.2.7.tar.gz 5.5 kB Without provenance Details for uuid0-0.2.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for uuid0 0.2.7
File Interpreter ABI Platform Actions
uuid0-0.2.7-py2.py3-none-any.whl Python 2, Python 3 none any Details for uuid0-0.2.7-py2.py3-none-any.whl

Total release size: 14.8 kB

Release files / uuid0-0.2.7.tar.gz

Download URL uuid0-0.2.7.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
835da1b868078eadfd63de24d17b9ea3bd1c655497f99e1b8b84f6e4117e6c2b
BLAKE2b-256 checksum
How to use checksums
765bf9cfca063b788e2cfcd4ff03ac04fa8b80ddea629a84b3e292bc6f1dcc8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / uuid0-0.2.7-py2.py3-none-any.whl

Download URL uuid0-0.2.7-py2.py3-none-any.whl
Size 9.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b200bd88098010cef5f4445ac0f1c6fe60e021f0478fb3968e943adc1e89b902
BLAKE2b-256 checksum
How to use checksums
9764a73d68952996907b22828754d8fd9ed0536c7455033957d06d1fb00f5cea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for uuid0 0.2.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page