PKSUID is a Python library for generating Prefixed K-Sortable Unique IDentifiers. It extends the KSUID specification by allowing an arbitrary string prefix of up to 16 bytes, similar to Stripe's prefixed IDs, which helps developers quickly identify the resource type an identifier refers to. The library is currently at version 1.1.2 and has a stable release cadence.
pip install pksuidVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to generate a new PKSUID with a specified prefix, extract its components like prefix, timestamp, and datetime, and parse a PKSUID from its string or byte representation.
Ensure your prefix string is 16 bytes or less in length. Use `len(prefix.encode('utf-8'))` to check the byte length if your prefix contains multi-byte characters.For standard epoch timestamps or `datetime` objects, use `uid.get_datetime()` which returns a `datetime.datetime` object in UTC. To convert to a standard Unix timestamp from the `datetime` object, use `int(uid.get_datetime().timestamp())`.
Always assign the result of methods like `PKSUID.parse()` or `PKSUID.parse_bytes()` to a new variable or overwrite the existing one, as they return new `PKSUID` instances.
Run `pip install pksuid` to install the library.
Shorten your prefix string to be 16 bytes or less. Remember that non-ASCII characters might take up more than one byte (e.g., `len('😀'.encode('utf-8'))` is 4).If you have a PKSUID as a string (e.g., from a database or log), you must first parse it back into a `PKSUID` object using `PKSUID.parse()` before calling its methods. Example: `uid_obj = PKSUID.parse('usr_24OnhzwMpa4sh0NQmTmICTYuFaD')`.No dependency data recorded yet.