ULID Python Implementation (ahawker/ulid)

1.1.0 · maintenance · verified Fri Apr 10

The `ulid-py` library (version 1.1.0) provides a minimal, self-contained Python 3 implementation of the Universally Unique Lexicographically Sortable Identifier (ULID) specification. It aims for a lightweight implementation without external dependencies. The project is currently in maintenance mode, with its last release in March 2022.

Warnings

Install

Imports

Quickstart

Demonstrates generating new ULIDs, creating them from existing timestamps, and accessing their components and other representations.

import ulid
import datetime

# Create a brand new ULID
u = ulid.new()
print(f"New ULID: {u}")
print(f"String representation: {str(u)}")

# Create a new ULID from an existing datetime object
dt = datetime.datetime(1999, 1, 1, tzinfo=datetime.timezone.utc)
u_from_dt = ulid.from_timestamp(dt)
print(f"ULID from datetime: {u_from_dt}")

# Access timestamp and randomness parts
print(f"Timestamp part: {u.timestamp()}")
print(f"Randomness part: {u.randomness()}")

# Convert to UUID
import uuid
uuid_val = u.to_uuid()
print(f"As UUID: {uuid_val}")

view raw JSON →