mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 01:47:36 +03:00 
			
		
		
		
	Update provider docs
This commit is contained in:
		
							parent
							
								
									93fc720a85
								
							
						
					
					
						commit
						dd0fa17ad4
					
				| 
						 | 
				
			
			@ -30,7 +30,6 @@ injections:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/callable_args.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Next one example shows usage of :py:class:`Callable` with keyword argument 
 | 
			
		||||
injections:
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +40,6 @@ injections:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/callable_kwargs.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. _callable_providers_delegation:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,6 @@ Configuration providers
 | 
			
		|||
   :language: python
 | 
			
		||||
   :emphasize-lines: 4,9-10
 | 
			
		||||
   :lines: 4-14
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
It implements "use first, define later" principle.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -23,13 +22,11 @@ Loading from ``ini`` file
 | 
			
		|||
   :language: python
 | 
			
		||||
   :lines: 3-5,6-
 | 
			
		||||
   :emphasize-lines: 6
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
where ``examples/providers/configuration/config.ini`` is:
 | 
			
		||||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/configuration/config.ini
 | 
			
		||||
   :language: ini
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
:py:meth:`Configuration.from_ini` supports environment variables interpolation. Use
 | 
			
		||||
``${ENV_NAME}`` format in the configuration file to substitute value of environment
 | 
			
		||||
| 
						 | 
				
			
			@ -45,13 +42,11 @@ Loading from ``yaml`` file
 | 
			
		|||
   :language: python
 | 
			
		||||
   :lines: 3-5,6-
 | 
			
		||||
   :emphasize-lines: 6
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
where ``examples/providers/configuration/config.yml`` is:
 | 
			
		||||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/configuration/config.yml
 | 
			
		||||
   :language: ini
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
:py:meth:`Configuration.from_yaml` supports environment variables interpolation. Use
 | 
			
		||||
``${ENV_NAME}`` format in the configuration file to substitute value of environment
 | 
			
		||||
| 
						 | 
				
			
			@ -73,7 +68,6 @@ Loading from ``dict``
 | 
			
		|||
   :language: python
 | 
			
		||||
   :lines: 3-5,6-
 | 
			
		||||
   :emphasize-lines: 6-13
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Loading from environment variable
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			@ -85,7 +79,6 @@ Loading from environment variable
 | 
			
		|||
   :language: python
 | 
			
		||||
   :lines: 5-7,13-21
 | 
			
		||||
   :emphasize-lines: 6-8
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Loading from multiple sources
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			@ -97,12 +90,10 @@ configuration is merged recursively over existing configuration.
 | 
			
		|||
   :language: python
 | 
			
		||||
   :lines: 3-5,6-14
 | 
			
		||||
   :emphasize-lines: 6-7
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
where ``examples/providers/configuration/config.local.yml`` is:
 | 
			
		||||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/configuration/config.local.yml
 | 
			
		||||
   :language: ini
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,7 +18,6 @@ coroutine:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/coroutine_async_await.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Coroutine providers and injections
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,7 +30,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/custom_factory.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,6 +39,5 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/dependency.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,7 +14,6 @@ Nothing could be better than brief example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Factory providers and __init__ injections
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +47,6 @@ injectable values are also provided by another factories:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory_init_injections.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. _factory_providers_delegation:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -92,7 +90,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory_delegation.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. _factory_providers_specialization:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -107,7 +104,6 @@ provided type:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory_provided_type.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. _abstract_factory_providers:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -142,13 +138,11 @@ Listing of ``cache.py``:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/abstract_factory/cache.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Listing of ``example.py``:
 | 
			
		||||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/abstract_factory/example.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Factory aggregate providers
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			@ -167,7 +161,6 @@ Next prototype might be the best demonstration of
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory_aggregate/prototype.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Example below shows one of the :py:class:`FactoryAggregate` use cases, when 
 | 
			
		||||
concrete implementation (game) must be selected based on dynamic input (CLI). 
 | 
			
		||||
| 
						 | 
				
			
			@ -176,12 +169,10 @@ Listing of ``games.py``:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory_aggregate/games.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Listing of ``example.py``:
 | 
			
		||||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/factory_aggregate/example.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,6 @@ List providers
 | 
			
		|||
   :language: python
 | 
			
		||||
   :emphasize-lines: 6-9
 | 
			
		||||
   :lines: 6-8, 23-29
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
:py:class:`List` provider is needed for injecting a list of dependencies. It handles
 | 
			
		||||
