mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-30 19:24:31 +03:00
Add example of callable delegation
This commit is contained in:
parent
96b44004b1
commit
a8dd335d71
|
@ -20,3 +20,4 @@ and powered by *Dependency Injector* framework.
|
|||
services_miniapp_v2
|
||||
bundles_miniapp
|
||||
use_cases_miniapp
|
||||
password_hashing_miniapp
|
||||
|
|
19
docs/examples/password_hashing_miniapp.rst
Normal file
19
docs/examples/password_hashing_miniapp.rst
Normal file
|
@ -0,0 +1,19 @@
|
|||
Dependency injection and password hashing in Python
|
||||
===================================================
|
||||
|
||||
Small example that demonstrates using of dependency injection for user
|
||||
password hashing.
|
||||
|
||||
Instructions for running:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python example.py
|
||||
|
||||
Listing of ``example.py``:
|
||||
|
||||
.. literalinclude:: ../../examples/miniapps/password_hashing/example.py
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
.. disqus::
|
|
@ -9,9 +9,11 @@ follows `Semantic versioning`_
|
|||
|
||||
Development version
|
||||
-------------------
|
||||
- Update main page example from "services" to "ioc_container".
|
||||
- Update main page example from "services_v1" to "services_v2".
|
||||
- Fix few typos on main page.
|
||||
- Add new example miniapp "ioc_container".
|
||||
- Add new example miniapp "password_hashing".
|
||||
- Add new example miniapp "services_v2".
|
||||
- Rename example miniapp "services" to "services_v1".
|
||||
- Fix incompatibility issue between Python 3.3, pip 10.0.0 and virtualenv
|
||||
16.0.0 (`details <https://github.com/awslabs/base64io-python/issues/4>`_)
|
||||
that caused failures of Python 3.3 tests on Travis.
|
||||
|
|
12
examples/miniapps/password_hashing/README.rst
Normal file
12
examples/miniapps/password_hashing/README.rst
Normal file
|
@ -0,0 +1,12 @@
|
|||
Dependency injection and password hashing in Python
|
||||
===================================================
|
||||
|
||||
Small example that demonstrates using of dependency injection for user
|
||||
password hashing.
|
||||
|
||||
|
||||
instructions for running:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python example.py
|
42
examples/miniapps/password_hashing/example.py
Normal file
42
examples/miniapps/password_hashing/example.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
"""Example of dependency injection and password hashing in Python."""
|
||||
|
||||
import passlib.hash
|
||||
|
||||
import dependency_injector.containers as containers
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class UsersService(object):
|
||||
"""Users service."""
|
||||
|
||||
def __init__(self, password_hasher):
|
||||
"""Initializer."""
|
||||
self._password_hasher = password_hasher
|
||||
|
||||
def create_user(self, name, password):
|
||||
"""Create user with hashed password."""
|
||||
hashed_password = self._password_hasher(password)
|
||||
return dict(name=name, password=hashed_password)
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
"""Inversion of control container."""
|
||||
|
||||
password_hasher = providers.Callable(
|
||||
passlib.hash.sha256_crypt.encrypt,
|
||||
salt_size=16,
|
||||
rounds=10000)
|
||||
|
||||
users_service = providers.Factory(
|
||||
UsersService,
|
||||
password_hasher=password_hasher.provider)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
container = Container()
|
||||
users_service = container.users_service()
|
||||
|
||||
user1 = users_service.create_user(name='Roman', password='secret1')
|
||||
user2 = users_service.create_user(name='Vitaly', password='secret2')
|
||||
|
||||
print(user1, user2)
|
Loading…
Reference in New Issue
Block a user