mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-07 07:00:49 +03:00
Update Callable provider docs
This commit is contained in:
parent
9326fdace4
commit
444af2ab0e
Binary file not shown.
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
@ -1,17 +1,19 @@
|
||||||
Callable providers
|
Callable providers
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
``Callable`` provider is a provider that wraps particular callable with
|
``di.Callable`` provider is a provider that wraps particular callable with
|
||||||
some injections. Every call of this provider returns result of call of initial
|
some injections. Every call of this provider returns result of call of initial
|
||||||
callable.
|
callable.
|
||||||
|
|
||||||
Callable providers and injections
|
Callable providers and injections
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
``Callable`` provider uses ``KwArg`` injections. ``KwArg`` injections are
|
``di.Callable`` provider uses keyword argument injections. Keyword argument
|
||||||
done by passing injectable values as keyword arguments during call time.
|
injections are done by passing injectable values as keyword arguments during
|
||||||
|
call time.
|
||||||
|
|
||||||
Context keyword arguments have higher priority than ``KwArg`` injections.
|
Context keyword arguments have higher priority than keyword argument
|
||||||
|
injections.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
@ -25,13 +27,13 @@ Example:
|
||||||
Callable providers delegation
|
Callable providers delegation
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
``Callable`` provider could be delegated to any other provider via any kind of
|
``di.Callable`` provider could be delegated to any other provider via any kind
|
||||||
injection. Delegation of ``Callable`` providers is the same as ``Factory`` and
|
of injection. Delegation of ``di.Callable`` providers is the same as
|
||||||
``Singleton`` providers delegation, please follow *Factory providers
|
``di.Factory`` and ``di.Singleton`` providers delegation, please follow
|
||||||
delegation* section for example.
|
*Factory providers delegation* section for example.
|
||||||
|
|
||||||
``Callable`` delegate could be created obviously using
|
``di.Callable`` delegate could be created obviously using
|
||||||
``Delegate(Callable())`` or by calling ``Callable.delegate()`` method.
|
``di.Delegate(di.Callable())`` or by calling ``di.Callable.delegate()`` method.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
"""`Callable` providers delegation example."""
|
"""`di.Callable` providers delegation example."""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import dependency_injector as di
|
||||||
from dependency_injector.providers import Callable
|
|
||||||
from dependency_injector.providers import Delegate
|
|
||||||
|
|
||||||
|
|
||||||
# Creating some callable provider and few delegates of it:
|
# Creating some callable provider and few delegates of it:
|
||||||
callable_provider = Callable(sys.exit)
|
callable_provider = di.Callable(sys.exit)
|
||||||
callable_provider_delegate1 = callable_provider.delegate()
|
callable_provider_delegate1 = callable_provider.delegate()
|
||||||
callable_provider_delegate2 = Delegate(callable_provider)
|
callable_provider_delegate2 = di.Delegate(callable_provider)
|
||||||
|
|
||||||
# Making some asserts:
|
# Making some asserts:
|
||||||
assert callable_provider_delegate1() is callable_provider
|
assert callable_provider_delegate1() is callable_provider
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
"""`Callable` providers example."""
|
"""`di.Callable` providers example."""
|
||||||
|
|
||||||
from passlib.hash import sha256_crypt
|
|
||||||
|
|
||||||
from dependency_injector.providers import Callable
|
|
||||||
from dependency_injector.injections import KwArg
|
|
||||||
|
|
||||||
|
import passlib.hash
|
||||||
|
import dependency_injector as di
|
||||||
|
|
||||||
# Password hasher and verifier providers (hash function could be changed
|
# Password hasher and verifier providers (hash function could be changed
|
||||||
# anytime (for example, to sha512) without any changes in client's code):
|
# anytime (for example, to sha512) without any changes in client's code):
|
||||||
password_hasher = Callable(sha256_crypt.encrypt,
|
password_hasher = di.Callable(passlib.hash.sha256_crypt.encrypt,
|
||||||
KwArg('salt_size', 16),
|
salt_size=16,
|
||||||
KwArg('rounds', 10000))
|
rounds=10000)
|
||||||
password_verifier = Callable(sha256_crypt.verify)
|
password_verifier = di.Callable(passlib.hash.sha256_crypt.verify)
|
||||||
|
|
||||||
# Making some asserts (client's code):
|
# Making some asserts (client's code):
|
||||||
hashed_password = password_hasher('super secret')
|
hashed_password = password_hasher('super secret')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user