Skip to main content

Generate unique IDs to identify resources in apps

Project description

idlib

Time-ordered unique IDs for identifying resources in applications.

idlib generates unique IDs that are sequential: every ID generated will be lexicographically greater than all previously generated IDs. This makes them ideal as primary keys in databases, since they naturally sort in insertion order without a secondary timestamp column.

IDs are encoded with base32hex, which preserves the sort order of the underlying bytes while remaining safe to use in URLs and filenames.

Installation

pip install whatamithinking-idlib

ID Types

UOID — Unique Ordered ID

Structure: time (10 bytes) + random (6 bytes) → 16 bytes → 32-character string

The base type. Encodes a nanosecond-precision timestamp and random bytes.

from whatamithinking.idlib import UOID

id1 = UOID()
id2 = UOID()

print(str(id1))   # e.g. "0O0000CGFS0000ABCDEFGH123456"
print(str(id2))   # always lexicographically greater than id1

assert id1 < id2  # guaranteed ordering

Properties:

Property Description
.time Nanosecond epoch timestamp encoded in the ID (int)
.rand Random bytes component (bytes)

Reconstruction from string or bytes:

id_str = str(id1)
restored = UOID(id_str)   # from string
restored = UOID(bytes(id1))  # from bytes

UOHID — Unique Ordered Host ID

Structure: time (10 bytes) + random (6 bytes) + hostname (1–64 bytes) → 17–80 bytes → 32–128-character string

Extends UOID by encoding the hostname of the machine that generated the ID. Useful for systems where resources need to be routed back to the originating service by hostname.

Warning: This ID leaks the hostname of the generating machine. Do not use it when that information is sensitive.

from whatamithinking.idlib import UOHID

id1 = UOHID()

print(str(id1))       # base32hex-encoded string
print(id1.hostname)   # e.g. "my-server"

Properties:

Property Description
.time Nanosecond epoch timestamp (int)
.rand Random bytes component (bytes)
.hostname Hostname of the generating machine (str)

UOPHID — Unique Ordered Port-Host ID

Structure: time (10 bytes) + random (6 bytes) + port (2 bytes) + hostname (1–64 bytes) → 19–82 bytes → 32–136-character string

Extends UOHID by also encoding the port number of the service that created the ID. Useful for systems where resources need to be routed to a specific port on the originating host.

from whatamithinking.idlib import UOPHID

id1 = UOPHID(port=9100)

print(str(id1))       # base32hex-encoded string
print(id1.port)       # 9100
print(id1.hostname)   # e.g. "my-server"

Properties:

Property Description
.time Nanosecond epoch timestamp (int)
.rand Random bytes component (bytes)
.port Service port number (int, 0–65535)
.hostname Hostname of the generating machine (str)

Common API

All three types share this interface:

# Create new
id1 = UOID()

# Restore from string
id2 = UOID("0O0000CGFS0000ABCDEFGH123456")

# Restore from bytes
id3 = UOID(bytes(id1))

# String representation (base32hex, sortable)
s = str(id1)

# Raw bytes
b = bytes(id1)

# Comparison (all ordering operators supported)
assert id1 < id2
assert id1 == id1

# Replace individual fields and get a new instance
new_id = id1.replace(rand=b"\x00" * 6)

Ordering Guarantees

IDs are guaranteed to be strictly increasing as long as they are generated on the same thread. If multiple IDs are generated within the same nanosecond, the random component is auto-incremented to maintain order. If clock drift is detected (clock moved backwards), generation will retry for up to timeout seconds (default: 30 ms) before raising a RuntimeError.

License

MIT

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

whatamithinking_idlib-1.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for whatamithinking_idlib-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf23043756049b0fb27dbb663242dae7765036db63ec484eb1107b4915385ff1
MD5 aa24ea54a4ab16bbb4c82aa70d6a2ce3
BLAKE2b-256 f2b2b588edd7864c940277253142f1f4fd82c00a6489a55b604407014a9e7af6

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