mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +03:00
Update front example
This commit is contained in:
parent
316799ae6d
commit
43a1a6cf39
10
README.rst
10
README.rst
|
@ -80,7 +80,7 @@ Key features of the ``Dependency Injector``:
|
|||
.. code-block:: python
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from dependency_injector.wiring import Provide, inject
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
@ -104,11 +104,11 @@ Key features of the ``Dependency Injector``:
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
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__]])
|
||||
container.config.api_key.from_env("API_KEY")
|
||||
container.config.timeout.from_env("TIMEOUT")
|
||||
container.wire(modules=[__name__])
|
||||
|
||||
main() # <-- dependency is injected automatically
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ Key features of the ``Dependency Injector``:
|
|||
.. code-block:: python
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from dependency_injector.wiring import Provide, inject
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
@ -110,11 +110,11 @@ Key features of the ``Dependency Injector``:
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
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__]])
|
||||
container.config.api_key.from_env("API_KEY")
|
||||
container.config.timeout.from_env("TIMEOUT")
|
||||
container.wire(modules=[__name__])
|
||||
|
||||
main() # <-- dependency is injected automatically
|
||||
|
||||
|
|
|
@ -67,8 +67,8 @@ Before:
|
|||
class ApiClient:
|
||||
|
||||
def __init__(self):
|
||||
self.api_key = os.getenv('API_KEY') # <-- dependency
|
||||
self.timeout = os.getenv('TIMEOUT') # <-- dependency
|
||||
self.api_key = os.getenv("API_KEY") # <-- dependency
|
||||
self.timeout = os.getenv("TIMEOUT") # <-- dependency
|
||||
|
||||
|
||||
class Service:
|
||||
|
@ -82,7 +82,7 @@ Before:
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
After:
|
||||
|
@ -109,12 +109,12 @@ After:
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main(
|
||||
service=Service(
|
||||
api_client=ApiClient(
|
||||
api_key=os.getenv('API_KEY'),
|
||||
timeout=os.getenv('TIMEOUT'),
|
||||
api_key=os.getenv("API_KEY"),
|
||||
timeout=os.getenv("TIMEOUT"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
@ -136,8 +136,8 @@ Now you need to assemble and inject the objects like this:
|
|||
main(
|
||||
service=Service(
|
||||
api_client=ApiClient(
|
||||
api_key=os.getenv('API_KEY'),
|
||||
timeout=os.getenv('TIMEOUT'),
|
||||
api_key=os.getenv("API_KEY"),
|
||||
timeout=os.getenv("TIMEOUT"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
@ -162,7 +162,7 @@ the dependency.
|
|||
.. code-block:: python
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from dependency_injector.wiring import Provide, inject
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
@ -186,11 +186,11 @@ the dependency.
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
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__]])
|
||||
container.config.api_key.from_env("API_KEY")
|
||||
container.config.timeout.from_env("TIMEOUT")
|
||||
container.wire(modules=[__name__])
|
||||
|
||||
main() # <-- dependency is injected automatically
|
||||
|
||||
|
|
|
@ -18,12 +18,12 @@ def main(service: Service): # <-- dependency is injected
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main(
|
||||
service=Service(
|
||||
api_client=ApiClient(
|
||||
api_key=os.getenv('API_KEY'),
|
||||
timeout=os.getenv('TIMEOUT'),
|
||||
api_key=os.getenv("API_KEY"),
|
||||
timeout=os.getenv("TIMEOUT"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
|
|
@ -4,8 +4,8 @@ import os
|
|||
class ApiClient:
|
||||
|
||||
def __init__(self):
|
||||
self.api_key = os.getenv('API_KEY') # <-- dependency
|
||||
self.timeout = os.getenv('TIMEOUT') # <-- dependency
|
||||
self.api_key = os.getenv("API_KEY") # <-- dependency
|
||||
self.timeout = os.getenv("TIMEOUT") # <-- dependency
|
||||
|
||||
|
||||
class Service:
|
||||
|
@ -19,5 +19,5 @@ def main() -> None:
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
import sys
|
||||
from unittest import mock
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from dependency_injector.wiring import Provide, inject
|
||||
|
||||
from after import ApiClient, Service
|
||||
|
||||
|
@ -28,11 +27,11 @@ def main(service: Service = Provide[Container.service]):
|
|||
...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
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__]])
|
||||
container.config.api_key.from_env("API_KEY")
|
||||
container.config.timeout.from_env("TIMEOUT")
|
||||
container.wire(modules=[__name__])
|
||||
|
||||
main() # <-- dependency is injected automatically
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user