Skip to main content

Generates sortable, date/time-containing, human friendly unique string IDs

Project description

Generates sortable, date/time-containing, human friendly unique string IDs. The default length of generated IDs is 24 characters containing 118 bits total.

Features

  • Consists of only unambiguous lowercase letters and digits for human readability.

  • The first and last characters are letters chosen from among the most neat ones.

  • The generated IDs contain the date/time with a potential precision of 50ns encoded in the first 12 characters.

  • Extractable date/time.

  • The order of the strings is the same as the order of the date/time encoded in them.

  • The remaining characters contain highest possible secure random bits, generated from the following sources of entropy:

    • Current process ID

    • Hostname

    • Random bits of the highest possible secure source of randomness

    • Environment variable names and values

    • A counter that is randomly initialized when the module is imported and thead-safely incremented each time suid_gen is called

Installing

pip install suid

Every time the module is loaded it performs a time_ns precision test. If worse than 5 mks, a warning is logged. On the Windows platform it is recommended to install the win-precise-time package:

pip install win-precise-time

Usage

Generate a unique ID containing the current date/time:

>>> from suid import suid_gen
>>> suid_gen()
'by6wtze6jduq6ksj4tbsnjcx'

Generate a unique ID of the specified length:

>>> suid_gen(length=32)
'by6wtzvyyq380mc4fdyv9q1p1bbyr1vp'

Generate a unique ID containing a custom date/time:

>>> from datetime import datetime, timezone
>>> dt = datetime(year=2023, month=11, day=10, hour=14, minute=26, second=15, microsecond=1234, tzinfo=timezone.utc)
>>> suid_gen(at=dt)
'by640s7bh6vc9rk2s5ud6wvw'

Extract encoded date/time from ID:

>>> from suid import suid_to_datetime
>>> suid_to_datetime('by640s7bh6vc9rk2s5ud6wvw')
datetime.datetime(2023, 11, 10, 14, 26, 15, 1234, tzinfo=datetime.timezone.utc)

There is a helper function to get the current date/time, which can be more precise then the standard datetime.now(tz=timezone.utc):

>>> from suid import utcnow
>>> utcnow()
datetime.datetime(2023, 11, 26, 9, 0, 35, 886315, tzinfo=datetime.timezone.utc)

1.0.1 —

  • Fixed a bug with incorrect sort order.

1.0

  • Initial version.

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

suid-1.0.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

suid-1.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file suid-1.0.1.tar.gz.

File metadata

  • Download URL: suid-1.0.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for suid-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5b47f03a89672e0103ebc818472dabef09364bb8897fe8e89de664958103e639
MD5 3a933bd94b8d935408abdfd1faa9fced
BLAKE2b-256 36566270598105ad42f889e912c48d70c0be0a7a275341f609e342eb5404fa10

See more details on using hashes here.

File details

Details for the file suid-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: suid-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for suid-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64624f801e7040578c6895767a2fc588a89a895647d299bc30cd1acc2008b522
MD5 b94866750c9ada96bf1f5a014e2d9282
BLAKE2b-256 dd9928abb96934940810e705e57b322b27d56a6cdb66189f88023ff4d13bae29

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page