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