mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-16 19:40:59 +03:00
Update docs of Callable provider with description of positional agrument injections and new example
This commit is contained in:
parent
59b98959bc
commit
dd5cc79dd1
|
@ -16,7 +16,7 @@ Development version
|
||||||
- Add images for catalog "Writing catalogs" and "Operating with catalogs"
|
- Add images for catalog "Writing catalogs" and "Operating with catalogs"
|
||||||
examples.
|
examples.
|
||||||
- Add functionality for using positional argument injections with
|
- Add functionality for using positional argument injections with
|
||||||
``di.Factory`` and ``di.Singleton`` providers.
|
``di.Factory``, ``di.Singleton`` and ``di.Callable`` providers.
|
||||||
- Add optimization for ``di.Injection.value`` property that will compute
|
- Add optimization for ``di.Injection.value`` property that will compute
|
||||||
type of injection once, instead of doing this on every call.
|
type of injection once, instead of doing this on every call.
|
||||||
- Add functionality for decorating classes with ``@di.inject``.
|
- Add functionality for decorating classes with ``@di.inject``.
|
||||||
|
|
|
@ -8,20 +8,40 @@ callable.
|
||||||
Callable providers and injections
|
Callable providers and injections
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
``di.Callable`` provider uses keyword argument injections. Keyword argument
|
``di.Callable`` takes a various number of positional and keyword arguments
|
||||||
injections are done by passing injectable values as keyword arguments during
|
that are used as decorated callable injections. Every time, when
|
||||||
call time.
|
``di.Callable`` is called, positional and keyword argument injections would be
|
||||||
|
passed as an callable arguments.
|
||||||
|
|
||||||
Context keyword arguments have higher priority than keyword argument
|
Such behaviour is very similar to the standard Python ``functools.partial``
|
||||||
injections.
|
object, except of one thing: all injectable values are provided
|
||||||
|
*"as is"*, except of providers (subclasses of ``di.Provider``). Providers
|
||||||
|
will be called every time, when injection needs to be done. For example,
|
||||||
|
if injectable value of injection is a ``di.Factory``, it will provide new one
|
||||||
|
instance (as a result of its call) every time, when injection needs to be done.
|
||||||
|
|
||||||
Example:
|
``di.Callable`` behaviour with context positional and keyword arguments is
|
||||||
|
very like a standard Python ``functools.partial``:
|
||||||
|
|
||||||
|
- Positional context arguments will be appended after ``di.Callable``
|
||||||
|
positional injections.
|
||||||
|
- Keyword context arguments have priority on ``di.Callable`` keyword
|
||||||
|
injections and will be merged over them.
|
||||||
|
|
||||||
|
Example that shows usage of ``di.Callable`` with positional argument
|
||||||
|
injections:
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/providers/callable_args.py
|
||||||
|
:language: python
|
||||||
|
|
||||||
|
Next one example shows usage of ``di.Callable`` with keyword argument
|
||||||
|
injections:
|
||||||
|
|
||||||
.. image:: /images/providers/callable.png
|
.. image:: /images/providers/callable.png
|
||||||
:width: 100%
|
:width: 100%
|
||||||
:align: center
|
:align: center
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/callable_injections.py
|
.. literalinclude:: ../../examples/providers/callable_kwargs.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
||||||
Callable providers delegation
|
Callable providers delegation
|
||||||
|
|
16
examples/providers/callable_args.py
Normal file
16
examples/providers/callable_args.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
"""`di.Callable` providers with positional arguments example."""
|
||||||
|
|
||||||
|
import dependency_injector as di
|
||||||
|
|
||||||
|
|
||||||
|
# Creating even and odd filter providers:
|
||||||
|
even_filter = di.Callable(filter, lambda x: x % 2 == 0)
|
||||||
|
odd_filter = di.Callable(filter, lambda x: x % 2 != 0)
|
||||||
|
|
||||||
|
# Creating even and odd ranges using xrange() and filter providers:
|
||||||
|
even_range = even_filter(xrange(1, 10))
|
||||||
|
odd_range = odd_filter(xrange(1, 10))
|
||||||
|
|
||||||
|
# Making some asserts:
|
||||||
|
assert even_range == [2, 4, 6, 8]
|
||||||
|
assert odd_range == [1, 3, 5, 7, 9]
|
|
@ -1,4 +1,4 @@
|
||||||
"""`di.Callable` providers example."""
|
"""`di.Callable` providers with keyword arguments example."""
|
||||||
|
|
||||||
import passlib.hash
|
import passlib.hash
|
||||||
import dependency_injector as di
|
import dependency_injector as di
|
||||||
|
@ -10,6 +10,6 @@ password_hasher = di.Callable(passlib.hash.sha256_crypt.encrypt,
|
||||||
rounds=10000)
|
rounds=10000)
|
||||||
password_verifier = di.Callable(passlib.hash.sha256_crypt.verify)
|
password_verifier = di.Callable(passlib.hash.sha256_crypt.verify)
|
||||||
|
|
||||||
# Making some asserts (client's code):
|
# Making some asserts:
|
||||||
hashed_password = password_hasher('super secret')
|
hashed_password = password_hasher('super secret')
|
||||||
assert password_verifier('super secret', hashed_password)
|
assert password_verifier('super secret', hashed_password)
|
Loading…
Reference in New Issue
Block a user