A comprehensive Python factory pattern implementation with thread-safe operations and type-safe generics
Project description
Sweet Tea Factory System
A comprehensive, production-ready Python factory pattern implementation with advanced features for building extensible applications.
🚀 Features
- Dual Factory Patterns: Class-based factories (new instances) AND instance-based singletons (shared instances)
- Thread-Safe Registry: Concurrent operations with RLock synchronization
- Type-Safe Generics: Full TypeVar support with
__class_getitem__ - Flexible Key Matching: Support for ClassName, class_name, classname variations
- Optional Dependencies: Graceful handling with custom warnings
- Auto-Registration: Classes automatically registered via package imports
- Comprehensive Testing: 58 tests with 97% coverage
- Rich Documentation: MkDocs with Dracula theme and API reference
📦 Installation
Using uv (Recommended)
uv add sweet-tea
Using pip
pip install sweet-tea
Using Poetry
poetry add sweet-tea
🏁 Quick Start
from sweet_tea import Registry, Factory, AbstractFactory, SingletonFactory
# === CLASS-BASED FACTORY (Creates new instances each time) ===
Registry.register("database", DatabaseConnection)
db1 = Factory.create("database", configuration={"host": "server1"})
db2 = Factory.create("database", configuration={"host": "server2"})
# db1 ≠ db2 (different instances)
# === INSTANCE-BASED SINGLETON FACTORY (Reuses same instance) ===
db_connection = DatabaseConnection(host="prod-db", pool_size=10)
SingletonFactory.register("database", db_connection)
db3 = SingletonFactory.get("database")
db4 = SingletonFactory.get("database")
# db3 === db4 (same instance)
# === TYPE-SAFE ABSTRACT FACTORIES ===
class DatabaseInterface:
def connect(self) -> str: ...
db_factory = AbstractFactory[DatabaseInterface]
db = db_factory.create("postgres") # Only classes implementing DatabaseInterface
Three Factory Patterns
- Factory - Class registration → New instances with configuration
- AbstractFactory - Type-constrained → New instances with type safety
- SingletonFactory - Instance registration → Shared singleton instances
📖 Documentation
Complete documentation is available at https://snoodleboot-io.github.io/sweet_tea/
User Guides
- Getting Started - Installation and setup
- Basic Usage - Core factory patterns
- Advanced Features - Type constraints and threading
API Reference
- Registry - Global class registry
- Factory - Basic factory implementation
- AbstractFactory - Generic type-constrained factories
Development
- Contributing - Development setup and guidelines
- Testing - Testing strategy and coverage
🔧 Development
Prerequisites
- Python 3.10 or higher
- uv package manager (recommended)
Setup
# Clone the repository
git clone https://github.com/snoodleboot-io/sweet_tea.git
cd sweet_tea
# Install development dependencies
uv sync
# Install pre-commit hooks
uv run pre-commit install
# Run tests
uv run pytest
# Build documentation locally
uv run mkdocs serve
Code Quality
This project uses several tools to maintain code quality:
- Black: Code formatting
- isort: Import sorting (compatible with Black)
- Ruff: Fast linting and additional formatting
- Bandit: Security scanning
- MyPy: Type checking
All tools run automatically via pre-commit hooks on git commit.
🤝 Contributing
We welcome contributions! Please see our contributing guide for details.
Development Workflow
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and ensure they pass
- Update documentation if needed
- Submit a pull request
📄 License
Copyright © 2025 snoodleboot, LLC. Licensed under the Apache License 2.0.
See LICENSE for the full license text.
🙏 Acknowledgments
- Built with Pydantic for data validation
- Documentation powered by MkDocs with Material theme
- Testing framework: pytest with coverage
- Code quality: Black, Ruff, MyPy
Sweet Tea Factory System - Production-ready factory patterns for Python applications.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sweet_tea-0.1.21.tar.gz.
File metadata
- Download URL: sweet_tea-0.1.21.tar.gz
- Upload date:
- Size: 30.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b250330fc80151913b31380f6b00c4dbee68ae54e932ea4342ddf400d9c9901d
|
|
| MD5 |
7a797d0c3526fc4cb28ceaeda4f8a5f7
|
|
| BLAKE2b-256 |
a6c7c420d999c99e623df344b20dc4c0118c65b85eb6032f303916f90edf677b
|
File details
Details for the file sweet_tea-0.1.21-py3-none-any.whl.
File metadata
- Download URL: sweet_tea-0.1.21-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
328f939f20816b85df642ab388b369fa4f239107f154bb46f540b4d7ce6ea2e1
|
|
| MD5 |
b84bbdae304c508316322c479bd4982b
|
|
| BLAKE2b-256 |
35b6ab327b373fd7c56075c08dca92244d8bd8d54061b9c64a20088639c01146
|