diff --git a/docs/examples/index.rst b/docs/examples/index.rst index ac1a59da..977a35a2 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -20,3 +20,4 @@ and powered by *Dependency Injector* framework. services_miniapp_v2 bundles_miniapp use_cases_miniapp + password_hashing_miniapp diff --git a/docs/examples/password_hashing_miniapp.rst b/docs/examples/password_hashing_miniapp.rst new file mode 100644 index 00000000..45eb1d4c --- /dev/null +++ b/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:: diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 0f48a211..503e2714 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -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 `_) that caused failures of Python 3.3 tests on Travis. diff --git a/examples/miniapps/password_hashing/README.rst b/examples/miniapps/password_hashing/README.rst new file mode 100644 index 00000000..9c306ab5 --- /dev/null +++ b/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 diff --git a/examples/miniapps/password_hashing/example.py b/examples/miniapps/password_hashing/example.py new file mode 100644 index 00000000..a291485d --- /dev/null +++ b/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)