| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: shortuuid |
| 3 |
Version: 1.0.13 |
| 4 |
Summary: A generator library for concise, unambiguous and URL-safe UUIDs. |
| 5 |
Home-page: https://github.com/skorokithakis/shortuuid/ |
| 6 |
License: BSD-3-Clause |
| 7 |
Author: Stavros Korokithakis |
| 8 |
Author-email: [email protected] |
| 9 |
Requires-Python: >=3.6 |
| 10 |
Classifier: License :: OSI Approved :: BSD License |
| 11 |
Classifier: Programming Language :: Python |
| 12 |
Classifier: Programming Language :: Python :: 3 |
| 13 |
Classifier: Programming Language :: Python :: 3.6 |
| 14 |
Classifier: Programming Language :: Python :: 3.7 |
| 15 |
Classifier: Programming Language :: Python :: 3.8 |
| 16 |
Classifier: Programming Language :: Python :: 3.9 |
| 17 |
Classifier: Programming Language :: Python :: 3.10 |
| 18 |
Classifier: Programming Language :: Python :: 3.11 |
| 19 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
| 20 |
Description-Content-Type: text/markdown |
| 21 |
|
| 22 |
Description |
| 23 |
=========== |
| 24 |
|
| 25 |
`shortuuid` is a simple python library that generates concise, unambiguous, URL-safe |
| 26 |
UUIDs. |
| 27 |
|
| 28 |
Often, one needs to use non-sequential IDs in places where users will see them, but the |
| 29 |
IDs must be as concise and easy to use as possible. `shortuuid` solves this problem by |
| 30 |
generating uuids using Python's built-in `uuid` module and then translating them to |
| 31 |
base57 using lowercase and uppercase letters and digits, and removing similar-looking |
| 32 |
characters such as l, 1, I, O and 0. |
| 33 |
|
| 34 |
[](https://travis-ci.org/skorokithakis/shortuuid) |
| 35 |
|
| 36 |
|
| 37 |
Installation |
| 38 |
------------ |
| 39 |
|
| 40 |
To install `shortuuid` you need: |
| 41 |
|
| 42 |
- Python 3.6+ |
| 43 |
|
| 44 |
If you have the dependencies, you have multiple options of installation: |
| 45 |
|
| 46 |
- With pip (preferred), do `pip install shortuuid`. |
| 47 |
- With setuptools, do `easy_install shortuuid`. |
| 48 |
- To install the source, download it from |
| 49 |
https://github.com/stochastic-technologies/shortuuid and run `python setup.py |
| 50 |
install`. |
| 51 |
|
| 52 |
|
| 53 |
Usage |
| 54 |
----- |
| 55 |
|
| 56 |
To use `shortuuid`, just import it in your project like so: |
| 57 |
|
| 58 |
```python |
| 59 |
>>> import shortuuid |
| 60 |
``` |
| 61 |
|
| 62 |
You can then generate a short UUID: |
| 63 |
|
| 64 |
```python |
| 65 |
>>> shortuuid.uuid() |
| 66 |
'vytxeTZskVKR7C7WgdSP3d' |
| 67 |
``` |
| 68 |
|
| 69 |
If you prefer a version 5 UUID, you can pass a name (DNS or URL) to the call and it will |
| 70 |
be used as a namespace (`uuid.NAMESPACE_DNS` or `uuid.NAMESPACE_URL`) for the resulting |
| 71 |
UUID: |
| 72 |
|
| 73 |
```python |
| 74 |
>>> shortuuid.uuid(name="example.com") |
| 75 |
'exu3DTbj2ncsn9tLdLWspw' |
| 76 |
|
| 77 |
>>> shortuuid.uuid(name="<http://example.com>") |
| 78 |
'shortuuid.uuid(name="<http://example.com>")' |
| 79 |
``` |
| 80 |
|
| 81 |
You can also generate a cryptographically secure random string (using `os.urandom()` |
| 82 |
internally) with: |
| 83 |
|
| 84 |
```python |
| 85 |
>>> shortuuid.ShortUUID().random(length=22) |
| 86 |
'RaF56o2r58hTKT7AYS9doj' |
| 87 |
``` |
| 88 |
|
| 89 |
To see the alphabet that is being used to generate new UUIDs: |
| 90 |
|
| 91 |
```python |
| 92 |
>>> shortuuid.get_alphabet() |
| 93 |
'23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
| 94 |
``` |
| 95 |
|
| 96 |
If you want to use your own alphabet to generate UUIDs, use `set_alphabet()`: |
| 97 |
|
| 98 |
```python |
| 99 |
>>> shortuuid.set_alphabet("aaaaabcdefgh1230123") |
| 100 |
>>> shortuuid.uuid() |
| 101 |
'0agee20aa1hehebcagddhedddc0d2chhab3b' |
| 102 |
``` |
| 103 |
|
| 104 |
The default alphabet matches the regex `[2-9A-HJ-NP-Za-km-z]{22}`. |
| 105 |
|
| 106 |
`shortuuid` will automatically sort and remove duplicates from your alphabet to ensure |
| 107 |
consistency: |
| 108 |
|
| 109 |
```python |
| 110 |
>>> shortuuid.get_alphabet() |
| 111 |
'0123abcdefgh' |
| 112 |
``` |
| 113 |
|
| 114 |
If the default 22 digits are too long for you, you can get shorter IDs by just |
| 115 |
truncating the string to the desired length. The IDs won't be universally unique any |
| 116 |
longer, but the probability of a collision will still be very low. |
| 117 |
|
| 118 |
To serialize existing UUIDs, use `encode()` and `decode()`: |
| 119 |
|
| 120 |
```python |
| 121 |
>>> import uuid |
| 122 |
>>> u = uuid.uuid4() |
| 123 |
>>> u |
| 124 |
UUID('6ca4f0f8-2508-4bac-b8f1-5d1e3da2247a') |
| 125 |
|
| 126 |
>>> s = shortuuid.encode(u) |
| 127 |
>>> s |
| 128 |
'MLpZDiEXM4VsUryR9oE8uc' |
| 129 |
|
| 130 |
>>> shortuuid.decode(s) == u |
| 131 |
True |
| 132 |
|
| 133 |
>>> short = s[:7] |
| 134 |
>>> short |
| 135 |
'MLpZDiE' |
| 136 |
|
| 137 |
>>> h = shortuuid.decode(short) |
| 138 |
UUID('00000000-0000-0000-0000-009a5b27f8b9') |
| 139 |
|
| 140 |
>>> shortuuid.decode(shortuuid.encode(h)) == h |
| 141 |
True |
| 142 |
``` |
| 143 |
|
| 144 |
|
| 145 |
Class-based usage |
| 146 |
----------------- |
| 147 |
|
| 148 |
If you need to have various alphabets per-thread, you can use the `ShortUUID` class, |
| 149 |
like so: |
| 150 |
|
| 151 |
```python |
| 152 |
>>> su = shortuuid.ShortUUID(alphabet="01345678") |
| 153 |
>>> su.uuid() |
| 154 |
'034636353306816784480643806546503818874456' |
| 155 |
|
| 156 |
>>> su.get_alphabet() |
| 157 |
'01345678' |
| 158 |
|
| 159 |
>>> su.set_alphabet("21345687654123456") |
| 160 |
>>> su.get_alphabet() |
| 161 |
'12345678' |
| 162 |
``` |
| 163 |
|
| 164 |
|
| 165 |
Command-line usage |
| 166 |
------------------ |
| 167 |
|
| 168 |
`shortuuid` provides a simple way to generate a short UUID in a terminal: |
| 169 |
|
| 170 |
```bash |
| 171 |
$ shortuuid |
| 172 |
fZpeF6gcskHbSpTgpQCkcJ |
| 173 |
``` |
| 174 |
|
| 175 |
|
| 176 |
Django field |
| 177 |
------------ |
| 178 |
|
| 179 |
`shortuuid` includes a Django field that generates random short UUIDs by default, for |
| 180 |
your convenience: |
| 181 |
|
| 182 |
```python |
| 183 |
from shortuuid.django_fields import ShortUUIDField |
| 184 |
|
| 185 |
class MyModel(models.Model): |
| 186 |
# A primary key ID of length 16 and a short alphabet. |
| 187 |
id = ShortUUIDField( |
| 188 |
length=16, |
| 189 |
max_length=40, |
| 190 |
prefix="id_", |
| 191 |
alphabet="abcdefg1234", |
| 192 |
primary_key=True, |
| 193 |
) |
| 194 |
|
| 195 |
# A short UUID of length 22 and the default alphabet. |
| 196 |
api_key = ShortUUIDField() |
| 197 |
``` |
| 198 |
|
| 199 |
The field is the same as the `CharField`, with a `length` argument (the length of the |
| 200 |
ID), an `alphabet` argument, and the `default` argument removed. Everything else is |
| 201 |
exactly the same, e.g. `index`, `help_text`, `max_length`, etc. |
| 202 |
|
| 203 |
|
| 204 |
Compatibility note |
| 205 |
------------------ |
| 206 |
|
| 207 |
Versions of ShortUUID prior to 1.0.0 generated UUIDs with their MSB last, i.e. reversed. |
| 208 |
This was later fixed, but if you have some UUIDs stored as a string with the old method, |
| 209 |
you need to pass `legacy=True` to `decode()` when converting your strings back to UUIDs. |
| 210 |
|
| 211 |
That option will go away in the future, so you will want to convert your UUIDs to |
| 212 |
strings using the new method. This can be done like so: |
| 213 |
|
| 214 |
```python |
| 215 |
>>> new_uuid_str = encode(decode(old_uuid_str, legacy=True)) |
| 216 |
``` |
| 217 |
|
| 218 |
License |
| 219 |
------- |
| 220 |
|
| 221 |
`shortuuid` is distributed under the BSD license. |
| 222 |
|