Skip to content

Easy Generator

py_simple.easy_generator

easy_generator helps generate things faster.

EasyGeneratorError

Bases: Exception

Raised when a value can't be generated.

Wraps whatever the underlying operation raises internally (bad length/character counts, missing data, qrcode/PIL errors, etc.) so py_simple functions can fail with one consistent, easy-to-read exception instead of a random builtin or library-specific one.

Parameters:

Name Type Description Default
message str

Human-readable description of what went wrong.

required

generate_api_key()

Generates a secure, random, URL-safe API key in one call, using Python's secrets module so the result is safe for tokens, API keys, and other security-sensitive values.

Returns:

Name Type Description
str str

A random, URL-safe text string suitable for use as an API key or access token.

Example
from py_simple import generate_api_key

key = generate_api_key()
import secrets

key = secrets.token_urlsafe(64)

generate_coupon_code(length=8, prefix='')

Generates a short coupon code made of uppercase letters and digits.

Characters that are easy to mix up (0, O, 1, and I) are left out, so a code is easier to read aloud or type from a receipt.

Parameters:

Name Type Description Default
length int

How many random characters to add after the prefix. Defaults to 8.

8
prefix str

Text placed at the start of the code, such as "SAVE". Defaults to an empty string.

''

Returns:

Name Type Description
str str

The coupon code, for example "SAVE-K7MNP2QX". A hyphen is added between the prefix and the random part when a prefix is given.

Raises:

Type Description
EasyGeneratorError

If length is less than 1.

Example
from py_simple import generate_coupon_code

code = generate_coupon_code(prefix="SAVE")
import secrets

alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
random_part = "".join(secrets.choice(alphabet) for _ in range(8))
code = f"SAVE-{random_part}"

generate_otp(length=4, with_letters=False)

Generates a random one-time password (OTP) code of a given length in one call, handling the character-pool selection every OTP generator needs.

Parameters:

Name Type Description Default
length int

Number of characters in the generated code. Defaults to 4.

4
with_letters bool

If True, the code is drawn from both letters and digits. If False, the code is digits only. Defaults to False.

False

Returns:

Name Type Description
str str

The generated OTP code.

Example
from py_simple import generate_otp

code = generate_otp(6, with_letters=True)
import random
import string

length = 6
otp_chars = string.ascii_letters + string.digits
code = "".join(random.choice(otp_chars) for _ in range(length))

generate_passphrase(words=4, separator='-')

Generates a random, easy-to-remember passphrase in one call, like
"Brave-river-Calm-lamp-47". It is the readable counterpart to
`generate_password`: words alternate between adjectives and nouns,
each one is randomly capitalized, and a 2-digit number is added at
the end, all picked with the `secrets` module.

For high-security needs, prefer `generate_password`, or ask for more
words: each extra word makes the passphrase stronger.

Args:
    words (int, optional): Number of words in the passphrase, not
        counting the number at the end. Defaults to `4`.
    separator (str, optional): The character placed between words.
        Defaults to `-`.

Returns:
    str: The generated passphrase (e.g., "Brave-river-Calm-lamp-47").

Raises:
    EasyGeneratorError: If `words` is less than 1.

Example:
    === "The Py_simple Way"

python from py_simple import generate_passphrase passphrase = generate_passphrase(separator="_")

    === "The Traditional Way"

python import secrets adjectives = ["brave", "calm", "bright", "quick"] nouns = ["river", "lamp", "cloud", "stone"] words = [] for i in range(4): word = secrets.choice(adjectives if i % 2 == 0 else nouns) if secrets.randbelow(2): word = word.capitalize() words.append(word) words.append(str(secrets.randbelow(90) + 10)) passphrase = "_".join(words)

generate_password(pass_length=12, uppercase_chars=2, digit_chars=2, special_chars=2)

Generates a randomized password of a given length in one call, handling the character-pool selection, shuffling, and repeated-character checks every password generator needs, instead of writing that logic by hand each time.

Parameters:

Name Type Description Default
pass_length int

Total number of characters in the generated password. Defaults to 12.

12
uppercase_chars int

Number of uppercase letters to include. Defaults to 2.

2
digit_chars int

Number of digits to include. Defaults to 2.

2
special_chars int

Number of punctuation characters to include. Defaults to 2.

2

Returns:

