Improve List and Configuration provider docs

This commit is contained in:
Roman Mogylatov 2020-06-29 16:32:48 -04:00
parent 2513d1f600
commit a0a3da8f72
3 changed files with 46 additions and 12 deletions

View File

@ -7,7 +7,8 @@ Configuration providers
.. literalinclude:: ../../examples/providers/configuration/configuration.py
:language: python
:emphasize-lines: 7,12-13,18-25
:emphasize-lines: 4,9-10
:lines: 4-14
:linenos:
It implements "use first, define later" principle.
@ -20,8 +21,8 @@ Loading from ``ini`` file
.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py
:language: python
:lines: 6-
:emphasize-lines: 3
:lines: 3-5,6-
:emphasize-lines: 6
:linenos:
where ``examples/providers/configuration/config.ini`` is:
@ -42,8 +43,8 @@ Loading from ``yaml`` file
.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py
:language: python
:lines: 6-
:emphasize-lines: 3
:lines: 3-5,6-
:emphasize-lines: 6
:linenos:
where ``examples/providers/configuration/config.yml`` is:
@ -62,6 +63,18 @@ variable ``ENV_NAME``.
`Dependency Injector` with extras ``pip install dependency-injector[yaml]`` or install
``PyYAML`` separately ``pip install pyyaml``.
Loading from ``dict``
~~~~~~~~~~~~~~~~~~~~~
:py:class:`Configuration` provider can load configuration from Python ``dict`` using
:py:meth:`Configuration.from_dict`:
.. literalinclude:: ../../examples/providers/configuration/configuration_dict.py
:language: python
:lines: 3-5,6-
:emphasize-lines: 6-13
:linenos:
Loading from environment variable
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -70,11 +83,10 @@ Loading from environment variable
.. literalinclude:: ../../examples/providers/configuration/configuration_env.py
:language: python
:lines: 13-21
:emphasize-lines: 3-5
:lines: 5-7,13-21
:emphasize-lines: 6-8
:linenos:
Loading from multiple sources
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -83,8 +95,8 @@ configuration is merged recursively over existing configuration.
.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py
:language: python
:lines: 6-14
:emphasize-lines: 3-4
:lines: 3-5,6-14
:emphasize-lines: 6-7
:linenos:
where ``examples/providers/configuration/config.local.yml`` is:

View File

@ -7,7 +7,8 @@ List providers
.. literalinclude:: ../../examples/providers/list.py
:language: python
:lines: 23-29
:emphasize-lines: 6-9
:lines: 6-8, 23-29
:linenos:
:py:class:`List` provider is needed for injecting a list of dependencies. It handles
@ -24,7 +25,8 @@ Full example:
.. literalinclude:: ../../examples/providers/list.py
:language: python
:emphasize-lines: 23-29
:emphasize-lines: 23-26
:lines: 3-
:linenos:
.. note::

View File

@ -0,0 +1,20 @@
"""`Configuration` provider values loading example."""
from dependency_injector import providers
config = providers.Configuration()
config.from_dict(
{
'aws': {
'access_key_id': 'KEY',
'secret_access_key': 'SECRET',
},
},
)
assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
assert config.aws.access_key_id() == 'KEY'
assert config.aws.secret_access_key() == 'SECRET'