Skip to content

Commit

Permalink
Add example of callable delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
rmk135 committed Jun 27, 2018
1 parent 96b4400 commit a8dd335
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/examples/index.rst
Expand Up @@ -20,3 +20,4 @@ and powered by *Dependency Injector* framework.
services_miniapp_v2
bundles_miniapp
use_cases_miniapp
password_hashing_miniapp
19 changes: 19 additions & 0 deletions docs/examples/password_hashing_miniapp.rst
@@ -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::
6 changes: 4 additions & 2 deletions docs/main/changelog.rst
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions examples/miniapps/password_hashing/README.rst
@@ -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 changes: 42 additions & 0 deletions examples/miniapps/password_hashing/example.py
@@ -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)

0 comments on commit a8dd335

Please sign in to comment.