Name Type Description
str str

The generated password, made up of lowercase letters, uppercase letters, digits, and special characters shuffled together, with no two identical characters placed next to each other. The number of lowercase letters is calculated as pass_length - (uppercase_chars + digit_chars + special_chars).

Example
from py_simple import generate_password

password = generate_password(16, uppercase_chars=3, digit_chars=3, special_chars=3)
import string
import random

length = 16
uppercase_chars = 3
digit_chars = 3
special_chars = 3
lowercase_chars = length - (uppercase_chars + digit_chars + special_chars)

chars = (
    [random.choice(string.ascii_lowercase) for _ in range(lowercase_chars)]
    + [random.choice(string.ascii_uppercase) for _ in range(uppercase_chars)]
    + [random.choice(string.digits) for _ in range(digit_chars)]
    + [random.choice(string.punctuation) for _ in range(special_chars)]
)
random.shuffle(chars)
password = ''.join(chars)

generate_pin(length=4)

Generates a numeric PIN code of a chosen length in one call.

Parameters:

Name Type Description Default
length int

Number of digits in the generated PIN. Defaults to 4.

4

Returns:

Name Type Description
str str

A numeric PIN string that keeps leading zeros when they are generated.

Raises:

Type Description
EasyGeneratorError

If length is less than 1.

Example
from py_simple import generate_pin

pin = generate_pin(6)
import secrets

pin = "".join(str(secrets.randbelow(10)) for _ in range(6))

generate_qr_code(data_to_encode)

Generates a QR code image from the given data and saves it to disk in one call, handling the qrcode image creation and non-overwriting filename selection every QR code generator needs.

Parameters:

Name Type Description Default
data_to_encode str

The text or data to encode in the QR code (e.g. a URL, message, or other string).

required

Returns:

Name Type Description
None None

The QR code is saved directly to disk as qrcode{num}.png, where num is the smallest non-negative integer that does not collide with an existing file in the current directory.

Raises:

Type Description
EasyGeneratorError

If data_to_encode is empty, or if the QR code image can't be created or saved (e.g. an underlying qrcode/PIL error).

Example
from py_simple import generate_qr_code

generate_qr_code("[https://example.com](https://example.com)")
import qrcode
import os

data = "[https://example.com](https://example.com)"
img = qrcode.make(data)

num = 0
while os.path.exists(f'qrcode{num}.png'):
    num += 1
img.save(f'qrcode{num}.png')

generate_slug(text)

Generates a slug(URL-friendly string) from string.

Parameters:

Name Type Description Default
text str

The text to be converted to a slug.

required

Returns:

Name Type Description
str str

A URL-friendly slug.

Raises:

Type Description
EasyGeneratorError

If str is empty, or if the

Example
from py_simple import generate_slug

text = "Hello This-Becomes_A slug"
slug = generate_slug(text)
print(slug)  # 'hello-this-becomes-a-slug'
import re
import unicodedata

text = "Hello This-Becomes_A slug"

normalized = unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode("ascii")
lowercased = normalized.lower()
slug = re.sub(r"[^a-z0-9]+", "-", lowercased).strip("-")
print(slug)

generate_username(separator='-')

Generates a random, friendly username using a combination of a random adjective, noun, and number, handling the word-pool selection in one call.

Parameters:

Name Type Description Default
separator str

The character used to separate words in the username. Defaults to -.

'-'

Returns:

Name Type Description
str str

A randomly generated username string (e.g., "swift-coder-42").

Example
from py_simple import generate_username

username = generate_username(separator="_")
import random

adjectives = ["swift", "clever", "brave", "calm"]
nouns = ["coder", "hacker", "ninja", "wizard"]
username = f"{random.choice(adjectives)}_{random.choice(nouns)}_{random.randint(10, 99)}"

generate_uuid()

Generates a random UUID (version 4) as a string in one call, so you don't need to import uuid and remember which version to use.

Returns:

Name Type Description
str str

A randomly generated UUID, formatted as the standard 8-4-4-4-12 hex string (e.g. "3f2504e0-4f89-11d3-9a0c-0305e82c3301").

Example
from py_simple import generate_uuid

result = generate_uuid()  # -> "3f2504e0-4f89-11d3-9a0c-0305e82c3301"
import uuid

result = str(uuid.uuid4())