Skip to main content

A lightweight Dependency Injection (DI) container for Python

Project description

Pynjector

PyPI

A lightweight Dependency Injection (DI) container for Python that allows automatic resolution of dependencies based on constructor type hints.

🚀 Features

  • Simple and intuitive API
  • Automatic dependency resolution based on type hints
  • Supports binding classes, factory functions, and pre-initialized instances
  • Inspired by C#'s dependency injection approach

📦 Installation

Install via pip:

pip install pynjector

Install via poetry:

poetry add pynjector

🎯 Usage

from src.pynjector import DIContainer


class Database:
    def query(self) -> str:
        return "Data from Database"


class Service:
    def __init__(self, db: Database):
        self.db = db

    def get_data(self) -> str:
        return self.db.query()


# Create the pynjector
container = DIContainer()

# Register dependencies
container.bind(Database)

# Resolve a service instance
service = container.resolve(Service)

print(service.get_data())  # Output: "Data from Database"

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

pynjector-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

pynjector-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pynjector-0.1.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for pynjector-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1b9a0c81c1aed41b6921bd2e446d20ab2bb7fad907d3b62c60c0a8f8114443df
MD5 70e864b15a0a0e5aa1d3a07fbbe207db
BLAKE2b-256 86556f46503f187d76cac91bb8cc7422792a722165175817a470d4d9ccf81c7f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pynjector-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for pynjector-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dae3f7644b47f8f66f8db577e8a4720912695da5fb550d9c3184e0c1d1b16d54
MD5 ab5c01ac2b38785c2cd735a90937ed27
BLAKE2b-256 dd685f5575dc1669430f7100890428c399d79b05668002d7caaaa6ad5de271df

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