mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 04:53:59 +03:00
Update catalog docs examples
This commit is contained in:
parent
e1fc8dde39
commit
0a4e8c6934
|
@ -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,
|
||||
assert Catalog.filter(di.Factory) == dict(provider1=Catalog.provider1,
|
||||
provider2=Catalog.provider2)
|
||||
assert Catalog.filter(Singleton) == dict(provider3=Catalog.provider3,
|
||||
assert Catalog.filter(di.Singleton) == dict(provider3=Catalog.provider3,
|
||||
provider4=Catalog.provider4)
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -1,31 +1,28 @@
|
|||
"""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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user