New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple-injector

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-injector

Dependency Injection made simple.

  • 1.1.2
  • PyPI
  • Socket score

Maintainers
1

Dependency Injection made simple.

Instalation

pip instal simple-injector

Getting Started

from simple_injector import SimpleInjector, inject
from random import random

class Repository():
    def __init__(self):
        self.number = random()

    def get_a_number(self):
        return self.number

class Service():
    def __init__(self, repo: Repository):
        self.repo = repo

    def get_number(self):
        return {"n":self.repo.get_a_number()}

def bootstrap():
    SimpleInjector().register(Repository)
    SimpleInjector().singleton(Service)

bootstrap()

repo = inject(Repository)
serv = inject(Service)

Reference Guide

SimpleInjector()

This will always return the same instance of the SimpleInjector.

SimpleInjector().register(class, obj_or_callable=None)

Used to register a class on the dependencies list.

Can also receive an instance of the object that you want to assign to it.

Can also receive a callable with one parameter, that will be the instance of the Reference.

Can also receive a callable with 0 parameters.

Any lambda received as a parameter should always return the instance that will be injected.

SimpleInjector().singleton(class)

Used to register a class as a singleton on the dependencies list.

This class' dependencies will be resolved at the moment of this method call, so all its dependencies must be already registered with SimpleInjector.

SimpleInjector().lazy(class, obj)

Used to register a class with its instantiate object on the dependencies list.

Any call to SimpleInjector().resolve(class) to this class will be resolved with the object previously provided.

SimpleInjector().resolve(class) or inject

Used to resolve a dependency of a class that has been registered prior to the execution of this resolve method.

This method will return an object it has on the dependency list for this class, if it is a singleton, or a registered object, otherwise it will return a new instance for the class provided, injecting all needed dependencies, if they were registered prior to the execution of this method.

SimpleInjector().instantiate(class, extraParams={})

Can be used if you need to get a new instance of some class, or if you want a new instance and want to override some param on the constructor of the class.

SimpleInjector().reset()

Can be used if you need to be sure that the SimpleInjector has no dependencies registered on its list

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc