python-dependency-injector/examples/providers/factory_init_injections.py

44 lines
995 B
Python
Raw Normal View History

"""`Factory` providers with init injections example."""
from objects.providers import Factory
from objects.injections import KwArg
class User(object):
2015-06-05 12:28:12 +03:00
"""Example class User."""
2015-06-05 12:28:12 +03:00
def __init__(self, main_photo):
"""Initializer.
:param main_photo: Photo
:return:
"""
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:
photos_factory = Factory(Photo)
users_factory = Factory(User,
2015-06-05 12:28:12 +03:00
KwArg('main_photo', photos_factory))
# Creating several User objects:
2015-06-05 12:28:12 +03:00
user1 = users_factory() # Same as: User(main_photo=Photo())
user2 = users_factory() # Same as: User(main_photo=Photo())
# 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)
assert user1 is not user2
assert user1.main_photo is not user2.main_photo