### Django ShortUUIDField Example Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Provides an example of integrating the ShortUUIDField into a Django model for primary keys or other fields. ```python 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, max_length=40, prefix="id_", alphabet="abcdefg1234", dont_sort_alphabet=False primary_key=True, ) # A short UUID of length 22 and the default alphabet. api_key = ShortUUIDField() ``` -------------------------------- ### Command-line UUID Generation Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Shows how to generate a short UUID directly from the command line using the installed shortuuid package. ```bash $ shortuuid fZpeF6gcskHbSpTgpQCkcJ ``` -------------------------------- ### Get and Set Alphabet Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Demonstrates how to retrieve the current alphabet used for UUID generation and how to set a custom alphabet. ```python >>> shortuuid.get_alphabet() '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' >>> shortuuid.set_alphabet("aaaaabcdefgh1230123") >>> shortuuid.uuid() '0agee20aa1hehebcagddhedddc0d2chhab3b' ``` -------------------------------- ### Import and Generate Short UUID Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Demonstrates how to import the shortuuid library and generate a default short UUID. ```python >>> import shortuuid >>> shortuuid.uuid() 'vytxeTZskVKR7C7WgdSP3d' ``` -------------------------------- ### Generate Version 5 Short UUID Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Shows how to generate a version 5 UUID using a provided name or namespace. ```python >>> shortuuid.uuid(name="example.com") 'exu3DTbj2ncsn9tLdLWspw' >>> shortuuid.uuid(name="") 'shortuuid.uuid(name="")' ``` -------------------------------- ### Encode and Decode UUIDs with Legacy Support in Python Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Demonstrates how to encode and decode UUID strings using the shortuuid library. It emphasizes the use of `legacy=True` in the `decode` function for compatibility with older UUID formats and shows the recommended way to convert UUIDs to strings for future compatibility. ```Python >>> new_uuid_str = encode(decode(old_uuid_str, legacy=True)) ``` -------------------------------- ### Class-based ShortUUID Usage Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Demonstrates using the ShortUUID class to manage different alphabets per thread or instance. ```python >>> su = shortuuid.ShortUUID(alphabet="01345678") >>> su.uuid() '034636353306816784480643806546503818874456' >>> su.get_alphabet() '01345678' >>> su.set_alphabet("21345687654123456") >>> su.get_alphabet() '12345678' ``` -------------------------------- ### Generate Random String Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Illustrates generating a cryptographically secure random string of a specified length using os.urandom(). ```python >>> shortuuid.ShortUUID().random(length=22) 'RaF56o2r58hTKT7AYS9doj' ``` -------------------------------- ### Encode and Decode UUIDs Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Shows how to serialize standard Python UUID objects into shortuuid strings and deserialize them back. ```python >>> import uuid >>> u = uuid.uuid4() >>> u UUID('6ca4f0f8-2508-4bac-b8f1-5d1e3da2247a') >>> s = shortuuid.encode(u) >>> s 'MLpZDiEXM4VsUryR9oE8uc' >>> shortuuid.decode(s) == u True >>> short = s[:7] >>> short 'MLpZDiE' >>> h = shortuuid.decode(short) UUID('00000000-0000-0000-0000-009a5b27f8b9') >>> shortuuid.decode(shortuuid.encode(h)) == h True ``` -------------------------------- ### Alphabet Sorting Control Source: https://github.com/skorokithakis/shortuuid/blob/master/README.md Explains how to prevent the alphabet from being sorted when using set_alphabet() for compatibility with other implementations. ```python >>> shortuuid.set_alphabet("aaaaabcdefgh1230123", dont_sort_alphabet=True) >>> shortuuid.get_alphabet() 'abcdefgh1230' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.