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
  • Lazy Singletons: SingletonFactory.create() for on-demand singleton instantiation
  • 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)

# === LAZY SINGLETON FACTORY (Creates and caches instances on-demand) ===
Registry.register("database", DatabaseConnection)
db3 = SingletonFactory.create("database", configuration={"host": "prod-db", "pool_size": 10})
db4 = SingletonFactory.create("database")  # Returns cached instance
# db3 === db4 (same cached 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 - Lazy singletons → Cached instances created on-demand

📖 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.28.tar.gz (30.5 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.28-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sweet_tea-0.1.28.tar.gz
  • Upload date:
  • Size: 30.5 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.28.tar.gz
Algorithm Hash digest
SHA256 72109c2c0f9bc2e79d43141aec85331c763e5a0066f380c704808f588ad3662a
MD5 28d3018e61bda98130744d44bec34a91
BLAKE2b-256 ea864f499c0d3715c31024ee56583a8f8b3a72ba396c3bd8204de98ed687a594

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sweet_tea-0.1.28-py3-none-any.whl
  • Upload date:
  • Size: 19.9 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.28-py3-none-any.whl
Algorithm Hash digest
SHA256 b45deb0ecf40d7e128f6f4ea600bacef5dcd53e85c39ee3b8b53a6d0a83d5ef3
MD5 882f19699e2147d339df92d945086701
BLAKE2b-256 4d509971d939ca374e346d0a1508476f7b9cdb0c8c16e0889740578fbd89441b

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