From f1429d557a1b636d3cc875caed4ab734a9eba13b Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Mon, 11 Jan 2016 11:14:37 +0200 Subject: [PATCH] Update concept examples with provider overriding example --- README.rst | 31 ++++++++++++++++++++++++++----- examples/concept.py | 22 ++++++++++++++++++++++ examples/concept_full_syntax.py | 24 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index dd4a2f4e..3671d99f 100644 --- a/README.rst +++ b/README.rst @@ -93,17 +93,16 @@ Examples class Services(catalogs.DeclarativeCatalog): """Catalog of service providers.""" - database = providers.Singleton(sqlite3.connect, - injections.Arg(':memory:')) + database = providers.Singleton(sqlite3.connect, ':memory:') """:type: providers.Provider -> sqlite3.Connection""" users = providers.Factory(UsersService, - injections.KwArg('db', database)) + db=database) """:type: providers.Provider -> UsersService""" auth = providers.Factory(AuthService, - injections.KwArg('db', database), - injections.KwArg('users_service', users)) + db=database, + users_service=users) """:type: providers.Provider -> AuthService""" @@ -132,6 +131,28 @@ Examples # Making a call of decorated callback: example() + + # Overriding auth service provider and making some asserts: + class ExtendedAuthService(AuthService): + """Extended version of auth service.""" + + def __init__(self, db, users_service, ttl): + """Initializer.""" + self.ttl = ttl + super(ExtendedAuthService, self).__init__(db=db, + users_service=users_service) + + + Services.auth.override(providers.Factory(ExtendedAuthService, + db=Services.database, + users_service=Services.users, + ttl=3600)) + + + auth_service = Services.auth() + + assert isinstance(auth_service, ExtendedAuthService) + You can get more *Dependency Injector* examples in ``/examples`` directory on GitHub: diff --git a/examples/concept.py b/examples/concept.py index 57c4cb68..d9302e57 100644 --- a/examples/concept.py +++ b/examples/concept.py @@ -64,3 +64,25 @@ def example(users_service, auth_service, database): # Making a call of decorated callback: example() + + +# Overriding auth service provider and making some asserts: +class ExtendedAuthService(AuthService): + """Extended version of auth service.""" + + def __init__(self, db, users_service, ttl): + """Initializer.""" + self.ttl = ttl + super(ExtendedAuthService, self).__init__(db=db, + users_service=users_service) + + +Services.auth.override(providers.Factory(ExtendedAuthService, + db=Services.database, + users_service=Services.users, + ttl=3600)) + + +auth_service = Services.auth() + +assert isinstance(auth_service, ExtendedAuthService) diff --git a/examples/concept_full_syntax.py b/examples/concept_full_syntax.py index 3d9b3d4e..16f65cee 100644 --- a/examples/concept_full_syntax.py +++ b/examples/concept_full_syntax.py @@ -65,3 +65,27 @@ def example(users_service, auth_service, database): # Making a call of decorated callback: example() + + +# Overriding auth service provider and making some asserts: +class ExtendedAuthService(AuthService): + """Extended version of auth service.""" + + def __init__(self, db, users_service, ttl): + """Initializer.""" + self.ttl = ttl + super(ExtendedAuthService, self).__init__(db=db, + users_service=users_service) + + +Services.auth.override(providers.Factory(ExtendedAuthService, + injections.KwArg('db', + Services.database), + injections.KwArg('users_service', + Services.users), + injections.KwArg('ttl', 3600))) + + +auth_service = Services.auth() + +assert isinstance(auth_service, ExtendedAuthService)