Skip to main content

A Python package for generating random slugs using a customizable vocabulary of words.

Project description

Random Slugs

random-slugs is a user-friendly Python package that generates those random word slugs. This package is customizable and allows users to specify the parts of speech, categories of words, and the reproducibility.

Features

  • Generate slugs with a specified number of words
  • Customize the parts of speech and categories of words used
  • Choose the format of the slug (kebab-case, snake_case or camelCase)
  • Set a random seed for reproducibility
  • Handle errors gracefully with custom exceptions

Installation

You can install random-slugs using pip:

pip install random-slugs

Usage

Here is a basic example of how to use the package:

from random_slugs import generate_slug

slug = generate_slug()
print(slug) # E.g. 'quick-brown-fox'

Customize the Slug

You can customize the slug generation by passing an options dictionary to the generate_slug function:

options = {
    "parts_of_speech": ["adjectives", "nouns"],
    "categories": {"adjectives": ["color"], "nouns": ["animal"]},
    "format": "snake",
    "seed": 42
}

slug = generate_slug(num_of_words=2, options=options)
print(slug)  # E.g. : 'brown_fox'

API

generate_slug

generate_slug(num_of_words: int = 3, options: Options = None) -> str

Generates a random slug.

Parameters:

  • num_of_words (int): The number of words to include in the slug. Defaults to 3.
  • options (Options): A dictionary of options to customize the slug generation.

Options:

  • parts_of_speech (List[str]): List of parts of speech to use. Must be one of: adjectives, nouns.

  • categories (Dict): A dictionary mapping parts of speech to categories of words. E.g. {"adjectives": ["color"], "nouns": ["animal"]}.

    • Adjective Categories:
      • color
      • condition
      • emotion
      • size
      • quantity
    • Noun Categories:
      • animal
      • color
      • pokemon
      • profession
      • technology
      • thing
      • transport
  • format (str): The format of the slug. Must be one of: kebab, snake, camel. Defaults to kebab.

  • seed (int): The random seed value to use for reproducibility.

Returns:

  • str: The generated slug.

Total Unique Slugs

The total number of unique slugs that can be generated is calculated as follows:

from random_slugs import get_total_unique_slugs

total = get_total_unique_slugs()
print(total)  # 6254859

Exceptions

  • RandomSlugsError: Base class for all exceptions in the package.
  • RandomSlugConfigError: Exception for configuration errors.

Example

Here is a complete example that includes custom options and error handling:

from random_slugs import generate_slug, RandomSlugConfigError

options = {
    "parts_of_speech": ["adjectives", "nouns"],
    "categories": {"adjectives": ["size"], "nouns": ["animal"]},
    "format": "camel",
    "seed": "example_seed"
}

try:
    slug = generate_slug(num_of_words=2, options=options)
    print(slug)  # E.g. "bigCat"
except RandomSlugConfigError as e:
    print(f"Configuration error: {e}")
except RandomSlugsError as e:
    print(f"Slug generation error: {e}")

Testing

You can run the test suite using the following command:

pytest tests

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

random_slugs-1.0.2.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

random_slugs-1.0.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file random_slugs-1.0.2.tar.gz.

File metadata

  • Download URL: random_slugs-1.0.2.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for random_slugs-1.0.2.tar.gz
Algorithm Hash digest
SHA256 762d0cecf3c0a83eda68eeaf0c7cf7fcea15d314b51c48602df292c94a39df72
MD5 4c90756c6a05a55d972067110b9824f2
BLAKE2b-256 5d860a92775a37a45d22c45452f243841c696cd9f8bd7f50e3bc913e9d6f4152

See more details on using hashes here.

File details

Details for the file random_slugs-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for random_slugs-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5d88935c259ad284be7b0e278a7318e5029e9e5bf7de5f21e64944a72455b4d2
MD5 eb212446b47dba80abb953a158ab44d9
BLAKE2b-256 a78e995cfacb9c33bc01f24499657514593ae222c60ae403e570dac983cd1b22

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page