Update main example

This commit is contained in:
Roman Mogilatov 2016-04-20 14:25:40 +03:00
parent 4cf3bdb1fb
commit 1292d4af68
2 changed files with 16 additions and 26 deletions

View File

@ -52,30 +52,31 @@ Installation
pip install dependency_injector
Examples
--------
Example
-------
.. code-block:: python
"""Dependency Injector initial example."""
"""Dependency Injector example."""
import sys
import sqlite3
import boto.s3.connection
import services
from boto.s3.connection import S3Connection
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
import services # Example business services module (Users, Photos, Auth)
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect, ':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection,
s3 = providers.Singleton(S3Connection,
aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
@ -97,19 +98,13 @@ Examples
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
def main(argv, users_service, auth_service):
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
"""Main function."""
login, password, photo_path = argv[1:]
user = users_service.get_user(login)
auth_service.authenticate(user, password)
upload_photo(user, photo_path)
@injections.inject(photos_service=Services.photos)
def upload_photo(user, photo_path, photos_service):
"""Upload photo."""
photos_service.upload_photo(user['id'], photo_path)

View File

@ -1,22 +1,23 @@
"""Dependency Injector initial example."""
"""Dependency Injector example."""
import sys
import sqlite3
import boto.s3.connection
import services
from boto.s3.connection import S3Connection
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
import services # Example business services module (Users, Photos, Auth)
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect, ':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection,
s3 = providers.Singleton(S3Connection,
aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
@ -38,19 +39,13 @@ class Services(catalogs.DeclarativeCatalog):
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
def main(argv, users_service, auth_service):
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
"""Main function."""
login, password, photo_path = argv[1:]
user = users_service.get_user(login)
auth_service.authenticate(user, password)
upload_photo(user, photo_path)
@injections.inject(photos_service=Services.photos)
def upload_photo(user, photo_path, photos_service):
"""Upload photo."""
photos_service.upload_photo(user['id'], photo_path)