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.1.tar.gz (3.0 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.1-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pynversify-1.0.1.tar.gz
  • Upload date:
  • Size: 3.0 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.1.tar.gz
Algorithm Hash digest
SHA256 b671db258b8f92697a1bc0739189cae90734e49d12dd9a756c55a52de39069a7
MD5 f13c0d5735bd732a2107794cb40ce38b
BLAKE2b-256 52115edc0a97f90b31b44b34b8eb33046015ec624815323a8b8b6683de1a2c41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pynversify-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 909b33d2d430739b80698ec1bf58138a31db721fde246cbcc2cb6f5a9cce3f9f
MD5 53c34a2792f074ac735f9c569ad315e9
BLAKE2b-256 219812475a1627cd863007789191d8d07b46d12e1b9a263edcea9ff455c46c0e

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