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_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
|
with_letters
|
bool
|
If |
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_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
|
uppercase_chars
|
int
|
Number of uppercase letters to
include. Defaults to |
2
|
digit_chars
|
int
|
Number of digits to include.
Defaults to |
2
|
special_chars
|
int
|
Number of punctuation characters
to include. Defaults to |
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 |
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_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
|
Raises:
| Type | Description |
|---|---|
EasyGeneratorError
|
If |
Example
from py_simple import generate_qr_code
generate_qr_code("https://example.com")
import qrcode
import os
data = "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_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
|
Example
from py_simple import generate_uuid
result = generate_uuid() # -> "3f2504e0-4f89-11d3-9a0c-0305e82c3301"
import uuid
result = str(uuid.uuid4())