Convert Figma logo to code with AI

lithammer logoshortuuid

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

1,170
57
1,170
4

Top Related Projects

5,250

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

4,432

Universally Unique Lexicographically Sortable Identifier (ULID) in Go

4,889

K-Sortable Globally Unique IDs

3,879

xid is a globally unique id generator thought for the web

3,421

Collision-resistant ids optimized for horizontal scaling and performance.

Quick Overview

ShortUUID is a Python library that generates concise, unambiguous, URL-safe UUIDs. It provides a way to create shorter, more user-friendly unique identifiers compared to standard UUIDs, while maintaining uniqueness and randomness.

Pros

  • Generates shorter, more readable UUIDs
  • Customizable alphabet for UUID generation
  • Supports encoding and decoding between standard UUIDs and short UUIDs
  • Compatible with Python 2.7 and 3.5+

Cons

  • Not as widely adopted as standard UUIDs
  • May require additional explanation or documentation in projects
  • Potential for confusion with other short ID generation libraries
  • Limited to 22 characters in length by default

Code Examples

Generate a short UUID:

import shortuuid

# Generate a short UUID
short_id = shortuuid.uuid()
print(short_id)  # Output: something like "3KoHMNLnZvKnxzKktiEbP8"

Encode and decode standard UUIDs:

import uuid
import shortuuid

# Encode a standard UUID to a short UUID
standard_uuid = uuid.uuid4()
short_id = shortuuid.encode(standard_uuid)
print(short_id)

# Decode a short UUID back to a standard UUID
decoded_uuid = shortuuid.decode(short_id)
print(decoded_uuid)

Use a custom alphabet:

import shortuuid

# Create a ShortUUID object with a custom alphabet
su = shortuuid.ShortUUID(alphabet="23456789ABCDEFGHJKLMNPQRSTUVWXYZ")

# Generate a short UUID using the custom alphabet
custom_id = su.uuid()
print(custom_id)

Getting Started

To use ShortUUID in your Python project, follow these steps:

  1. Install the library using pip:

    pip install shortuuid
    
  2. Import the library in your Python script:

    import shortuuid
    
  3. Generate a short UUID:

    short_id = shortuuid.uuid()
    print(short_id)
    

That's it! You can now start using ShortUUID to generate concise, unique identifiers in your project.

Competitor Comparisons

5,250

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

Pros of uuid

  • Widely adopted and battle-tested in production environments
  • Implements multiple UUID versions (v1, v3, v4, v5) for different use cases
  • Provides robust, cryptographically secure random number generation

Cons of uuid

  • Larger package size due to comprehensive implementation
  • More complex API with multiple functions for different UUID versions
  • Generates longer UUIDs (36 characters) which may not be ideal for all use cases

Code Comparison

uuid:

import "github.com/google/uuid"

id := uuid.New()
fmt.Printf("UUID: %s\n", id.String())

shortuuid:

import shortuuid

id = shortuuid.uuid()
print(f"Short UUID: {id}")

Key Differences

  • shortuuid focuses on generating shorter, URL-friendly UUIDs
  • uuid provides more flexibility with multiple UUID versions
  • shortuuid is simpler to use with a more straightforward API
  • uuid offers better compatibility with standard UUID formats
  • shortuuid is available in multiple programming languages, while uuid is primarily for Go

Use Cases

  • Choose uuid for applications requiring standard UUID compliance or multiple UUID versions
  • Opt for shortuuid when shorter, more user-friendly identifiers are preferred, especially in URLs or user-facing scenarios
4,432

Universally Unique Lexicographically Sortable Identifier (ULID) in Go

Pros of ULID

  • Lexicographically sortable, allowing for efficient database indexing
  • Includes a timestamp component, useful for time-based sorting and querying
  • Provides 128-bit compatibility with UUID systems

Cons of ULID

  • Slightly longer identifier length (26 characters) compared to ShortUUID
  • Less customizable in terms of alphabet and length options
  • May require additional parsing to extract timestamp information

Code Comparison

ULID:

id := ulid.New()
fmt.Printf("ULID: %s\n", id.String())

ShortUUID:

import shortuuid
id = shortuuid.uuid()
print(f"ShortUUID: {id}")

Key Differences

  • ULID is implemented in Go, while ShortUUID is primarily a Python library
  • ULID focuses on sortability and timestamp inclusion, whereas ShortUUID prioritizes shorter, customizable identifiers
  • ShortUUID offers more flexibility in terms of alphabet and length configuration
  • ULID provides better performance for time-based queries and sorting operations

Use Cases

  • ULID: Ideal for distributed systems requiring sortable, time-based identifiers
  • ShortUUID: Better suited for generating short, URL-friendly unique identifiers with customizable characteristics

Both libraries offer unique identifier generation, but cater to different priorities and use cases in software development.

