mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Merge pull request #6 from rmk135/callable_provider
Adding callable provider with examples
This commit is contained in:
commit
781965ddb1
47
README.md
47
README.md
|
@ -310,3 +310,50 @@ assert (object_a2 is not object_a3) and \
|
|||
(object_a2 is not object_a5) and \
|
||||
(object_a2 is not object_a6)
|
||||
```
|
||||
|
||||
Example of objects catalog with callable provider:
|
||||
|
||||
```python
|
||||
"""
|
||||
Callable provider examples.
|
||||
"""
|
||||
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import Singleton, Callable
|
||||
from objects.injections import Injection, InitArg, Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
# Some example function.
|
||||
def consuming_function(arg, db):
|
||||
return arg, db
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
|
||||
consuming_function = Callable(consuming_function,
|
||||
Injection('db', database))
|
||||
""" :type: (objects.Provider) -> consuming_function """
|
||||
|
||||
|
||||
# Some calls.
|
||||
arg1, db1 = Catalog.consuming_function(1)
|
||||
arg2, db2 = Catalog.consuming_function(2)
|
||||
arg3, db3 = Catalog.consuming_function(3)
|
||||
|
||||
# Some asserts.
|
||||
assert db1 is db2 is db3
|
||||
assert arg1 == 1
|
||||
assert arg2 == 2
|
||||
assert arg3 == 3
|
||||
```
|
||||
|
|
42
examples/callable_provider.py
Normal file
42
examples/callable_provider.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
"""
|
||||
Callable provider examples.
|
||||
"""
|
||||
|
||||
from objects import AbstractCatalog
|
||||
from objects.providers import Singleton, Callable
|
||||
from objects.injections import Injection, InitArg, Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
# Some example function.
|
||||
def consuming_function(arg, db):
|
||||
return arg, db
|
||||
|
||||
|
||||
# Catalog of objects providers.
|
||||
class Catalog(AbstractCatalog):
|
||||
"""
|
||||
Objects catalog.
|
||||
"""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""" :type: (objects.Provider) -> sqlite3.Connection """
|
||||
|
||||
consuming_function = Callable(consuming_function,
|
||||
Injection('db', database))
|
||||
""" :type: (objects.Provider) -> consuming_function """
|
||||
|
||||
|
||||
# Some calls.
|
||||
arg1, db1 = Catalog.consuming_function(1)
|
||||
arg2, db2 = Catalog.consuming_function(2)
|
||||
arg3, db3 = Catalog.consuming_function(3)
|
||||
|
||||
# Some asserts.
|
||||
assert db1 is db2 is db3
|
||||
assert arg1 == 1
|
||||
assert arg2 == 2
|
||||
assert arg3 == 3
|
|
@ -3,7 +3,7 @@ Standard providers.
|
|||
"""
|
||||
|
||||
from collections import Iterable
|
||||
from .injections import InitArg, Attribute, Method
|
||||
from .injections import Injection, InitArg, Attribute, Method
|
||||
|
||||
|
||||
class Provider(object):
|
||||
|
@ -247,3 +247,31 @@ class Value(_StaticProvider):
|
|||
"""
|
||||
Value provider provides value.
|
||||
"""
|
||||
|
||||
|
||||
class Callable(Provider):
|
||||
"""
|
||||
Callable providers will provides callable calls with some predefined
|
||||
dependencies injections.
|
||||
"""
|
||||
|
||||
def __init__(self, calls, *injections):
|
||||
"""
|
||||
Initializer.
|
||||
"""
|
||||
self.calls = calls
|
||||
self.injections = fetch_injections(injections, Injection)
|
||||
super(Callable, self).__init__()
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
"""
|
||||
Returns provided instance.
|
||||
"""
|
||||
if self.__overridden_by__:
|
||||
return self.__overridden_by__[-1].__call__(*args, **kwargs)
|
||||
|
||||
injections = prepare_injections(self.injections)
|
||||
injections = dict(injections)
|
||||
injections.update(kwargs)
|
||||
|
||||
return self.calls(*args, **injections)
|
||||
|
|
Loading…
Reference in New Issue
Block a user