positional argument injections the same way as :py:class:`Factory` provider:
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +26,6 @@ Full example:
 | 
			
		|||
   :language: python
 | 
			
		||||
   :emphasize-lines: 23-26
 | 
			
		||||
   :lines: 3-
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. note::
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/object.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -33,7 +33,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/overriding_simple.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +42,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/overriding_users_model.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,6 @@ Selector providers
 | 
			
		|||
   :language: python
 | 
			
		||||
   :emphasize-lines: 6-10
 | 
			
		||||
   :lines: 3-5,14-20
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
:py:class:`Selector` provider has a callable called ``selector`` and a dictionary of providers.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -26,6 +25,5 @@ Full example:
 | 
			
		|||
   :language: python
 | 
			
		||||
   :emphasize-lines: 14-18
 | 
			
		||||
   :lines: 3-
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,7 +14,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/singleton.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Singleton providers resetting
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			@ -28,7 +27,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/singleton_resetting.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
Singleton providers and injections
 | 
			
		||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
| 
						 | 
				
			
			@ -108,7 +106,6 @@ Example:
 | 
			
		|||
 | 
			
		||||
.. literalinclude:: ../../examples/providers/singleton_thread_locals.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :linenos:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,19 +10,27 @@ cache_client_factory = providers.AbstractFactory(cache.AbstractCacheClient)
 | 
			
		|||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    # Override abstract factory with redis client factory:
 | 
			
		||||
    cache_client_factory.override(providers.Factory(cache.RedisCacheClient,
 | 
			
		||||
                                                    host='localhost',
 | 
			
		||||
                                                    port=6379,
 | 
			
		||||
                                                    db=0))
 | 
			
		||||
    cache_client_factory.override(
 | 
			
		||||
        providers.Factory(
 | 
			
		||||
            cache.RedisCacheClient,
 | 
			
		||||
            host='localhost',
 | 
			
		||||
            port=6379,
 | 
			
		||||
            db=0,
 | 
			
		||||
        ),
 | 
			
		||||
    )
 | 
			
		||||
    redis_cache = cache_client_factory()
 | 
			
		||||
    print(redis_cache)  # <cache.RedisCacheClient object at 0x10975bc50>
 | 
			
		||||
    print(redis_cache)
 | 
			
		||||
    # <cache.RedisCacheClient object at 0x10975bc50>
 | 
			
		||||
 | 
			
		||||
    # Override abstract factory with memcache client factory:
 | 
			
		||||
    cache_client_factory.override(providers.Factory(cache.MemcacheCacheClient,
 | 
			
		||||
                                                    hosts=['10.0.1.1',
 | 
			
		||||
                                                           '10.0.1.2',
 | 
			
		||||
                                                           '10.0.1.3'],
 | 
			
		||||
                                                    port=11211,
 | 
			
		||||
                                                    prefix='my_app'))
 | 
			
		||||
    cache_client_factory.override(
 | 
			
		||||
        providers.Factory(
 | 
			
		||||
            cache.MemcacheCacheClient,
 | 
			
		||||
            hosts=['10.0.1.1', '10.0.1.2', '10.0.1.3'],
 | 
			
		||||
            port=11211,
 | 
			
		||||
            prefix='my_app',
 | 
			
		||||
        ),
 | 
			
		||||
    )
 | 
			
		||||
    memcache_cache = cache_client_factory()
 | 
			
		||||
    print(memcache_cache)  # <cache.MemcacheCacheClient object at 0x10975bc90>
 | 
			
		||||
    print(memcache_cache)
 | 
			
		||||
    # <cache.MemcacheCacheClient object at 0x10975bc90>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,9 +7,11 @@ import dependency_injector.providers as providers
 | 
			
		|||
from games import Chess, Checkers, Ludo
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
game_factory = providers.FactoryAggregate(chess=providers.Factory(Chess),
 | 
			
		||||
                                          checkers=providers.Factory(Checkers),
 | 
			
		||||
                                          ludo=providers.Factory(Ludo))
 | 
			
		||||
