From 0a4e8c693478f18d323aae995863e2be5eb088ab Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 2 Sep 2015 00:24:19 +0300 Subject: [PATCH] Update catalog docs examples --- examples/catalogs/operating_with_providers.py | 30 +++++++------- examples/catalogs/override.py | 37 ++++++++--------- examples/catalogs/override_decorator.py | 40 +++++++++---------- examples/catalogs/simple.py | 31 +++++++------- 4 files changed, 63 insertions(+), 75 deletions(-) diff --git a/examples/catalogs/operating_with_providers.py b/examples/catalogs/operating_with_providers.py index 3c82b0e7..aac10d07 100644 --- a/examples/catalogs/operating_with_providers.py +++ b/examples/catalogs/operating_with_providers.py @@ -1,25 +1,23 @@ """Operating with catalog providers example.""" -from dependency_injector.catalog import AbstractCatalog -from dependency_injector.providers import Factory -from dependency_injector.providers import Singleton +import dependency_injector as di -class Catalog(AbstractCatalog): +class Catalog(di.AbstractCatalog): """Providers catalog.""" - provider1 = Factory(object) - """:type: (dependency_injector.Provider) -> object""" + provider1 = di.Factory(object) + """:type: (di.Provider) -> object""" - provider2 = Factory(object) - """:type: (dependency_injector.Provider) -> object""" + provider2 = di.Factory(object) + """:type: (di.Provider) -> object""" - provider3 = Singleton(object) - """:type: (dependency_injector.Provider) -> object""" + provider3 = di.Singleton(object) + """:type: (di.Provider) -> object""" - provider4 = Singleton(object) - """:type: (dependency_injector.Provider) -> object""" + provider4 = di.Singleton(object) + """:type: (di.Provider) -> object""" # Making some asserts: @@ -27,7 +25,7 @@ assert Catalog.providers == dict(provider1=Catalog.provider1, provider2=Catalog.provider2, provider3=Catalog.provider3, provider4=Catalog.provider4) -assert Catalog.filter(Factory) == dict(provider1=Catalog.provider1, - provider2=Catalog.provider2) -assert Catalog.filter(Singleton) == dict(provider3=Catalog.provider3, - provider4=Catalog.provider4) +assert Catalog.filter(di.Factory) == dict(provider1=Catalog.provider1, + provider2=Catalog.provider2) +assert Catalog.filter(di.Singleton) == dict(provider3=Catalog.provider3, + provider4=Catalog.provider4) diff --git a/examples/catalogs/override.py b/examples/catalogs/override.py index baaf690c..ff45a9ca 100644 --- a/examples/catalogs/override.py +++ b/examples/catalogs/override.py @@ -1,44 +1,41 @@ """`Catalog.override()` example.""" -from collections import namedtuple - -from dependency_injector.catalog import AbstractCatalog -from dependency_injector.providers import Factory -from dependency_injector.injections import KwArg +import collections +import dependency_injector as di # Creating some example classes: -Object1 = namedtuple('Object1', ['arg1', 'arg2']) -Object2 = namedtuple('Object2', ['object1']) -ExtendedObject2 = namedtuple('ExtendedObject2', []) +Object1 = collections.namedtuple('Object1', ['arg1', 'arg2']) +Object2 = collections.namedtuple('Object2', ['object1']) +ExtendedObject2 = collections.namedtuple('ExtendedObject2', []) -class Catalog(AbstractCatalog): +class Catalog(di.AbstractCatalog): """Providers catalog.""" - object1_factory = Factory(Object1, - KwArg('arg1', 1), - KwArg('arg2', 2)) - """:type: (dependency_injector.Provider) -> Object1""" + object1_factory = di.Factory(Object1, + arg1=1, + arg2=2) + """:type: (di.Provider) -> Object1""" - object2_factory = Factory(Object2, - KwArg('object1', object1_factory)) - """:type: (dependency_injector.Provider) -> Object2""" + object2_factory = di.Factory(Object2, + object1=object1_factory) + """:type: (di.Provider) -> Object2""" -class AnotherCatalog(AbstractCatalog): +class AnotherCatalog(di.AbstractCatalog): """Another providers catalog.""" - object2_factory = Factory(ExtendedObject2) - """:type: (dependency_injector.Provider) -> ExtendedObject2""" + object2_factory = di.Factory(ExtendedObject2) + """:type: (di.Provider) -> ExtendedObject2""" # Overriding `Catalog` with `AnotherCatalog`: Catalog.override(AnotherCatalog) -# Creating some dependency_injector using overridden catalog: +# Creating some objects using overridden catalog: object2_1 = Catalog.object2_factory() object2_2 = Catalog.object2_factory() diff --git a/examples/catalogs/override_decorator.py b/examples/catalogs/override_decorator.py index 27fa9c24..099aa577 100644 --- a/examples/catalogs/override_decorator.py +++ b/examples/catalogs/override_decorator.py @@ -1,44 +1,40 @@ """Catalog `@override()` decorator example.""" -from collections import namedtuple - -from dependency_injector.catalog import AbstractCatalog -from dependency_injector.catalog import override -from dependency_injector.providers import Factory -from dependency_injector.injections import KwArg +import collections +import dependency_injector as di # Creating some example classes: -Object1 = namedtuple('Object1', ['arg1', 'arg2']) -Object2 = namedtuple('Object2', ['object1']) -ExtendedObject2 = namedtuple('ExtendedObject2', []) +Object1 = collections.namedtuple('Object1', ['arg1', 'arg2']) +Object2 = collections.namedtuple('Object2', ['object1']) +ExtendedObject2 = collections.namedtuple('ExtendedObject2', []) -class Catalog(AbstractCatalog): +class Catalog(di.AbstractCatalog): """Providers catalog.""" - object1_factory = Factory(Object1, - KwArg('arg1', 1), - KwArg('arg2', 2)) - """:type: (dependency_injector.Provider) -> Object1""" + object1_factory = di.Factory(Object1, + arg1=1, + arg2=2) + """:type: (di.Provider) -> Object1""" - object2_factory = Factory(Object2, - KwArg('object1', object1_factory)) - """:type: (dependency_injector.Provider) -> Object2""" + object2_factory = di.Factory(Object2, + object1=object1_factory) + """:type: (di.Provider) -> Object2""" # Overriding `Catalog` with `AnotherCatalog`: -@override(Catalog) -class AnotherCatalog(AbstractCatalog): +@di.override(Catalog) +class AnotherCatalog(di.AbstractCatalog): """Another providers catalog.""" - object2_factory = Factory(ExtendedObject2) - """:type: (dependency_injector.Provider) -> ExtendedObject2""" + object2_factory = di.Factory(ExtendedObject2) + """:type: (di.Provider) -> ExtendedObject2""" -# Creating some dependency_injector using overridden catalog: +# Creating some objects using overridden catalog: object2_1 = Catalog.object2_factory() object2_2 = Catalog.object2_factory() diff --git a/examples/catalogs/simple.py b/examples/catalogs/simple.py index 951da2e1..19ab853d 100644 --- a/examples/catalogs/simple.py +++ b/examples/catalogs/simple.py @@ -1,31 +1,28 @@ -"""Catalog example.""" +"""Catalog example.""" -from collections import namedtuple - -from dependency_injector.catalog import AbstractCatalog -from dependency_injector.providers import Factory -from dependency_injector.injections import KwArg +import collections +import dependency_injector as di # Creating some example classes: -Object1 = namedtuple('Object1', ['arg1', 'arg2']) -Object2 = namedtuple('Object2', ['object1']) +Object1 = collections.namedtuple('Object1', ['arg1', 'arg2']) +Object2 = collections.namedtuple('Object2', ['object1']) -class Catalog(AbstractCatalog): +class Catalog(di.AbstractCatalog): """Providers catalog.""" - object1_factory = Factory(Object1, - KwArg('arg1', 1), - KwArg('arg2', 2)) - """:type: (dependency_injector.Provider) -> Object1""" + object1_factory = di.Factory(Object1, + arg1=1, + arg2=2) + """:type: (di.Provider) -> Object1""" - object2_factory = Factory(Object2, - KwArg('object1', object1_factory)) - """:type: (dependency_injector.Provider) -> Object2""" + object2_factory = di.Factory(Object2, + object1=object1_factory) + """:type: (di.Provider) -> Object2""" -# Creating some dependency_injector: +# Creating some objects: object2_1 = Catalog.object2_factory() object2_2 = Catalog.object2_factory()