2015-11-22 00:59:36 +03:00
|
|
|
"""`Factory` providers with init keyword injections example."""
|
2015-06-05 11:29:36 +03:00
|
|
|
|
2015-11-22 00:59:36 +03:00
|
|
|
from dependency_injector import providers
|
2015-06-05 11:29:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
class User(object):
|
2015-06-05 12:28:12 +03:00
|
|
|
"""Example class User."""
|
2015-06-05 11:29:36 +03:00
|
|
|
|
2015-06-05 12:28:12 +03:00
|
|
|
def __init__(self, main_photo):
|
2015-07-25 00:14:45 +03:00
|
|
|
"""Initializer."""
|
2015-06-05 11:29:36 +03:00
|
|
|
self.main_photo = main_photo
|
|
|
|
super(User, self).__init__()
|
|
|
|
|
|
|
|
|
|
|
|
class Photo(object):
|
|
|
|
"""Example class Photo."""
|
|
|
|
|
2015-06-05 12:28:12 +03:00
|
|
|
# User and Photo factories:
|
2015-11-22 00:59:36 +03:00
|
|
|
photos_factory = providers.Factory(Photo)
|
|
|
|
users_factory = providers.Factory(User, main_photo=photos_factory)
|
2015-06-05 11:29:36 +03:00
|
|
|
|
|
|
|
# Creating several User objects:
|
2015-06-08 11:33:27 +03:00
|
|
|
user1 = users_factory() # Same as: user1 = User(main_photo=Photo())
|
|
|
|
user2 = users_factory() # Same as: user2 = User(main_photo=Photo())
|
2015-06-05 11:29:36 +03:00
|
|
|
|
|
|
|
# Making some asserts:
|
2015-06-05 12:28:12 +03:00
|
|
|
assert isinstance(user1, User)
|
|
|
|
assert isinstance(user1.main_photo, Photo)
|
|
|
|
|
|
|
|
assert isinstance(user2, User)
|
|
|
|
assert isinstance(user2.main_photo, Photo)
|
|
|
|
|
2015-06-05 11:29:36 +03:00
|
|
|
assert user1 is not user2
|
|
|
|
assert user1.main_photo is not user2.main_photo
|