game_factory = providers.FactoryAggregate(
 | 
			
		||||
    chess=providers.Factory(Chess),
 | 
			
		||||
    checkers=providers.Factory(Checkers),
 | 
			
		||||
    ludo=providers.Factory(Ludo),
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    game_type = sys.argv[1].lower()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,27 +26,36 @@ class User:
 | 
			
		|||
 | 
			
		||||
# Defining User and Photo factories using DelegatedFactory provider:
 | 
			
		||||
photos_factory = providers.DelegatedFactory(Photo)
 | 
			
		||||
users_factory = providers.DelegatedFactory(User,
 | 
			
		||||
                                           photos_factory=photos_factory)
 | 
			
		||||
users_factory = providers.DelegatedFactory(
 | 
			
		||||
    User,
 | 
			
		||||
    photos_factory=photos_factory,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# or using Delegate(Factory(...))
 | 
			
		||||
 | 
			
		||||
photos_factory = providers.Factory(Photo)
 | 
			
		||||
users_factory = providers.Factory(User,
 | 
			
		||||
                                  photos_factory=providers.Delegate(
 | 
			
		||||
                                      photos_factory))
 | 
			
		||||
users_factory = providers.Factory(
 | 
			
		||||
    User,
 | 
			
		||||
    photos_factory=providers.Delegate(photos_factory),
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# or using Factory(...).delegate()
 | 
			
		||||
 | 
			
		||||
photos_factory = providers.Factory(Photo)
 | 
			
		||||
users_factory = providers.Factory(User,
 | 
			
		||||
                                  photos_factory=photos_factory.delegate())
 | 
			
		||||
users_factory = providers.Factory(
 | 
			
		||||
    User,
 | 
			
		||||
    photos_factory=photos_factory.delegate(),
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Creating several User objects:
 | 
			
		||||
user1 = users_factory()  # Same as: user1 = User(photos_factory=photos_factory)
 | 
			
		||||
user2 = users_factory()  # Same as: user2 = User(photos_factory=photos_factory)
 | 
			
		||||
user1 = users_factory()
 | 
			
		||||
user2 = users_factory()
 | 
			
		||||
 | 
			
		||||
# Same as:
 | 
			
		||||
# user1 = User(photos_factory=photos_factory)
 | 
			
		||||
# user2 = User(photos_factory=photos_factory)
 | 
			
		||||
 | 
			
		||||
# Making some asserts:
 | 
			
		||||
assert isinstance(user1.main_photo, Photo)
 | 
			
		||||
| 
						 | 
				
			
			@ -55,13 +64,18 @@ assert isinstance(user2.main_photo, Photo)
 | 
			
		|||
# or using Factory(...).provider
 | 
			
		||||
 | 
			
		||||
photos_factory = providers.Factory(Photo)
 | 
			
		||||
users_factory = providers.Factory(User,
 | 
			
		||||
                                  photos_factory=photos_factory.provider)
 | 
			
		||||
 | 
			
		||||
users_factory = providers.Factory(
 | 
			
		||||
    User,
 | 
			
		||||
    photos_factory=photos_factory.provider,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Creating several User objects:
 | 
			
		||||
user1 = users_factory()  # Same as: user1 = User(photos_factory=photos_factory)
 | 
			
		||||
user2 = users_factory()  # Same as: user2 = User(photos_factory=photos_factory)
 | 
			
		||||
user1 = users_factory()
 | 
			
		||||
user2 = users_factory()
 | 
			
		||||
 | 
			
		||||
# Same as:
 | 
			
		||||
# user1 = User(photos_factory=photos_factory)
 | 
			
		||||
# user2 = User(photos_factory=photos_factory)
 | 
			
		||||
 | 
			
		||||
# Making some asserts:
 | 
			
		||||
assert isinstance(user1.main_photo, Photo)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,8 +7,8 @@ import dependency_injector.providers as providers
 | 
			
		|||
 | 
			
		||||
UsersService = collections.namedtuple('UsersService', [])
 | 
			
		||||
 | 
			
		||||
# Singleton provider creates new instance of specified class on first call and
 | 
			
		||||
# returns same instance on every next call.
 | 
			
		||||
# Singleton provider creates new instance of specified class on first call
 | 
			
		||||
# and returns same instance on every next call.
 | 
			
		||||
users_service_provider = providers.Singleton(UsersService)
 | 
			
		||||
 | 
			
		||||
# Retrieving several UserService objects:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,19 +18,22 @@ thread_local_object = providers.ThreadLocalSingleton(object)
 | 
			
		|||
queue_factory = providers.ThreadSafeSingleton(queue.Queue)
 | 
			
		||||
 | 
			
		||||
# Create callable provider for example(), inject dependencies:
 | 
			
		||||
example = providers.DelegatedCallable(example,
 | 
			
		||||
                                      example_object=thread_local_object,
 | 
			
		||||
                                      queue_object=queue_factory)
 | 
			
		||||
example = providers.DelegatedCallable(
 | 
			
		||||
    example,
 | 
			
		||||
    example_object=thread_local_object,
 | 
			
		||||
    queue_object=queue_factory,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Create factory provider for threads that are targeted to execute example():
 | 
			
		||||
thread_factory = providers.Factory(threading.Thread,
 | 
			
		||||
                                   target=example)
 | 
			
		||||
# Create factory for threads that are targeted to execute example():
 | 
			
		||||
thread_factory = providers.Factory(threading.Thread, target=example)
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    # Create 10 threads for concurrent execution of example():
 | 
			
		||||
    threads = []
 | 
			
		||||
    for thread_number in range(10):
 | 
			
		||||
        threads.append(thread_factory(name='Thread{0}'.format(thread_number)))
 | 
			
		||||
        threads.append(
 | 
			
		||||
            thread_factory(name='Thread{0}'.format(thread_number)),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    # Start execution of all created threads:
 | 
			
		||||
    for thread in threads:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user