Skip to main content

A lightweight dependency injection framework for Python applications

Project description

a13x-depinj

A lightweight, yet powerful dependency injection framework for Python applications.

Features

  • Simple and intuitive API
  • YAML-based component configuration
  • Lazy component initialization
  • Automatic dependency management
  • Component lifecycle management
  • Type hints support
  • Context manager interface

Installation

pip install a13x-depinj

Quick Start

  1. Define your components:
from dataclasses import dataclass

@dataclass
class DatabaseConfig:
    url: str
    port: int

class Database:
    def __init__(self, config: dict):
        self.config = DatabaseConfig(**config)
        
    def connect(self):
        # Implementation
        pass

class UserService:
    def __init__(self, config: dict):
        self.db = ComponentRegistry().get(Database)
  1. Create a deployment configuration (deployment.yaml):
components:
  - module: myapp.database
    class: Database
    config_path: database.config
  - module: myapp.services
    class: UserService
    config_path: services.user

where config_path references a starting entry in the config.yaml file:

services:
    user:
        name: "a13x"
        email: "a13x.h.cc@gmail.com"

database:
    config:
        db_path: "data/market_data.db"
        enable_wal: true
        pragma_synchronous: "NORMAL"
        pragma_journal_mode: "WAL"
        pragma_cache_size: -2000  # 2MB cache
        max_connections: 10
  1. Initialize the registry:
from a13x_depinj import ComponentRegistry, Config

# Load application configuration
config = Config.load_config('config.yaml')

# Initialize components
with ComponentRegistry.from_yaml(config, 'deployment.yaml') as registry:
    # Get component instances
    db = registry.get(Database)
    user_service = registry.get(UserService)
    
    # Use components
    db.connect()

Advanced Usage

Lazy Initialization

Components can be initialized lazily when first accessed:

registry = ComponentRegistry.from_yaml(config, 'deployment.yaml', lazy=True)

Component Lifecycle Management

Components can implement cleanup methods:

class Database:
    def cleanup(self):
        # Cleanup resources
        pass

The registry will automatically call cleanup when using the context manager or when unregistering components.

Type Hints

The registry supports type hints for better IDE integration:

from typing import TypeVar, Type

T = TypeVar('T')
registry = ComponentRegistry[T]()
db: Database = registry.get(Database)

Contributing

Contributions are welcome! Please check our Contributing Guidelines for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

a13x_depinj-0.1.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

a13x_depinj-0.1.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file a13x_depinj-0.1.1.tar.gz.

File metadata

  • Download URL: a13x_depinj-0.1.1.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Linux/6.8.0-52-generic

File hashes

Hashes for a13x_depinj-0.1.1.tar.gz
Algorithm Hash digest
SHA256 607231e0e66e5738b50455ec200c5db68ee698b5a36782a46d93448a07ef73a6
MD5 f05f24587358dd4b7c6d87fcf0c25296
BLAKE2b-256 2d13f3b4ab5507af25810ef5e4b8101371fd5c1c9d8c1b839e67af0bae358470

See more details on using hashes here.

File details

Details for the file a13x_depinj-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: a13x_depinj-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Linux/6.8.0-52-generic

File hashes

Hashes for a13x_depinj-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48f0bd4ce99e9c3d066ac57fe7e7acf9cd6bff166c9717f29f292464393d72c1
MD5 66611f4516dd7255e5a037f00239f573
BLAKE2b-256 5a5a2262aad72dbdbf4acc05402eec8e22ee885c5d8e51081f60b3f9f8fcc7e4

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