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.19.tar.gz (30.0 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.19-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sweet_tea-0.1.19.tar.gz
  • Upload date:
  • Size: 30.0 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.19.tar.gz
Algorithm Hash digest
SHA256 c7c1eb4dc94a5ccf69c0db8f8bee38d4d89fa2c728f5472f245a1aa9c1e87c55
MD5 b9e733262ab0e256601eac2d776bed61
BLAKE2b-256 d39994a3521063a581f80f9807917b88e4f969d42dcb9253762ec9b0917fe088

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sweet_tea-0.1.19-py3-none-any.whl
  • Upload date:
  • Size: 18.6 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.19-py3-none-any.whl
Algorithm Hash digest
SHA256 abbffa512bc03525c96203a2337daafbb8bffab490993d82c4b646c952ffec40
MD5 90775760286494af2d1285172bbd8039
BLAKE2b-256 dd37b5fc5b57ba5f0f4905fd1f12123e9e6989ab2c0b840dd83eb73d2ab16caf

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