Skip to main content

Dependency Injection Container inspired by Inversify for Python

Project description

Inversify for Python

A dependency injection container inspired by Inversify for Python.

This library supports hierarchical containers, various binding types (toSelf, to, constant, dynamic), and scopes (singleton and transient).

Examples

from inversify import Container, injectable, inject_params

class Token:
    def __init__(self, name):
        self.name = name
    def __hash__(self):
        return hash(self.name)
    def __eq__(self, other):
        return isinstance(other, Token) and self.name == other.name

# Tokens for bindings
LOGGER_TOKEN = Token("LOGGER")
USER_SERVICE_TOKEN = Token("USER_SERVICE")

@injectable
class Logger:
    def __init__(self):
        self.name = "Parent Logger"
    def log(self, message: str):
        print(f"[{self.name}]: {message}")

@injectable
class CustomLogger:
    def __init__(self):
        self.name = "Child Logger"
    def log(self, message: str):
        print(f"[{self.name}]: {message}")

@injectable
class UserService:
    @inject_params({'logger': LOGGER_TOKEN})
    def __init__(self, logger):
        self.logger = logger
    def process(self):
        self.logger.log("Processing in UserService")

# Create parent container and bind tokens
parent_container = Container()
parent_container.bind(LOGGER_TOKEN).to(Logger).inSingletonScope()
parent_container.bind(USER_SERVICE_TOKEN).to(UserService).inTransientScope()

# Create child container and override the LOGGER_TOKEN binding
child_container = parent_container.create_child()
child_container.bind(LOGGER_TOKEN).to(CustomLogger).inSingletonScope()

# Get services from parent and child containers
parent_service = parent_container.get(USER_SERVICE_TOKEN)
child_service = child_container.get(USER_SERVICE_TOKEN)

parent_service.process()  # Uses Parent Logger
child_service.process()   # Uses Child Logger

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

pynversify-1.0.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

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

pynversify-1.0.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file pynversify-1.0.0.tar.gz.

File metadata

  • Download URL: pynversify-1.0.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for pynversify-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7bd3f9b4a0b10575b16fa32e0c9e143266030150443d0c3a8159a6fe07251c79
MD5 66bbdf792bb2051891a9a3b995cdd353
BLAKE2b-256 4cee6ffb60e218b0716f18997791fba4faa7d969056175d6812f2643c0c98248

See more details on using hashes here.

File details

Details for the file pynversify-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pynversify-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for pynversify-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6da7247987289342011b1a55c77de2b95d90823a8f15c6db26f2a7ceff614e3
MD5 e9533967b4eedd042d8f613763637923
BLAKE2b-256 0cb79c2bc92e69006777466efebf781ce349fe67f3a0ff151db24330504c3926

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