mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-17 12:03:13 +03:00
36 lines
882 B
Python
36 lines
882 B
Python
import sys
|
|
|
|
from dependency_injector import containers, providers
|
|
from dependency_injector.wiring import inject, Provide
|
|
from fast_depends import Depends
|
|
from typing_extensions import Annotated
|
|
|
|
|
|
class CoefficientService:
|
|
@staticmethod
|
|
def get_coefficient() -> float:
|
|
return 1.2
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
service = providers.Factory(CoefficientService)
|
|
|
|
|
|
@inject
|
|
def apply_coefficient(
|
|
a: int,
|
|
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
|
|
) -> float:
|
|
return a * coefficient_provider.get_coefficient()
|
|
|
|
|
|
@inject
|
|
def apply_coefficient_annotated(
|
|
a: int,
|
|
coefficient_provider: Annotated[CoefficientService, Depends(Provide[Container.service])],
|
|
) -> float:
|
|
return a * coefficient_provider.get_coefficient()
|
|
|
|
|
|
container = Container()
|
|
container.wire(modules=[sys.modules[__name__]]) |