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