mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Removing old examples
This commit is contained in:
parent
2c2d4f7434
commit
878f1d49aa
|
@ -1,56 +0,0 @@
|
|||
"""Provider delegation example."""
|
||||
|
||||
from objects.catalog import AbstractCatalog
|
||||
|
||||
from objects.providers import Factory
|
||||
from objects.providers import Singleton
|
||||
|
||||
from objects.injections import KwArg
|
||||
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
|
||||
|
||||
|
||||
class ObjectB(object):
|
||||
|
||||
"""Example class ObjectB, that has dependency on ObjectA provider."""
|
||||
|
||||
def __init__(self, a_provider):
|
||||
"""Initializer."""
|
||||
self.a_provider = a_provider
|
||||
|
||||
|
||||
class Catalog(AbstractCatalog):
|
||||
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
KwArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||
|
||||
object_a = Factory(ObjectA,
|
||||
KwArg('db', database))
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
object_b = Singleton(ObjectB,
|
||||
KwArg('a_provider', object_a.delegate()))
|
||||
""":type: (objects.Provider) -> ObjectB"""
|
||||
|
||||
|
||||
# Catalog static provides.
|
||||
b = Catalog.object_b()
|
||||
a1, a2 = b.a_provider(), b.a_provider()
|
||||
|
||||
# Some asserts.
|
||||
assert a1 is not a2
|
||||
assert a1.db is a2.db is Catalog.database()
|
|
@ -1,24 +0,0 @@
|
|||
"""`Factory` and `Singleton` providers example."""
|
||||
|
||||
from objects.providers import Factory
|
||||
from objects.providers import Singleton
|
||||
|
||||
|
||||
# Factory provider creates new instance of specified class on every call.
|
||||
object_factory = Factory(object)
|
||||
|
||||
object_1 = object_factory()
|
||||
object_2 = object_factory()
|
||||
|
||||
assert object_1 is not object_2
|
||||
assert isinstance(object_1, object) and isinstance(object_2, object)
|
||||
|
||||
# Singleton provider creates new instance of specified class on first call
|
||||
# and returns same instance on every next call.
|
||||
single_object = Singleton(object)
|
||||
|
||||
single_object_1 = single_object()
|
||||
single_object_2 = single_object()
|
||||
|
||||
assert single_object_1 is single_object_2
|
||||
assert isinstance(object_1, object) and isinstance(object_2, object)
|
|
@ -1,45 +0,0 @@
|
|||
"""`Factory` and `Singleton` providers with injections example."""
|
||||
|
||||
import sqlite3
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Factory
|
||||
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
|
||||
class ObjectA(object):
|
||||
|
||||
"""ObjectA has dependency on database."""
|
||||
|
||||
def __init__(self, database):
|
||||
"""Initializer.
|
||||
|
||||
Database dependency need to be injected via init arg."""
|
||||
self.database = database
|
||||
|
||||
def get_one(self):
|
||||
"""Select one from database and return it."""
|
||||
return self.database.execute('SELECT 1').fetchone()[0]
|
||||
|
||||
|
||||
# Database and `ObjectA` providers.
|
||||
database = Singleton(sqlite3.Connection,
|
||||
KwArg('database', ':memory:'),
|
||||
KwArg('timeout', 30),
|
||||
KwArg('detect_types', True),
|
||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a_factory = Factory(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a_factory()
|
||||
object_a_2 = object_a_factory()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
assert object_a_1.database is object_a_2.database is database()
|
||||
assert object_a_1.get_one() == object_a_2.get_one() == 1
|
|
@ -1,66 +0,0 @@
|
|||
"""Providers overriding example."""
|
||||
|
||||
import sqlite3
|
||||
|
||||
from objects.providers import Factory
|
||||
from objects.providers import Singleton
|
||||
|
||||
from objects.injections import KwArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
|
||||
class ObjectA(object):
|
||||
|
||||
"""ObjectA has dependency on database."""
|
||||
|
||||
def __init__(self, database):
|
||||
"""Initializer.
|
||||
|
||||
Database dependency need to be injected via init arg."""
|
||||
self.database = database
|
||||
|
||||
def get_one(self):
|
||||
"""Select one from database and return it."""
|
||||
return self.database.execute('SELECT 1')
|
||||
|
||||
|
||||
class ObjectAMock(ObjectA):
|
||||
|
||||
"""Mock of ObjectA.
|
||||
|
||||
Has no dependency on database.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initializer."""
|
||||
|
||||
def get_one(self):
|
||||
"""Select one from database and return it.
|
||||
|
||||
Mock makes no database queries and always returns two instead of one.
|
||||
"""
|
||||
return 2
|
||||
|
||||
|
||||
# Database and `ObjectA` providers.
|
||||
database = Singleton(sqlite3.Connection,
|
||||
KwArg('database', ':memory:'),
|
||||
KwArg('timeout', 30),
|
||||
KwArg('detect_types', True),
|
||||
KwArg('isolation_level', 'EXCLUSIVE'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
|
||||
object_a_factory = Factory(ObjectA,
|
||||
KwArg('database', database))
|
||||
|
||||
|
||||
# Overriding `ObjectA` provider with `ObjectAMock` provider.
|
||||
object_a_factory.override(Factory(ObjectAMock))
|
||||
|
||||
# Creating several `ObjectA` instances.
|
||||
object_a_1 = object_a_factory()
|
||||
object_a_2 = object_a_factory()
|
||||
|
||||
# Making some asserts.
|
||||
assert object_a_1 is not object_a_2
|
||||
assert object_a_1.get_one() == object_a_2.get_one() == 2
|
|
@ -1,68 +0,0 @@
|
|||
"""Providers delegation example."""
|
||||
|
||||
from objects.providers import Factory
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Delegate
|
||||
|
||||
from objects.injections import KwArg
|
||||
|
||||
|
||||
class User(object):
|
||||
|
||||
"""Example class User."""
|
||||
|
||||
def __init__(self, id, name):
|
||||
"""Initializer.
|
||||
|
||||
:param id: int
|
||||
:param name: str
|
||||
:return:
|
||||
"""
|
||||
self.id = id
|
||||
self.name = name
|
||||
|
||||
|
||||
class UserService(object):
|
||||
|
||||
"""Example class UserService.
|
||||
|
||||
UserService has dependency on users factory.
|
||||
"""
|
||||
|
||||
def __init__(self, users_factory):
|
||||
"""Initializer.
|
||||
|
||||
:param users_factory: objects.providers.Factory
|
||||
:return:
|
||||
"""
|
||||
self.users_factory = users_factory
|
||||
|
||||
def get_by_id(self, id):
|
||||
"""Return user info by user id."""
|
||||
return self.users_factory(id=id, name=self._get_name_from_db(id))
|
||||
|
||||
def _get_name_from_db(self, id):
|
||||
"""Return user's name from database by his id.
|
||||
|
||||
Main purpose of this method is just to show the fact of retrieving
|
||||
some user's data from database, so, actually, it simulates work
|
||||
with database just by merging constant string with provided user's id.
|
||||
"""
|
||||
return ''.join(('user', str(id)))
|
||||
|
||||
|
||||
# Users factory and UserService provider:
|
||||
users_factory = Factory(User)
|
||||
users_service = Singleton(UserService,
|
||||
KwArg('users_factory', Delegate(users_factory)))
|
||||
|
||||
# Creating several User objects:
|
||||
user1 = users_service().get_by_id(1)
|
||||
user2 = users_service().get_by_id(2)
|
||||
|
||||
# Making some asserts:
|
||||
assert user1.id == 1
|
||||
assert user1.name == 'user1'
|
||||
|
||||
assert user2.id == 2
|
||||
assert user2.name == 'user2'
|
Loading…
Reference in New Issue
Block a user