Skip to main content

Dependency Injection Container Library

Project description

Injecta

Dependency Injection (DI) Container written in Python. Main component of the Pyfony Framework.

Installation

$ pip install injecta

Simple container initialization

(The following steps are covered in the ContainerInitializerTest)

To start using Injecta, create a simple config.yaml file to define your DI services:

parameters:
  api:
    endpoint: 'https://api.mycompany.com'

services:
    mycompany.api.ApiClient:
      arguments:
        - '@mycompany.api.Authenticator'

    mycompany.api.Authenticator:
      class: mycompany.authenticator.RestAuthenticator
      arguments:
        - '%api.endpoint%'
        - '%env(API_TOKEN)%'

Then, initialize the container:

from injecta.container.ContainerBuilder import ContainerBuilder
from injecta.config.YamlConfigReader import YamlConfigReader
from injecta.container.ContainerInitializer import ContainerInitializer

config = YamlConfigReader().read('/path/to/config.yaml')

containerBuild = ContainerBuilder().build(config)

container = ContainerInitializer().init(containerBuild)

Use container.get() to finally retrieve your service:

from mycompany.api.ApiClient import ApiClient

apiClient = container.get('mycompany.api.ApiClient') # type: ApiClient   
apiClient.get('/foo/bar')

Advanced examples

  1. Configuring services using parameters
  2. Service autowiring
  3. Using service factories
  4. Tagged services

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

injecta-0.10.2-py3-none-any.whl (59.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page