Skip to main content

Python-PKSUID

PyPI version fury.io

(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

# generate a new unique identifier with the prefix usr
uid = PKSUID('usr')

# returns 'usr_24OnhzwMpa4sh0NQmTmICTYuFaD'
print(uid)

# returns: usr
print(uid.get_prefix())

# returns: 1643510623
print(uid.get_timestamp())

# returns: 2022-01-30 02:43:43
print(uid.get_datetime())

# returns: b'\x81>*\xccDJT\xf1\xbe\xa9\xf3&\xe8\xa5\xb2\xc1'
print(uid.get_payload())

# convert from a str representation back to PKSUID
uid_from_string = PKSUID.parse('usr_24OnhzwMpa4sh0NQmTmICTYuFaD')

# this can now be used as usual
# returns: 1643510623
print(uid_from_string.get_timestamp())

# conversion to and parsing from bytes is also possible
uid_as_bytes = uid.bytes()
uid_from_bytes = PKSUID.parse_bytes(uid_as_bytes)

# returns: 2022-01-30 02:43:43
print(uid_from_bytes.get_datetime())

# all the standard comparison operators are available
import time
ts = int(time.time())

lesser_uid, greater_uid = PKSUID('usr', timestamp = ts), PKSUID('usr', timestamp=ts + 5)

# returns True
print(lesser_uid < greater_uid)

# except for the case of equivalence operators (eq, ne), the prefix is not taken into account when comparing
prefixed_uid_1, prefixed_uid_2 = PKSUID('diff', timestamp = ts), PKSUID('prefix', timestamp=ts + 5)

# returns True
print(prefixed_uid_1 < prefixed_uid_2)

Testing

Run the unit tests with poetry run pytest.

Metadata

Release files for pksuid 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pksuid 1.1.2
File Size Uploaded
pksuid-1.1.2.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pksuid 1.1.2
File Interpreter ABI Platform
pksuid-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.9 kB

Release files / pksuid-1.1.2.tar.gz

Download URL pksuid-1.1.2.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dc08ed7924a8affea5f36af4b6af345b126f521c9165b95e91ca1a2acef99e49
BLAKE2b-256 checksum
How to use checksums
152f8df6e1663197459f3de732cee69d050a118023d80e3ce00d172b83b6f09d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.3 Darwin/21.4.0

Release files / pksuid-1.1.2-py3-none-any.whl

Download URL pksuid-1.1.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
332296a7104b715169ad0e8067f29eba7f8c17e80e0d1c8342e4b6fc60efaea6
BLAKE2b-256 checksum
How to use checksums
6c2dcc6719208faf61a5b00feadf743b3fc29aec588a3c22567358dc966f3117
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.3 Darwin/21.4.0

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page