mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-31 03:36:41 +03:00
Adding static provider docs
This commit is contained in:
parent
d386969004
commit
91ea2a54f6
|
@ -1,77 +1,6 @@
|
|||
Providers
|
||||
=========
|
||||
|
||||
Static providers
|
||||
----------------
|
||||
|
||||
Static providers are family of providers that return their values "as is".
|
||||
There are four of static providers: ``Class``, ``Object``, ``Function`` and
|
||||
``Value``. All of them has the same behaviour, but usage of anyone is
|
||||
predicted by readability and providable object's type.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
"""Static providers example."""
|
||||
|
||||
from objects.providers import Class
|
||||
from objects.providers import Object
|
||||
from objects.providers import Function
|
||||
from objects.providers import Value
|
||||
|
||||
|
||||
cls_provider = Class(object)
|
||||
assert cls_provider() is object
|
||||
|
||||
object_provider = Object(object())
|
||||
assert isinstance(object_provider(), object)
|
||||
|
||||
function_provider = Function(len)
|
||||
assert function_provider() is len
|
||||
|
||||
value_provider = Value(123)
|
||||
assert value_provider() == 123
|
||||
|
||||
|
||||
Callable providers
|
||||
------------------
|
||||
|
||||
``Callable`` provider is a provider that decorates particular callable with
|
||||
some injections. Every call of this provider returns result of call of initial
|
||||
callable.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
"""`Callable` providers examples."""
|
||||
|
||||
from objects.providers import Callable
|
||||
from objects.providers import Singleton
|
||||
|
||||
from objects.injections import KwArg
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
def some_function(arg, database):
|
||||
"""Example function that has input arg and dependency on database."""
|
||||
return database.execute('SELECT @1', [arg]).fetchone()[0]
|
||||
|
||||
|
||||
# Database and `ObjectA` providers.
|
||||
database = Singleton(sqlite3.Connection,
|
||||
KwArg('database', ':memory:'))
|
||||
|
||||
some_function = Callable(some_function,
|
||||
KwArg('database', database))
|
||||
|
||||
# Some asserts.
|
||||
assert some_function(1) == 1
|
||||
assert some_function(2) == 2
|
||||
assert some_function(2231) == 2231
|
||||
|
||||
|
||||
External dependency providers
|
||||
-----------------------------
|
||||
|
|
|
@ -7,6 +7,7 @@ All providers are callable. They describe how particular objects are provided.
|
|||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:glob:
|
||||
|
||||
*
|
||||
factory
|
||||
singleton
|
||||
static
|
||||
|
|
42
docs/providers/static.rst
Normal file
42
docs/providers/static.rst
Normal file
|
@ -0,0 +1,42 @@
|
|||
Static providers
|
||||
----------------
|
||||
|
||||
Static providers are family of providers that return their values "as is".
|
||||
There are four types of static providers:
|
||||
|
||||
- ``Class``
|
||||
- ``Object``
|
||||
- ``Function``
|
||||
- ``Value``
|
||||
|
||||
All of them have the same behaviour, but usage of anyone is predicted by
|
||||
readability and providing object's type.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
"""`Static` providers example."""
|
||||
|
||||
from objects.providers import Class
|
||||
from objects.providers import Object
|
||||
from objects.providers import Function
|
||||
from objects.providers import Value
|
||||
|
||||
|
||||
# Provides class - `object`
|
||||
cls_provider = Class(object)
|
||||
assert cls_provider() is object
|
||||
|
||||
# Provides object - `object()`
|
||||
object_provider = Object(object())
|
||||
assert isinstance(object_provider(), object)
|
||||
|
||||
# Provides function - `len`
|
||||
function_provider = Function(len)
|
||||
assert function_provider() is len
|
||||
|
||||
# Provides value - `123`
|
||||
value_provider = Value(123)
|
||||
assert value_provider() == 123
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
"""Static providers example."""
|
||||
"""`Static` providers example."""
|
||||
|
||||
from objects.providers import Class
|
||||
from objects.providers import Object
|
||||
|
@ -6,14 +6,18 @@ from objects.providers import Function
|
|||
from objects.providers import Value
|
||||
|
||||
|
||||
# Provides class - `object`
|
||||
cls_provider = Class(object)
|
||||
assert cls_provider() is object
|
||||
|
||||
# Provides object - `object()`
|
||||
object_provider = Object(object())
|
||||
assert isinstance(object_provider(), object)
|
||||
|
||||
# Provides function - `len`
|
||||
function_provider = Function(len)
|
||||
assert function_provider() is len
|
||||
|
||||
# Provides value - `123`
|
||||
value_provider = Value(123)
|
||||
assert value_provider() == 123
|
Loading…
Reference in New Issue
Block a user