🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

python-injection

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

python-injection

Fast and easy dependency injection framework.

0.18.10
PyPI
Maintainers
1

python-injection

CI PyPI Ruff

Fast and easy dependency injection framework.

Installation

⚠️ Requires Python 3.12 or higher

pip install python-injection

Motivations

  • Easy to use
  • No impact on class and function definitions
  • Easily interchangeable dependencies (depending on the runtime environment, for example)
  • No prerequisites

Quick start

Simply apply the decorators and the package takes care of the rest.

from injection import injectable, inject, singleton

@singleton
class Printer:
    def __init__(self):
        self.history = []

    def print(self, message: str):
        self.history.append(message)
        print(message)

@injectable
class Service:
    def __init__(self, printer: Printer):
        self.printer = printer

    def hello(self):
        self.printer.print("Hello world!")

@inject
def main(service: Service):
    service.hello()

if __name__ == "__main__":
    main()

Resources

⚠️ The package isn't threadsafe, for better performance in single-threaded applications and those using asyncio. So remember to use threading.Lock if you're writing a multithreaded program.

Keywords

dependencies

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