Random Name and Slug Generator
coolname is a Python library for generating random, human-readable names and slugs. It offers various functionalities, including generating names as sequences of words, creating slugs for use in web applications like Django, and generating names of specific lengths. The current version is 4.1.0, and it maintains an active development and release cadence.
Common errors
-
ModuleNotFoundError: No module named 'coolname'
cause The 'coolname' package is not installed in the Python environment you are currently using or is not accessible on the Python path.fixInstall the library using pip: `pip install coolname` -
ImportError: cannot import name 'non_existent_function' from 'coolname'
cause You are trying to import a function or class that either does not exist within the 'coolname' package or is misspelled, or you are trying to import something that is not directly exposed at the top level of the package.fixEnsure the function or class name is spelled correctly and that it is indeed part of the 'coolname' library's public API. Common functions are `generate` and `generate_slug`. For example: `from coolname import generate_slug` or `from coolname import generate`. -
if name[0] == 'R' or 'r': # Incorrect boolean logic always returns true
cause This is a common Python beginner mistake where the boolean `or` operator is misunderstood. The string literal `'r'` is always considered 'truthy' in Python, making the entire condition `True` regardless of `name[0] == 'R'`.fixEach condition in an `or` statement must be complete and self-contained. The correct way to write this is: `if name[0] == 'R' or name[0] == 'r':` or more Pythonically: `if name[0] in ('R', 'r'):`
Warnings
- breaking The `coolname` library requires Python 3.10 or newer. Installing on older Python versions will fail.
- gotcha Calling `generate_slug()` without arguments returns a name of random length, with a higher probability for 4-word names. If you require a consistent number of words, always specify the length (e.g., `generate_slug(2)`). Prepositions and articles ('of', 'from', 'the') are not counted towards the specified word length.
- gotcha Customizing word lists or generation rules requires using the `RandomGenerator` class and defining a configuration dictionary. This is more involved than using the simpler `generate()` or `generate_slug()` functions.
Install
-
pip install coolname
Imports
- generate_slug
from coolname import generate_slug
- generate
from coolname import generate
- RandomGenerator
from coolname import RandomGenerator
Quickstart
from coolname import generate_slug, generate, RandomGenerator
# Generate a random slug (default length, often 4 words)
print(f"Random slug: {generate_slug()}")
# Generate a slug with a specific number of words
print(f"2-word slug: {generate_slug(2)}")
print(f"3-word slug: {generate_slug(3)}")
# Generate a name as a list of words
name_words = generate()
print(f"Random name (list): {name_words}")
print(f"Joined name: {' '.join(name_words)}")
# Example of custom generator with custom word lists
custom_generator = RandomGenerator({
'all': {
'type': 'cartesian',
'lists': ['adjectives', 'animals']
},
'adjectives': {
'type': 'words',
'words': ['quick', 'sleepy', 'bright']
},
'animals': {
'type': 'words',
'words': ['fox', 'panda', 'owl']
}
})
print(f"Custom generated slug: {custom_generator.generate_slug()}")