Skip to main content

A generator library for concise, unambiguous and URL-safe UUIDs.

Project description

Description

shortuuid is a simple python library that generates concise, unambiguous, URL-safe UUIDs.

Often, one needs to use non-sequential IDs in places where users will see them, but the IDs must be as concise and easy to use as possible. shortuuid solves this problem by generating uuids using Python's built-in uuid module and then translating them to base57 using lowercase and uppercase letters and digits, and removing similar-looking characters such as l, 1, I, O and 0.

image

Installation

To install shortuuid you need:

  • Python 3.x.

If you have the dependencies, you have multiple options of installation:

Usage

To use shortuuid, just import it in your project like so:

>>> import shortuuid

You can then generate a short UUID:

>>> shortuuid.uuid()
'vytxeTZskVKR7C7WgdSP3d'

If you prefer a version 5 UUID, you can pass a name (DNS or URL) to the call and it will be used as a namespace (uuid.NAMESPACE_DNS or uuid.NAMESPACE_URL) for the resulting UUID:

>>> shortuuid.uuid(name="example.com")
'wpsWLdLt9nscn2jbTD3uxe'

>>> shortuuid.uuid(name="<http://example.com>")
'c8sh5y9hdSMS6zVnrvf53T'

You can also generate a cryptographically secure random string (using os.urandom() internally) with:

>>> shortuuid.ShortUUID().random(length=22)
'RaF56o2r58hTKT7AYS9doj'

To see the alphabet that is being used to generate new UUIDs:

>>> shortuuid.get_alphabet()
'23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

If you want to use your own alphabet to generate UUIDs, use set_alphabet():

>>> shortuuid.set_alphabet("aaaaabcdefgh1230123")
>>> shortuuid.uuid()
'0agee20aa1hehebcagddhedddc0d2chhab3b'

shortuuid will automatically sort and remove duplicates from your alphabet to ensure consistency:

>>> shortuuid.get_alphabet()
'0123abcdefgh'

If the default 22 digits are too long for you, you can get shorter IDs by just truncating the string to the desired length. The IDs won't be universally unique any longer, but the probability of a collision will still be very low.

To serialize existing UUIDs, use encode() and decode():

>>> import uuid
>>> u = uuid.uuid4()
>>> u
UUID('6ca4f0f8-2508-4bac-b8f1-5d1e3da2247a')

>>> s = shortuuid.encode(u)
>>> s
'cu8Eo9RyrUsV4MXEiDZpLM'

>>> shortuuid.decode(s) == u
True

>>> short = s[:7]
>>> short
'cu8Eo9R'

>>> h = shortuuid.decode(short)
UUID('00000000-0000-0000-0000-00b8c0b9f952')

>>> shortuuid.decode(shortuuid.encode(h)) == h
True

Class-based usage

If you need to have various alphabets per-thread, you can use the ShortUUID class, like so:

>>> su = shortuuid.ShortUUID(alphabet="01345678")
>>> su.uuid()
'034636353306816784480643806546503818874456'

>>> su.get_alphabet()
'01345678'

>>> su.set_alphabet("21345687654123456")
>>> su.get_alphabet()
'12345678'

Command-line usage

shortuuid provides a simple way to generate a short UUID in a terminal:

$ python3 -m shortuuid
fZpeF6gcskHbSpTgpQCkcJ

(Replace python3 with py if you are using Windows).

Django field

shortuuid includes a Django field that generates random short UUIDs by default, for your convenience:

from shortuuid.django_fields import ShortUUIDField

class MyModel(models.Model):
    # A primary key ID of length 16 and a short alphabet.
    id = ShortUUIDField(length=16, alphabet="abcdefg1234", primary_key=True)
    # A short UUID of length 22 and the default alphabet.
    api_key = ShortUUIDField()

The field is the same as the CharField, with max_length replaced with length, an alphabet argument added and the default argument removed. Everything else is exactly the same, e.g. index, help_text, etc.

Compatibility note

Versions of ShortUUID prior to 1.0.0 generated UUIDs with their MSB last, i.e. reversed. This was later fixed, but if you have some UUIDs stored as a string with the old method, you need to pass legacy=True to decode() when converting your strings back to UUIDs.

That option will go away in the future, so you will want to convert your UUIDs to strings using the new method. This can be done like so:

>>> new_uuid_str = encode(decode(old_uuid_str, legacy=True))

License

shortuuid is distributed under the BSD license.

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

shortuuid-1.0.4.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

shortuuid-1.0.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file shortuuid-1.0.4.tar.gz.

File metadata

  • Download URL: shortuuid-1.0.4.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.10

File hashes

Hashes for shortuuid-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d59613cc63866c7fc861a5873b25c60a2c8d886850322fe0a8bfbfc03118fbc3
MD5 104e03193c767b7429d64c6a22114623
BLAKE2b-256 3c9b1176d9869fb7e8e166d4906f07ad5afea9710f1c122f76404152028bac20

See more details on using hashes here.

File details

Details for the file shortuuid-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: shortuuid-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.10

File hashes

Hashes for shortuuid-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a7ee4e324735c54ea15a30215b5bb9fcbb9674981f51210ff6e075b40550b72a
MD5 9e166c643b89a665fbc5fb5560f58f79
BLAKE2b-256 bf5c2475d678393f0da39682cb221abff2e0be75f40c83af0a887eebbe27320d

See more details on using hashes here.

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