mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
updating examples codestyle
This commit is contained in:
parent
6eadac8a75
commit
d48616736b
|
@ -1,69 +1,71 @@
|
|||
"""
|
||||
Config provider examples.
|
||||
"""
|
||||
"""Config provider examples."""
|
||||
|
||||
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import (
|
||||
Config,
|
||||
NewInstance,
|
||||
)
|
||||
|
||||
from objects.providers import Config
|
||||
from objects.providers import NewInstance
|
||||
|
||||
from objects.injections import InitArg
|
||||
|
||||
|
||||
# Some example class.
|
||||
class ObjectA(object):
|
||||
def __init__(self, setting_one, setting_two, setting_three):
|
||||
self.setting_one = setting_one
|
||||
self.setting_two = setting_two
|
||||
self.setting_three = setting_three
|
||||
|
||||
"""Example class ObjectA, that has dependencies on some setting values."""
|
||||
|
||||
def __init__(self, fee, price, timezone):
|
||||
"""Initializer."""
|
||||
self.fee = fee
|
||||
self.price = price
|
||||
self.timezone = timezone
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
config = Config()
|
||||
""" :type: (objects.Config) """
|
||||
""":type: (objects.Config)"""
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
InitArg('setting_one', config.SETTING_ONE),
|
||||
InitArg('setting_two', config.SETTING_TWO),
|
||||
InitArg('setting_three', config.GLOBAL.SETTING_THREE))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
InitArg('fee', config.FEE),
|
||||
InitArg('price', config.PRICE),
|
||||
InitArg('timezone', config.GLOBAL.TIMEZONE))
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
|
||||
# Setting config value and making some tests.
|
||||
Catalog.config.update_from({
|
||||
'SETTING_ONE': 1,
|
||||
'SETTING_TWO': 2,
|
||||
'FEE': 1.25,
|
||||
'PRICE': 2.99,
|
||||
'GLOBAL': {
|
||||
'SETTING_THREE': 3
|
||||
'TIMEZONE': 'US/Eastern'
|
||||
}
|
||||
})
|
||||
|
||||
object_a1 = Catalog.object_a()
|
||||
|
||||
assert object_a1.setting_one == 1
|
||||
assert object_a1.setting_two == 2
|
||||
assert object_a1.setting_three == 3
|
||||
assert object_a1.fee == 1.25
|
||||
assert object_a1.price == 2.99
|
||||
assert object_a1.timezone == 'US/Eastern'
|
||||
|
||||
# Changing config value one more time and making some tests.
|
||||
Catalog.config.update_from({
|
||||
'SETTING_ONE': 11,
|
||||
'SETTING_TWO': 22,
|
||||
'FEE': 5.25,
|
||||
'PRICE': 19.99,
|
||||
'GLOBAL': {
|
||||
'SETTING_THREE': 33
|
||||
'TIMEZONE': 'US/Western'
|
||||
}
|
||||
})
|
||||
|
||||
object_a2 = Catalog.object_a()
|
||||
|
||||
assert object_a2.setting_one == 11
|
||||
assert object_a2.setting_two == 22
|
||||
assert object_a2.setting_three == 33
|
||||
# New one ObjectA has new config values.
|
||||
assert object_a2.fee == 5.25
|
||||
assert object_a2.price == 19.99
|
||||
assert object_a2.timezone == 'US/Western'
|
||||
|
||||
assert object_a1.setting_one == 1
|
||||
assert object_a1.setting_two == 2
|
||||
assert object_a1.setting_three == 3
|
||||
# And old one has old ones.
|
||||
assert object_a1.fee == 1.25
|
||||
assert object_a1.price == 2.99
|
||||
assert object_a1.timezone == 'US/Eastern'
|
||||
|
|
|
@ -1,52 +1,56 @@
|
|||
"""Example of providers delegate."""
|
||||
"""Provider delegation example."""
|
||||
|
||||
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import (
|
||||
Singleton,
|
||||
NewInstance,
|
||||
)
|
||||
from objects.injections import (
|
||||
InitArg,
|
||||
Attribute,
|
||||
)
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
|
||||
from objects.injections import InitArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
# Some example classes.
|
||||
class ObjectA(object):
|
||||
|
||||
"""Example class ObjectA, that has dependency on database."""
|
||||
|
||||
def __init__(self, db):
|
||||
"""Initializer."""
|
||||
self.db = db
|
||||
|
||||
|
||||
class ObjectB(object):
|
||||
def __init__(self, a_factory):
|
||||
self.a_factory = a_factory
|
||||
|
||||
"""Example class ObjectB, that has dependency on ObjectA provider."""
|
||||
|
||||
def __init__(self, a_provider):
|
||||
"""Initializer."""
|
||||
self.a_provider = a_provider
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
|
||||
"""Objects catalog."""
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
object_b = Singleton(ObjectB,
|
||||
InitArg('a_factory', object_a.delegate()))
|
||||
""" :type: (objects.Provider) -> ObjectB """
|
||||
InitArg('a_provider', object_a.delegate()))
|
||||
""":type: (objects.Provider) -> ObjectB"""
|
||||
|
||||
|
||||
# Catalog static provides.
|
||||
b = Catalog.object_b()
|
||||
a1, a2 = b.a_factory(), b.a_factory()
|
||||
a1, a2 = b.a_provider(), b.a_provider()
|
||||
|
||||
# Some asserts.
|
||||
assert a1 is not a2
|
||||
|
|
|
@ -1,46 +1,52 @@
|
|||
"""
|
||||
Concept example of objects catalogs.
|
||||
"""
|
||||
"""External dependency example."""
|
||||
|
||||
|
||||
from objects import AbstractCatalog
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
from objects.providers import ExternalDependency
|
||||
|
||||
from objects.injections import InitArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
# Some example classes.
|
||||
class ObjectA(object):
|
||||
|
||||
"""Example class ObjectA, that has dependency on database."""
|
||||
|
||||
def __init__(self, db):
|
||||
"""Initializer."""
|
||||
self.db = db
|
||||
|
||||
|
||||
class ObjectB(object):
|
||||
|
||||
"""Example class ObjectB, that has dependencies on ObjectA and database."""
|
||||
|
||||
def __init__(self, a, db):
|
||||
"""Initializer."""
|
||||
self.a = a
|
||||
self.db = db
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
database = ExternalDependency(instance_of=sqlite3.Connection)
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
object_b = NewInstance(ObjectB,
|
||||
InitArg('a', object_a),
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> ObjectB """
|
||||
""":type: (objects.Provider) -> ObjectB"""
|
||||
|
||||
|
||||
# Satisfaction of external dependency.
|
||||
|
|
|
@ -1,61 +1,54 @@
|
|||
"""
|
||||
Concept example of objects overrides.
|
||||
"""
|
||||
"""Override example."""
|
||||
|
||||
|
||||
from objects import (
|
||||
AbstractCatalog,
|
||||
overrides,
|
||||
)
|
||||
from objects.providers import (
|
||||
Singleton,
|
||||
NewInstance,
|
||||
)
|
||||
from objects.injections import (
|
||||
InitArg,
|
||||
Attribute,
|
||||
)
|
||||
from objects import AbstractCatalog
|
||||
from objects import overrides
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import NewInstance
|
||||
|
||||
from objects.injections import InitArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
# Some example class.
|
||||
class ObjectA(object):
|
||||
|
||||
"""Example class ObjectA, that has dependency on database."""
|
||||
|
||||
def __init__(self, db):
|
||||
"""Initializer."""
|
||||
self.db = db
|
||||
|
||||
|
||||
# Mock of example class.
|
||||
class ObjectAMock(ObjectA):
|
||||
pass
|
||||
|
||||
"""Mock of ObjectA example class."""
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||
|
||||
object_a = NewInstance(ObjectA,
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
|
||||
# Overriding Catalog by SandboxCatalog with some mocks.
|
||||
@overrides(Catalog)
|
||||
class SandboxCatalog(Catalog):
|
||||
"""
|
||||
Sandbox objects catalog with some mocks.
|
||||
"""
|
||||
|
||||
"""Sandbox objects catalog with some mocks that overrides Catalog."""
|
||||
|
||||
object_a = NewInstance(ObjectAMock,
|
||||
InitArg('db', Catalog.database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
|
||||
# Catalog static provides.
|
||||
|
|
|
@ -1,39 +1,38 @@
|
|||
"""
|
||||
Scoped provider examples.
|
||||
"""
|
||||
"""Scoped provider examples."""
|
||||
|
||||
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import (
|
||||
Singleton,
|
||||
Scoped,
|
||||
)
|
||||
from objects.injections import (
|
||||
InitArg,
|
||||
Attribute,
|
||||
)
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Scoped
|
||||
|
||||
from objects.injections import InitArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
class ObjectA(object):
|
||||
|
||||
"""Example class ObjectA, that has dependency on database."""
|
||||
|
||||
def __init__(self, db):
|
||||
"""Initializer."""
|
||||
self.db = db
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||
|
||||
object_a = Scoped(ObjectA,
|
||||
InitArg('db', database))
|
||||
""" :type: (objects.Provider) -> ObjectA """
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
|
||||
# Making scope using `with` statement.
|
||||
|
|
|
@ -49,6 +49,7 @@ class AbstractCatalog(object):
|
|||
def overrides(catalog):
|
||||
"""Catalog overriding decorator."""
|
||||
def decorator(overriding_catalog):
|
||||
"""Overriding decorator."""
|
||||
catalog.__override___(overriding_catalog)
|
||||
return overriding_catalog
|
||||
return decorator
|
||||
|
|
Loading…
Reference in New Issue
Block a user