Socket
Socket
Sign inDemoInstall

simple-di

Package Overview
Dependencies
4
Maintainers
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-di

simple dependency injection library


Maintainers
1

Readme

simple-di

A simple, strictly typed dependency injection library.

  • Install
  • Usage
  • API

mypy-strict

Python 3.6 Python 3.7 Python 3.8 Python 3.9

Install

    pip install simple_di

Usage

Examples:

    from simple_di import inject, Provide, Provider, container
    from simple_di.providers import Static, Factory, Configuration


    @container
    class OptionsClass(container):
        cpu: Provider[int] = Static(2)
        worker: Provider[int] = Factory(lambda c: 2 * int(c) + 1, c=cpu)

    Options = OptionsClass()

    @inject
    def func(worker: int = Provide[Options.worker]):
        return worker

    assert func() == 5
    assert func(1) == 1

    Options.worker.set(2)
    assert func() == 2

    Options.worker.reset()
    assert func() == 5

    Options.cpu.set(1)
    assert func() == 3

API

Type annotation supported

inject

Inject values into providers in function/method arguments.

Arguments:

  • squeeze_none: default False. Treat None value passed in as not passed.

FAQs


Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc