A pure-Python KSUID implementation
Actively maintained. Last release 9 months ago, commits in 26 of the last 26 weeks. A safe choice.
pip install svix-ksuid
Svix - Webhooks as a service
Svix-KSUID (Python)
This library is inspired by Segment's KSUID implementation: https://github.com/segmentio/ksuid
For the Rust version, please check out https://github.com/svix/rust-ksuid
What is a ksuid?
A ksuid is a K sorted UID. In other words, a KSUID also stores a date component, so that ksuids can be approximately sorted based on the time they were created.
Read more here.
Usage
pip install svix-ksuid
from ksuid import Ksuid
ksuid = Ksuid()
Higher timestamp accuracy mode
Ksuids have a 1 second accuracy which is not sufficient for all use-cases. That's why this library exposes a higher accuracy mode which supports accuracy of up to 4ms.
It's fully compatible with normal ksuids, in fact, it outputs valid ksuids. The difference is that it sacrifices one byte of the random payload in favor of this accuracy.
The code too is fully compatible:
pip install svix-ksuid
from ksuid import KsuidMs
ksuid = KsuidMs()
Examples
Default ksuid
Generate a ksuid without passing a specific datetime
In [1]: from ksuid import Ksuid
In [2]: ksuid = Ksuid()
In [3]: f"Base62: {ksuid}"
Out[3]: 'Base62: 1srOrx2ZWZBpBUvZwXKQmoEYga2'
In [4]: f"Bytes: {bytes(ksuid)}"
Out[4]: "Bytes: b'\\r5\\xc43\\xe1\\x93>7\\xf2up\\x87c\\xad\\xc7tZ\\xf5\\xe7\\xf2'"
In [5]: f"Datetime: {ksuid.datetime}"
Out[5]: 'Datetime: 2021-05-21 14:04:03'
In [6]: f"Timestamp: {ksuid.timestamp}"
Out[6]: 'Timestamp: 1621627443'
In [7]: f"Payload: {ksuid.payload}"
Out[7]: "Payload: b'\\xe1\\x93>7\\xf2up\\x87c\\xad\\xc7tZ\\xf5\\xe7\\xf2'"