A pure-Python KSUID implementation
Project description
Python-KSUID
This library is inspired by Segment's KSUID implementation: https://github.com/segmentio/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
Examples
Default ksuid
Generate a ksuid without passing a specific datetime
In [1]: from ksuid.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'"
ksuid from datetime
In [1]: datetime = datetime(year=2021, month=5, day=19, hour=1, minute=1, second=1, microsecond=1)
In [2]: datetime
Out[2]: datetime.datetime(2021, 5, 19, 1, 1, 1, 1)
In [3]: ksuid = Ksuid(datetime)
In [4]: ksuid.datetime
Out[4]: datetime.datetime(2021, 5, 19, 1, 1, 1)
In [5]: ksuid.timestamp
Out[5]: 1621407661
ksuid from base62
In [1]: ksuid = Ksuid()
In [2]: ksuid.timestamp
Out[2]: 1621634852
In [3]: f"Base62: {ksuid}"
Out[3]: 'Base62: 1srdszO8Xy2cR6CnARnvxCfRmK4'
In [4]: ksuid_from_base62 = Ksuid.from_base62("1srdszO8Xy2cR6CnARnvxCfRmK4")
In [5]: ksuid_from_base62.timestamp
Out[5]: 1621634852
ksuid from bytes
In [1]: ksuid = Ksuid()
In [2]: ksuid_from_bytes = ksuid.from_bytes(bytes(ksuid))
In [3]: f"ksuid: {ksuid}, ksuid_from_bytes: {ksuid_from_bytes}"
Out[3]: 'ksuid: 1sreAHoz6myPhXghsOdVBoec3Vr, ksuid_from_bytes: 1sreAHoz6myPhXghsOdVBoec3Vr'
In [4]: ksuid == ksuid_from_bytes
Out[4]: True
Compare ksuid(s)
In [1]: ksuid_1
Out[1]: <ksuid.ksuid.Ksuid at 0x7f8868dfcbb0>
In [2]: ksuid_2
Out[2]: <ksuid.ksuid.Ksuid at 0x7f88480de460>
In [3]: f"ksuid_1: {ksuid_1}, ksuid_2: {ksuid_2}"
Out[3]: 'ksuid_1: 1sreAHoz6myPhXghsOdVBoec3Vr, ksuid_2: 1sreAHoz6myPhXghsOdVBoec3Vr'
In [4]: ksuid_1 == ksuid_2
Out[4]: True
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
svix-ksuid-0.4.0.tar.gz
(4.4 kB
view details)
File details
Details for the file svix-ksuid-0.4.0.tar.gz
.
File metadata
- Download URL: svix-ksuid-0.4.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c15538b59de34980d293f7f47aad9b69e1f59f1884585b8780e302661dd4e34 |
|
MD5 | a5fdbc6305d8b6146ff1d1f950d4ccdb |
|
BLAKE2b-256 | 452109ae2b597657779cd7fdee480c6254b4aa920df4c1a43de887cc9305177a |