2015-09-02 14:20:19 +00:00
|
|
|
"""Custom `di.Factory` example."""
|
2015-07-27 22:29:31 +00:00
|
|
|
|
2015-09-02 14:20:19 +00:00
|
|
|
import dependency_injector as di
|
2015-07-27 22:29:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class User(object):
|
|
|
|
"""Example class User."""
|
|
|
|
|
|
|
|
|
2015-09-02 14:20:19 +00:00
|
|
|
class UsersFactory(di.Provider):
|
2015-07-27 22:29:31 +00:00
|
|
|
"""Example users factory."""
|
|
|
|
|
|
|
|
__slots__ = ('_factory',)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initializer."""
|
2015-09-02 14:20:19 +00:00
|
|
|
self._factory = di.Factory(User)
|
2015-07-27 22:29:31 +00:00
|
|
|
super(UsersFactory, self).__init__()
|
|
|
|
|
|
|
|
def _provide(self, *args, **kwargs):
|
|
|
|
"""Return provided instance."""
|
|
|
|
return self._factory(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
# Users factory:
|
|
|
|
users_factory = UsersFactory()
|
|
|
|
|
|
|
|
# Creating several User objects:
|
|
|
|
user1 = users_factory()
|
|
|
|
user2 = users_factory()
|
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert isinstance(user1, User)
|
|
|
|
assert isinstance(user2, User)
|
|
|
|
assert user1 is not user2
|