Skip to main content

Universally Unique Lexicographically Sortable Identifier

Project description



ulid


What is this?

This is a port of the original JavaScript ULID implementation to Python.

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

  $ pip install python-ulid

Basic Usage

Create a new ULID on from the current timestamp

  >>> from ulid import ULID
  >>> ulid = ULID()

Encode in different formats

  >>> str(ulid)
  '01BTGNYV6HRNK8K8VKZASZCFPE'
  >>> ulid.hex
  '015ea15f6cd1c56689a373fab3f63ece'
  >>> int(ulid)
  1820576928786795198723644692628913870
  >>> ulid.bytes
  b'\x01^\xa1_l\xd1\xc5f\x89\xa3s\xfa\xb3\xf6>\xce'

Access timestamp attribute

  >>> ulid.timestamp
  1505945939.153
  >>> ulid.milliseconds
  1505945939153
  >>> ulid.datetime
  datetime.datetime(2017, 9, 20, 22, 18, 59, 153000, tzinfo=datetime.timezone.utc)

Convert to UUID

  >>> ulid.to_uuid()
  UUID('015ea15f-6cd1-c566-89a3-73fab3f63ece')

Other implementations

Changelog

Version 1.0.0

  • Dropped support for Python 2. Only Python 3.6+ is supported.
  • Added type annotations
  • Added the named constructors ULID.from_datetime, ULID.from_timestamp and from_hex.
  • The named constructor ULID.new has been removed. Use one of the specifc named constructors instead. For a new ULID created from the current timestamp use the standard constructor.
  # old
  ulid = ULID.new()
  ulid = ULID.new(time.time())
  ulid = ULID.new(datetime.now())

  # new
  ulid = ULID()
  ulid = ULID.from_timestamp(time.time())
  ulid = ULID.from_datetime(datetime.now())
  • The ULID.str and ULID.int methods have been removed in favour of the more Pythonic special dunder-methods. Use str(ulid) and int(ulid) instead.
  • Added the property ULID.hex that returns a hex representation of the ULID.
  >>> ULID().hex
  '0171caa5459a8631a6894d072c8550a8'
  • Equality checks and ordering now also work with str-instances.
  • The package now has no external dependencies.
  • The test-coverage has been raised to 100%.

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-1.0.3.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file python-ulid-1.0.3.tar.gz.

File metadata

  • Download URL: python-ulid-1.0.3.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for python-ulid-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5dd8b969312a40e2212cec9c1ad63f25d4b6eafd92ee3195883e0287b6e9d19e
MD5 8cbe4649b0859eb07a09e4f69ce69afb
BLAKE2b-256 bbe3dd37e69a53c2437da7f0fbad3f02316a7953a52f20a322ffa9ba0d83dfe4

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: python_ulid-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for python_ulid-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8704dc20f547f531fe3a41d4369842d737a0f275403b909d0872e7ea0fe8d6f2
MD5 696dda210478ba5e35478eaf50ff7520
BLAKE2b-256 09536dbaf56f8401453cc99f0899ebc86a88503d95c5b0b54974095ab7f8567c

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