Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 42 additions & 0 deletions examples/callable_provider.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 29 additions & 1 deletion objects/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from collections import Iterable
from .injections import InitArg, Attribute, Method
from .injections import Injection, InitArg, Attribute, Method


class Provider(object):
Expand Down Expand Up @@ -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)