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.21.tar.gz (30.4 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.21-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

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

Hashes for sweet_tea-0.1.21.tar.gz
Algorithm Hash digest
SHA256 b250330fc80151913b31380f6b00c4dbee68ae54e932ea4342ddf400d9c9901d
MD5 7a797d0c3526fc4cb28ceaeda4f8a5f7
BLAKE2b-256 a6c7c420d999c99e623df344b20dc4c0118c65b85eb6032f303916f90edf677b

See more details on using hashes here.

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

Hashes for sweet_tea-0.1.21-py3-none-any.whl
Algorithm Hash digest
SHA256 328f939f20816b85df642ab388b369fa4f239107f154bb46f540b4d7ce6ea2e1
MD5 b84bbdae304c508316322c479bd4982b
BLAKE2b-256 35b6ab327b373fd7c56075c08dca92244d8bd8d54061b9c64a20088639c01146

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