2015-10-19 14:26:59 +00:00
|
|
|
"""`di.Factory` providers with init positional injections example."""
|
2015-06-05 08:29:36 +00:00
|
|
|
|
2015-09-02 21:24:20 +00:00
|
|
|
import dependency_injector as di
|
2015-06-05 08:29:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class User(object):
|
2015-06-05 09:28:12 +00:00
|
|
|
"""Example class User."""
|
2015-06-05 08:29:36 +00:00
|
|
|
|
2015-06-05 09:28:12 +00:00
|
|
|
def __init__(self, main_photo):
|
2015-07-24 21:14:45 +00:00
|
|
|
"""Initializer."""
|
2015-06-05 08:29:36 +00:00
|
|
|
self.main_photo = main_photo
|
|
|
|
super(User, self).__init__()
|
|
|
|
|
|
|
|
|
|
|
|
class Photo(object):
|
|
|
|
"""Example class Photo."""
|
|
|
|
|
2015-06-05 09:28:12 +00:00
|
|
|
# User and Photo factories:
|
2015-09-02 21:24:20 +00:00
|
|
|
photos_factory = di.Factory(Photo)
|
2015-10-19 14:26:59 +00:00
|
|
|
users_factory = di.Factory(User, photos_factory)
|
2015-06-05 08:29:36 +00:00
|
|
|
|
|
|
|
# Creating several User objects:
|
2015-10-19 14:26:59 +00:00
|
|
|
user1 = users_factory() # Same as: user1 = User(Photo())
|
|
|
|
user2 = users_factory() # Same as: user2 = User(Photo())
|
2015-06-05 08:29:36 +00:00
|
|
|
|
|
|
|
# Making some asserts:
|
2015-06-05 09:28:12 +00:00
|
|
|
assert isinstance(user1, User)
|
|
|
|
assert isinstance(user1.main_photo, Photo)
|
|
|
|
|
|
|
|
assert isinstance(user2, User)
|
|
|
|
assert isinstance(user2.main_photo, Photo)
|
|
|
|
|
2015-06-05 08:29:36 +00:00
|
|
|
assert user1 is not user2
|
|
|
|
assert user1.main_photo is not user2.main_photo
|