You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

pynversify

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pynversify

Dependency Injection Container inspired by Inversify for Python

1.0.2
pipPyPI
Maintainers
1

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

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.