Skip to main content

A comprehensive Python factory pattern implementation with thread-safe operations and type-safe generics

Project description

Sweet Tea Factory System

CI PyPI version Python 3.10+ codecov License

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

  1. Factory - Class registration → New instances with configuration
  2. AbstractFactory - Type-constrained → New instances with type safety
  3. SingletonFactory - Instance registration → Shared singleton instances

📖 Documentation

Complete documentation is available at https://snoodleboot-io.github.io/sweet_tea/

User Guides

API Reference

Development

🔧 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and ensure they pass
  5. Update documentation if needed
  6. Submit a pull request

📄 License

Copyright © 2025 snoodleboot, LLC. Licensed under the Apache License 2.0.

See LICENSE for the full license text.

🙏 Acknowledgments


Sweet Tea Factory System - Production-ready factory patterns for Python applications.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sweet_tea-0.1.20.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sweet_tea-0.1.20-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file sweet_tea-0.1.20.tar.gz.

File metadata

  • Download URL: sweet_tea-0.1.20.tar.gz
  • Upload date:
  • Size: 30.3 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

Hashes for sweet_tea-0.1.20.tar.gz
Algorithm Hash digest
SHA256 75a96470124d53d2d3a589e0ced41801bf711f61f8bd77295d7dcaac8bcec4e5
MD5 26cd87dc0c1bd41e2c268cccc8ad9eae
BLAKE2b-256 7ecda025cf2273bd5ed4395ebe3fda22ac0b49d89c1336fafd59ce8996c8ee18

See more details on using hashes here.

File details

Details for the file sweet_tea-0.1.20-py3-none-any.whl.

File metadata

  • Download URL: sweet_tea-0.1.20-py3-none-any.whl
  • Upload date:
  • Size: 19.0 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

Hashes for sweet_tea-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 f7c9475e1669a84cb6e75de3a3898de4fe105b6dacf74044e7b69c4d7e239791
MD5 03dc4daf216ec839ad7561b9b309fa06
BLAKE2b-256 c17f23192da4b681286ac1027f00899649199c5e37200c8644b8329f8c3c0bdd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page