mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Add overriding demo to the README
This commit is contained in:
parent
684745a6b4
commit
925801c73f
19
README.rst
19
README.rst
|
@ -109,19 +109,9 @@ your application objects when you apply dependency injection principle:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
from .example_di import ApiClient, Service
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -145,9 +135,12 @@ your application objects when you apply dependency injection principle:
|
||||||
container.config.from_yaml('config.yml')
|
container.config.from_yaml('config.yml')
|
||||||
|
|
||||||
service = container.service()
|
service = container.service()
|
||||||
|
|
||||||
assert isinstance(service.api_client, ApiClient)
|
assert isinstance(service.api_client, ApiClient)
|
||||||
|
|
||||||
|
with container.api_client.override(mock.Mock()):
|
||||||
|
service = container.service()
|
||||||
|
assert isinstance(service.api_client, mock.Mock)
|
||||||
|
|
||||||
`More examples <https://github.com/ets-labs/python-dependency-injector/tree/master/examples>`_
|
`More examples <https://github.com/ets-labs/python-dependency-injector/tree/master/examples>`_
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
|
|
|
@ -1,18 +1,7 @@
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
from .example_di import ApiClient, Service
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -41,4 +30,3 @@ if __name__ == '__main__':
|
||||||
with container.api_client.override(mock.Mock()):
|
with container.api_client.override(mock.Mock()):
|
||||||
service = container.service()
|
service = container.service()
|
||||||
assert isinstance(service.api_client, mock.Mock)
|
assert isinstance(service.api_client, mock.Mock)
|
||||||
|
|
||||||
|
|
13
examples/di_demo2/example_di.py
Normal file
13
examples/di_demo2/example_di.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
|
||||||
|
|
||||||
|
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
|
14
examples/di_demo2/example_no_di.py
Normal file
14
examples/di_demo2/example_no_di.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class ApiClient:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.api_key = os.getenv('API_KEY')
|
||||||
|
self.timeout = os.getenv('TIMEOUT')
|
||||||
|
|
||||||
|
|
||||||
|
class Service:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.api_client = ApiClient()
|
Loading…
Reference in New Issue
Block a user