Skip to main content

Universally unique lexicographically sortable identifier

Project description

A ULID is a universally unique lexicographically sortable identifier. It is

  • 128-bit compatible with UUID

  • 1.21e+24 unique ULIDs per millisecond

  • Lexicographically sortable!

  • Canonically encoded as a 26 character string, as opposed to the 36 character UUID

  • Uses Crockford’s base32 for better efficiency and readability (5 bits per character)

  • Case insensitive

  • No special characters (URL safe)

In general the structure of a ULID is as follows:

 01AN4Z07BY      79KA1307SR9X4MV3
|----------|    |----------------|
 Timestamp          Randomness
   48bits             80bits

For more information have a look at the original specification.

Installation

Use pip to install the library

$ pip install python-ulid

Basic Usage

Create a new ULID object from the current timestamp

>>> from ulid import ULID
>>> ULID()
ULID(01E75HZVW36EAZKMF1W7XNMSB4)

or use one of the named constructors

>>> import time, datetime
>>> ULID.from_timestamp(time.time())
ULID(01E75J1MKKWMGG0N5MBHFMRC84)
>>> ULID.from_datetime(datetime.datetime.now())
ULID(01E75J2XBK390V2XRH44EHC10X)

There are several options for encoding the ULID object (e.g. string, hex, int), as well as to access the timestamp attribute in different formats:

>>> str(ulid)
'01BTGNYV6HRNK8K8VKZASZCFPE'
>>> ulid.hex
'015ea15f6cd1c56689a373fab3f63ece'
>>> ulid.timestamp
1505945939.153
>>> ulid.datetime
datetime.datetime(2017, 9, 20, 22, 18, 59, 153000, tzinfo=datetime.timezone.utc)
>>> ulid.to_uuid()
UUID('015ea15f-6cd1-c566-89a3-73fab3f63ece')

Command line interface

The package comes with a CLI interface that can be invoked either by the script name ulid or as python module python -m ulid. The CLI allows you to generate, inspect and convert ULIDs, e.g.

$ ulid build
01HASFKBN8SKZTSVVS03K5AMMS

$ ulid build --from-datetime=2023-09-23T10:20:30
01HB0J0F5GCKEXNSWVAD5PEAC1

$ ulid show 01HASFKBN8SKZTSVVS03K5AMMS
ULID:      01HASFKBN8SKZTSVVS03K5AMMS
Hex:       018ab2f9aea8ccffacef7900e6555299
Int:       2049395013039097460549394558635823769
Timestamp: 1695219822.248
Datetime:  2023-09-20 14:23:42.248000+00:00

$ echo 01HASFKBN8SKZTSVVS03K5AMMS | ulid show --uuid -
018ab2f9-aea8-ccff-acef-7900e6555299

Other implementations

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

python_ulid-2.1.0.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

python_ulid-2.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file python_ulid-2.1.0.tar.gz.

File metadata

  • Download URL: python_ulid-2.1.0.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for python_ulid-2.1.0.tar.gz
Algorithm Hash digest
SHA256 bcca13fa6bcb16399bf04ab95b61c69275b2b9224c423a8d129bbb599896ccd2
MD5 256aca8985503da4a0f65d57a597676f
BLAKE2b-256 63ea32b815acee5d770869f4539821719325fe381a51c90c4ceff86a30770d50

See more details on using hashes here.

Provenance

File details

Details for the file python_ulid-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_ulid-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 969a3b96d5c82a96e7209c4b9839cbbfb52540f64b7e9c397a71713e3c8deee9
MD5 aa9ac74be5fbc2a19326eef4e043480e
BLAKE2b-256 d866a5724881484a45be8edfbf7b0221c3cfb630d0d6ea0bfe35cd92e242b101

See more details on using hashes here.

Provenance

Supported by

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