mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 07:57:43 +03:00 
			
		
		
		
	fixup! Add support for Fast Stream Depends
This commit is contained in:
		
							parent
							
								
									1cb1d66d7c
								
							
						
					
					
						commit
						2e76b2b4a8
					
				
							
								
								
									
										54
									
								
								docs/examples/fastdepends.rst
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								docs/examples/fastdepends.rst
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| .. _fastdepends-example: | ||||
| 
 | ||||
| FastDepends example | ||||
| =================== | ||||
| 
 | ||||
| .. meta:: | ||||
|    :keywords: Python,Dependency Injection,FastDepends,Example | ||||
|    :description: This example demonstrates a usage of the FastDepends and Dependency Injector. | ||||
| 
 | ||||
| 
 | ||||
| This example shows how to use ``Dependency Injector`` with `FastDepends <https://github.com/Lancetnik/FastDepends>`_. | ||||
| 
 | ||||
| The example application is a REST API that searches for funny GIFs on the `Giphy <https://giphy.com/>`_. | ||||
| 
 | ||||
| Example code is available on `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/tests/unit/wiringfastdepends>`_. | ||||
| 
 | ||||
| Quick sample | ||||
| ------------ | ||||
| 
 | ||||
| Just use it within ``Depends`` | ||||
| 
 | ||||
| .. code-block:: python | ||||
| 
 | ||||
|     import sys | ||||
| 
 | ||||
|     from dependency_injector import containers, providers | ||||
|     from dependency_injector.wiring import inject, Provide | ||||
|     from fast_depends import Depends | ||||
| 
 | ||||
| 
 | ||||
|     class CoefficientService: | ||||
|         @staticmethod | ||||
|         def get_coefficient() -> float: | ||||
|             return 1.2 | ||||
| 
 | ||||
| 
 | ||||
|     class Container(containers.DeclarativeContainer): | ||||
|         service = providers.Factory(CoefficientService) | ||||
| 
 | ||||
| 
 | ||||
|     @inject | ||||
|     def apply_coefficient( | ||||
|         a: int, | ||||
|         coefficient_provider: CoefficientService = Depends(Provide[Container.service]), | ||||
|     ) -> float: | ||||
|         return a * coefficient_provider.get_coefficient() | ||||
| 
 | ||||
| 
 | ||||
|     container = Container() | ||||
|     container.wire(modules=[sys.modules[__name__]]) | ||||
| 
 | ||||
|     assert apply_coefficient(100) == 120.0 | ||||
| 
 | ||||
| 
 | ||||
|  | @ -20,5 +20,6 @@ scipy | |||
| boto3 | ||||
| mypy_boto3_s3 | ||||
| typing_extensions | ||||
| fast-depends | ||||
| 
 | ||||
| -r requirements-ext.txt | ||||
|  |  | |||
							
								
								
									
										37
									
								
								tests/unit/samples/wiringfastdepends/sample.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								tests/unit/samples/wiringfastdepends/sample.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,37 @@ | |||
| from dependency_injector.wiring import inject, Provide | ||||
| from fast_depends import Depends | ||||
| 
 | ||||
| # Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir | ||||
| import os | ||||
| 
 | ||||
| _SAMPLES_DIR = os.path.abspath( | ||||
|     os.path.sep.join( | ||||
|         ( | ||||
|             os.path.dirname(__file__), | ||||
|             "../samples/", | ||||
|         ) | ||||
|     ), | ||||
| ) | ||||
| import sys | ||||
| 
 | ||||
| sys.path.append(_SAMPLES_DIR) | ||||
| 
 | ||||
| 
 | ||||
| from wiringfastdepends.sample import CoefficientService, Container | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| @inject | ||||
| def apply_coefficient( | ||||
|     a: int, | ||||
|     coefficient_provider: CoefficientService = Depends(Provide[Container.service]), | ||||
| ) -> float: | ||||
|     return a * coefficient_provider.get_coefficient() | ||||
| 
 | ||||
| 
 | ||||
| container = Container() | ||||
| container.wire(modules=[sys.modules[__name__]]) | ||||
| 
 | ||||
| 
 | ||||
| def test_wire_positive() -> None: | ||||
|     assert apply_coefficient(100) == 120.0 | ||||
							
								
								
									
										30
									
								
								tests/unit/wiring/test_fastdepends.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								tests/unit/wiring/test_fastdepends.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | |||
| 
 | ||||
| import sys | ||||
| 
 | ||||
| from dependency_injector import containers, providers | ||||
| from dependency_injector.wiring import inject, Provide | ||||
| from fast_depends import Depends | ||||
| 
 | ||||
| 
 | ||||
| class CoefficientService: | ||||
|     @staticmethod | ||||
|     def get_coefficient() -> float: | ||||
|         return 1.2 | ||||
| 
 | ||||
| 
 | ||||
| class Container(containers.DeclarativeContainer): | ||||
|     service = providers.Factory(CoefficientService) | ||||
| 
 | ||||
| 
 | ||||
| @inject | ||||
| def apply_coefficient( | ||||
|     a: int, | ||||
|     coefficient_provider: CoefficientService = Depends(Provide[Container.service]), | ||||
| ) -> float: | ||||
|     return a * coefficient_provider.get_coefficient() | ||||
| 
 | ||||
| 
 | ||||
| container = Container() | ||||
| container.wire(modules=[sys.modules[__name__]]) | ||||
| 
 | ||||
| assert apply_coefficient(100) == 120.0 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user