Add example of callable delegation

This commit is contained in:
Roman Mogylatov 2018-06-27 19:29:55 +03:00
parent 96b44004b1
commit a8dd335d71
5 changed files with 78 additions and 2 deletions

View File

@ -20,3 +20,4 @@ and powered by *Dependency Injector* framework.
services_miniapp_v2 services_miniapp_v2
bundles_miniapp bundles_miniapp
use_cases_miniapp use_cases_miniapp
password_hashing_miniapp

View 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::

View File

@ -9,9 +9,11 @@ follows `Semantic versioning`_
Development version 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. - 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 - 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>`_) 16.0.0 (`details <https://github.com/awslabs/base64io-python/issues/4>`_)
that caused failures of Python 3.3 tests on Travis. that caused failures of Python 3.3 tests on Travis.

View 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

View 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)