Updaet demo example

This commit is contained in:
Roman Mogylatov 2020-09-21 16:46:02 -04:00
parent edd8979bf6
commit dd8778bf20
7 changed files with 90 additions and 60 deletions

View File

@ -74,31 +74,39 @@ Key features of the ``Dependency Injector``:
.. code-block:: python
from dependency_injector import containers, providers
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide
class Container(containers.DeclarativeContainer):
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
config = providers.Configuration()
api_client = providers.Singleton(
ApiClient,
api_key=config.api_key,
timeout=config.timeout.as_int(),
)
api_client = providers.Singleton(
ApiClient,
api_key=config.api_key,
timeout=config.timeout.as_int(),
)
service = providers.Factory(
Service,
api_client=api_client,
)
service = providers.Factory(
Service,
api_client=api_client,
)
if __name__ == '__main__':
container = Container()
container.config.api_key.from_env('API_KEY')
container.config.timeout.from_env('TIMEOUT')
def main(service: Service = Provide[Container.service]):
...
service = container.service()
if __name__ == '__main__':
container = Container()
container.config.api_key.from_env('API_KEY')
container.config.timeout.from_env('TIMEOUT')
container.wire(modules=[sys.modules[__name__]])
main()
With the ``Dependency Injector`` you keep **application structure in one place**.
This place is called **the container**. You use the container to manage all the components of the

View File

@ -83,31 +83,39 @@ Key features of the ``Dependency Injector``:
.. code-block:: python
from dependency_injector import containers, providers
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide
class Container(containers.DeclarativeContainer):
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
config = providers.Configuration()
api_client = providers.Singleton(
ApiClient,
api_key=config.api_key,
timeout=config.timeout.as_int(),
)
api_client = providers.Singleton(
ApiClient,
api_key=config.api_key,
timeout=config.timeout.as_int(),
)
service = providers.Factory(
Service,
api_client=api_client,
)
service = providers.Factory(
Service,
api_client=api_client,
)
if __name__ == '__main__':
container = Container()
container.config.api_key.from_env('API_KEY')
container.config.timeout.from_env('TIMEOUT')
def main(service: Service = Provide[Container.service]):
...
service = container.service()
if __name__ == '__main__':
container = Container()
container.config.api_key.from_env('API_KEY')
container.config.timeout.from_env('TIMEOUT')
container.wire(modules=[sys.modules[__name__]])
main()
With the ``Dependency Injector`` you keep **application structure in one place**.
This place is called **the container**. You use the container to manage all the components of the

View File

@ -1,17 +1,9 @@
import sys
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide
class ApiClient:
def __init__(self, api_key: str, timeout: int):
self.api_key = api_key
self.timeout = timeout
class Service:
def __init__(self, api_client: ApiClient):
self.api_client = api_client
from di import ApiClient, Service
class Container(containers.DeclarativeContainer):
@ -30,9 +22,16 @@ class Container(containers.DeclarativeContainer):
)
def main(service: Service = Provide[Container.service]):
...
if __name__ == '__main__':
container = Container()
container.config.api_key.from_env('API_KEY')
container.config.timeout.from_env('TIMEOUT')
service = container.service()
container.wire(modules=[sys.modules[__name__]])
main()

View File

@ -14,5 +14,15 @@ class Service:
self.api_client = api_client
def main() -> None:
service = Service(
api_client=ApiClient(
api_key=os.getenv('API_KEY'),
timeout=os.getenv('TIMEOUT'),
),
)
...
if __name__ == '__main__':
service = Service(ApiClient(os.getenv('API_KEY'), os.getenv('TIMEOUT')))
main()

View File

@ -14,5 +14,10 @@ class Service:
self.api_client = ApiClient()
if __name__ == '__main__':
def main() -> None:
service = Service()
...
if __name__ == '__main__':
main()

11
examples/demo/test.py Normal file
View File

@ -0,0 +1,11 @@
from unittest import mock
import demo
if __name__ == '__main__':
container = demo.Container()
container.wire(modules=[demo])
with container.api_client.override(mock.Mock()):
demo.main()

View File

@ -1,11 +0,0 @@
from unittest import mock
from demo import Container
if __name__ == '__main__':
container = Container()
with container.api_client.override(mock.Mock()):
service = container.service()
assert isinstance(service.api_client, mock.Mock)