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