Skip to main content

samlibs-generate-password (Python)

PyPI version License: MIT Python 3.6+

A lightweight, fast, and optimized password generator library for Python. Unlike other libraries that require complex setup, this package provides simple functions that accept configuration parameters to generate secure passwords.

🚀 Features

  • Ultra Lightweight: Zero dependencies and optimized code
  • Fast Performance: Efficient character pool generation and selection
  • Type Hints: Full type annotations for better IDE support
  • Cryptographically Secure: Uses secrets module for secure random generation
  • Flexible API: Both function parameters and dictionary-based configuration
  • Character Exclusion: Exclude specific characters from password generation
  • Python 3.6+: Compatible with modern Python versions

📦 Installation

pip install samlibs-generate-password

🔧 Usage

Basic Usage

from samlibs_generate_password import generate_password

# Generate a simple password with default settings
password = generate_password()
print(password)  # e.g., "K9$mN7#qR2@x"

# Generate password with custom parameters
custom_password = generate_password(
    length=16,
    uppercase=True,
    lowercase=True,
    numbers=True,
    special=False,
    exclude=['0', 'O', 'l', '1']
)
print(custom_password)  # e.g., "KmN7qR2xPzWcJbVn"

Dictionary API (Node.js Style)

from samlibs_generate_password import generate_password_dict

# Generate password using dictionary configuration
password = generate_password_dict({
    'length': 20,
    'quantity': 1,
    'lowercase': True,
    'uppercase': True,
    'numbers': True,
    'special': True,
    'exclude': ['@', '#', '$']
})
print(password)

Multiple Passwords

# Generate multiple passwords
passwords = generate_password(length=12, quantity=5)
print(passwords)  # List of 5 passwords

Type Hints Support

from typing import List
from samlibs_generate_password import generate_password

# Type hints work perfectly
single_password: str = generate_password(quantity=1)
multiple_passwords: List[str] = generate_password(quantity=5)

⚙️ Function Parameters

generate_password() Parameters

Parameter Type Default Description
length int 12 Length of each password
quantity int 1 Number of passwords to generate
lowercase bool True Include lowercase letters (a-z)
uppercase bool True Include uppercase letters (A-Z)
numbers bool True Include numbers (0-9)
special bool True Include special characters (!@#$%^&*()_+-=[]{}
exclude List[str] None List of characters to exclude

Return Types

  • Single password (quantity=1): Returns str
  • Multiple passwords (quantity>1): Returns List[str]

📋 Examples

Character Type Control

# Only letters and numbers, 16 characters
password = generate_password(length=16, special=False)

# Only uppercase letters, 8 characters
password = generate_password(
    length=8,
    lowercase=False,
    numbers=False,
    special=False
)

# Only numbers, 6 characters (PIN-like)
pin = generate_password(
    length=6,
    lowercase=False,
    uppercase=False,
    special=False
)

Exclude Similar Characters

# Exclude confusing characters like 0, O, l, 1
password = generate_password(
    length=12,
    exclude=['0', 'O', 'l', '1', 'I']
)

# Database-safe password (no quotes or backslashes)
db_password = generate_password(
    length=32,
    special=False,
    exclude=['\\\\', '/', '"', "'", '`']
)

Batch Generation

# Generate 10 unique passwords
passwords = generate_password(length=15, quantity=10)
for i, pwd in enumerate(passwords, 1):
    print(f"Password {i}: {pwd}")

Error Handling

try:
    # This will raise ValueError
    password = generate_password(
        lowercase=False,
        uppercase=False,
        numbers=False,
        special=False
    )
except ValueError as e:
    print(f"Error: {e}")
    # Output: Error: At least one character set must be enabled

🔒 Security Features

  • Uses Python's secrets module for cryptographically secure randomness
  • No predictable patterns in password generation
  • Efficient character pool filtering to prevent bias
  • Secure random selection without replacement bias

🚀 Performance

This library is optimized for:

  • Minimal memory footprint - Zero dependencies
  • Fast character pool generation - Efficient string operations
  • Optimized exclusion filtering - Set-based operations for O(1) lookups
  • Bulk generation - Efficient batch password creation

Performance Benchmark

import time
from samlibs_generate_password import generate_password

# Benchmark: Generate 10,000 passwords
start_time = time.time()
passwords = generate_password(length=12, quantity=10000)
end_time = time.time()

print(f"Generated 10,000 passwords in {(end_time - start_time)*1000:.2f}ms")
# Typical output: Generated 10,000 passwords in 45.67ms

📊 Comparison with Other Libraries

# Other password libraries
from other_lib import PasswordGenerator
generator = PasswordGenerator(length=12, uppercase=True, ...)
password = generator.generate()

# samlibs-generate-password (simpler and faster)
from samlibs_generate_password import generate_password
password = generate_password(length=12, uppercase=True, ...)

🧪 Testing

Run the comprehensive test suite:

cd samlibs-generate-password-python
python test.py

The test suite includes:

  • ✅ 25 comprehensive test cases
  • ✅ Error handling validation
  • ✅ Performance benchmarks
  • ✅ Randomness verification
  • ✅ API compatibility tests

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

📄 License

MIT License - see LICENSE file for details.

🔗 Links

🆚 Related Projects


Made with ❤️ for Python developers who value simplicity and performance.

Metadata

Release files for samlibs-generate-password 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for samlibs-generate-password 1.0.0
File Size Uploaded Provenance
samlibs_generate_password-1.0.0.tar.gz 7.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for samlibs-generate-password 1.0.0
File Interpreter ABI Platform
samlibs_generate_password-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / samlibs_generate_password-1.0.0.tar.gz

Download URL samlibs_generate_password-1.0.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
26a2c07927bb8db790a7af5dd4048608e347694502284e13fe988e071fb05691
BLAKE2b-256 checksum
How to use checksums
65d1b5eebc753b4cdd914b2a37d88c38d11aa6dfde049053f03c461c0d4d5c50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / samlibs_generate_password-1.0.0-py3-none-any.whl

Download URL samlibs_generate_password-1.0.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07f9385ba0e234c39fd6bff4cc401abfea8c0df4dd0ff0f2868ba4d1b3cbeec3
BLAKE2b-256 checksum
How to use checksums
d7de6bee318405dfcf4db75e3617928be40a4f15f55cfe279c67b9f0f79873f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Security for samlibs-generate-password 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page