A generator library for concise, unambiguous and URL-safe UUIDs.
pip install shortuuidDescription
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.
Installation
To install shortuuid you need:
- Python 3.6+
If you have the dependencies, you have multiple options of installation:
- With pip (preferred), do
pip install shortuuid. - With setuptools, do
easy_install shortuuid. - To install the source, download it from
https://github.com/stochastic-technologies/shortuuid and run
python setup.py install.
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")
'exu3DTbj2ncsn9tLdLWspw'
>>> shortuuid.uuid(name="<http://example.com>")
'shortuuid.uuid(name="<http://example.com>")'
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():