4,889

K-Sortable Globally Unique IDs

Pros of ksuid

  • Time-sortable: KSUIDs include a timestamp, making them naturally sortable by creation time
  • Longer length: 27 characters vs. 22 for shortuuid, providing more uniqueness
  • Built-in command-line tool for generating and inspecting KSUIDs

Cons of ksuid

  • Less human-readable: Uses base62 encoding, which can be less readable than shortuuid's base57
  • Language-specific: Primarily designed for Go, while shortuuid supports multiple languages
  • Larger size: Takes up more storage space due to longer length

Code Comparison

ksuid:

id := ksuid.New()
fmt.Printf("KSUID: %s\n", id.String())

shortuuid:

import shortuuid
uuid = shortuuid.uuid()
print(f"ShortUUID: {uuid}")

Both libraries provide simple ways to generate unique identifiers, but ksuid offers additional features like time-sorting and inspection tools. shortuuid focuses on creating shorter, more human-friendly UUIDs across various programming languages. The choice between them depends on specific project requirements, such as sortability, readability, and language compatibility.

3,879

xid is a globally unique id generator thought for the web

Pros of xid

  • Faster generation of unique IDs
  • Smaller ID size (20 bytes) while maintaining uniqueness
  • Built-in sorting capability based on timestamp

Cons of xid

  • Less flexibility in ID format customization
  • Limited language support (primarily Go, with some community ports)
  • Not as widely adopted as shortuuid

Code Comparison

xid:

id := xid.New()
fmt.Printf("xid: %s\n", id.String())

shortuuid:

import shortuuid
id = shortuuid.uuid()
print(f"shortuuid: {id}")

Summary

xid and shortuuid are both libraries for generating unique identifiers, but they have different approaches and trade-offs. xid focuses on performance and compact size, making it ideal for high-throughput systems where space efficiency is crucial. It generates sortable IDs by default, which can be beneficial for certain use cases.

shortuuid, on the other hand, offers more flexibility in terms of ID format and is available in multiple programming languages. It generates more human-friendly IDs by default and allows for easy customization of the alphabet used for ID generation.

The choice between xid and shortuuid depends on specific project requirements, such as performance needs, desired ID format, and language compatibility.

3,421

Collision-resistant ids optimized for horizontal scaling and performance.

Pros of cuid

  • Designed for horizontal scalability and high-performance in distributed systems
  • Includes a timestamp component, allowing for time-based sorting
  • Provides collision-resistant IDs across multiple machines and processes

Cons of cuid

  • Longer ID length (25 characters) compared to shortuuid's default (22 characters)
  • Less flexibility in customizing the output format or alphabet

Code Comparison

shortuuid:

import shortuuid

# Generate a short UUID
uuid = shortuuid.uuid()
print(uuid)  # Output: something like "3k4NJN6QdxTMkCWbUJGe2Z"

cuid:

import { createId } from '@paralleldrive/cuid2';

// Generate a CUID
const id = createId();
console.log(id);  // Output: something like "ckwp1u3aw000001la8mbi2qs9"

Both libraries provide simple ways to generate unique identifiers, but they differ in their implementation and features. shortuuid focuses on creating shorter, more readable UUIDs, while cuid emphasizes scalability and performance in distributed systems. The choice between them depends on specific project requirements, such as ID length, sorting capabilities, and system architecture.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

shortuuid

Build Status Godoc

A Go library that generates concise, unambiguous, URL-safe UUIDs. Based on and compatible with the Python library shortuuid.

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 google/uuid 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.

Usage

package main

import (
	"fmt"

	"github.com/lithammer/shortuuid/v4"
)

func main() {
	u := shortuuid.New()
	fmt.Println(u) // KwSysDpxcBU9FNhGkn2dCf
}

To use UUID v5 (instead of the default v4), use NewWithNamespace(name string) instead of New().

shortuuid.NewWithNamespace("http://example.com")

It's possible to use a custom alphabet as well, though it has to be 57 characters long.

alphabet := "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxy="
shortuuid.NewWithAlphabet(alphabet) // iZsai==fWebXd5rLRWFB=u

Bring your own encoder! For example, base58 is popular among bitcoin.

package main

import (
	"fmt"

	"github.com/btcsuite/btcutil/base58"
	"github.com/google/uuid"
	"github.com/lithammer/shortuuid/v4"
)

type base58Encoder struct{}

func (enc base58Encoder) Encode(u uuid.UUID) string {
	return base58.Encode(u[:])
}

func (enc base58Encoder) Decode(s string) (uuid.UUID, error) {
	return uuid.FromBytes(base58.Decode(s))
}

func main() {
	enc := base58Encoder{}
	fmt.Println(shortuuid.NewWithEncoder(enc)) // 6R7VqaQHbzC1xwA5UueGe6
}

License

MIT