Python-PKSUID
(Prefixed K-Sortable Unique IDentifier)
This library provides an enhancement to the KSUID identifier
first proposed and used by Segment.io, whose reference implementation
exists here:
https://github.com/segmentio/ksuid
This library extends the KSUID specification as the PKSUID
specification
with a prefix, inspired by the Stripe prefixed IDs, such as txn_1032HU2eZvKYlo2CEPtcnUvl
.
This in turn makes it easy for developers to see at a glance the underlying type of the
resource that the identifier refers to, and makes for easier reading/tracing of resources
in various locations, such as log files.
Installation
The package is available on PyPi, simply install with:
pip install pksuid
Usage
This package is tested working with Python 3.6+
An example of how to use this library is as follows:
from pksuid import PKSUID
uid = PKSUID('usr')
print(uid)
print(uid.get_prefix())
print(uid.get_timestamp())
print(uid.get_datetime())
print(uid.get_payload())
uid_from_string = PKSUID.parse('usr_24OnhzwMpa4sh0NQmTmICTYuFaD')
print(uid_from_string.get_timestamp())
uid_as_bytes = uid.bytes()
uid_from_bytes = PKSUID.parse_bytes(uid_as_bytes)
print(uid_from_bytes.get_datetime())
import time
ts = int(time.time())
lesser_uid, greater_uid = PKSUID('usr', timestamp = ts), PKSUID('usr', timestamp=ts + 5)
print(lesser_uid < greater_uid)
prefixed_uid_1, prefixed_uid_2 = PKSUID('diff', timestamp = ts), PKSUID('prefix', timestamp=ts + 5)
print(prefixed_uid_1 < prefixed_uid_2)
Testing
Run the unit tests with poetry run pytest
.