Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dependency injection python

"""Example of dependency injection in Python."""

import logging
import sqlite3

import boto.s3.connection

import example.main
import example.services

import dependency_injector.containers as containers
import dependency_injector.providers as providers


class Platform(containers.DeclarativeContainer):
    """IoC container of platform service providers."""

    logger = providers.Singleton(logging.Logger, name='example')

    database = providers.Singleton(sqlite3.connect, ':memory:')

    s3 = providers.Singleton(boto.s3.connection.S3Connection,
                             aws_access_key_id='KEY',
                             aws_secret_access_key='SECRET')


class Services(containers.DeclarativeContainer):
    """IoC container of business service providers."""

    users = providers.Factory(example.services.UsersService,
                              logger=Platform.logger,
                              db=Platform.database)

    auth = providers.Factory(example.services.AuthService,
                             logger=Platform.logger,
                             db=Platform.database,
                             token_ttl=3600)

    photos = providers.Factory(example.services.PhotosService,
                               logger=Platform.logger,
                               db=Platform.database,
                               s3=Platform.s3)


class Application(containers.DeclarativeContainer):
    """IoC container of application component providers."""

    main = providers.Callable(example.main.main,
                              users_service=Services.users,
                              auth_service=Services.auth,
                              photos_service=Services.photos)
Comment

python Dependency injection

from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject


class Container(containers.DeclarativeContainer):

    config = providers.Configuration()

    api_client = providers.Singleton(
        ApiClient,
        api_key=config.api_key,
        timeout=config.timeout,
    )

    service = providers.Factory(
        Service,
        api_client=api_client,
    )


@inject
def main(service: Service = Provide[Container.service]):
    ...


if __name__ == "__main__":
    container = Container()
    container.config.api_key.from_env("API_KEY", required=True)
    container.config.timeout.from_env("TIMEOUT", as_=int, default=5)
    container.wire(modules=[__name__])

    main()  # <-- dependency is injected automatically

    with container.api_client.override(mock.Mock()):
        main()  # <-- overridden dependency is injected automatically
Comment

python dependency injection

from some_package import A

class B:
	def __init__(self, a=A()):
    	self._a = a
    
    ...
Comment

PREVIOUS NEXT
Code Example
Python :: python to dart converter 
Python :: QDateEdit.date().toString("MMMM dd, yyyy") does not display months in English 
Python :: parameter name in string 
Python :: How to set a tkinter window to a constant size 
Python :: How to Export Sql Server Result to Excel in Python 
Python :: gremlin python import 
Python :: python random number between 1000 and 9999 
Python :: how to check all possible combinations algorithm python 
Python :: browser environment: 
Python :: selsearch 
Python :: problème barbier semaphore python 
Python :: python list len 
Python :: Flask select which form to POST by button click 
Python :: if space bar pressed pygame 
Python :: Python (cpython 2.7.16) sample 
Python :: for y in range(10): for x in range(y): print("*",end=') print() 
Python :: clock replacement algorithm python 
Python :: ring write the same example using normal for loop the Encrypt() and Decrypt() functions. 
Python :: z3 symbolic expressions cannot be cast to concrete boolean values 
Python :: notebook prevent cell output 
Python :: python alphabet to number 
Python :: how to ge squrre root symobl as string inpython 
Python :: django date grater 
Python :: create new column pandas and order sequence 
Python :: x not defined python 
Python :: jupter notebook save session variables 
Python :: python assign variable to another variable 
Python :: python run async function without await 
Python :: site:www.python-kurs.eu generators 
Python :: python autotrader web 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =