2015-07-24 21:12:52 +00:00
|
|
|
"""Simple providers overriding example."""
|
|
|
|
|
2015-09-02 15:14:13 +00:00
|
|
|
import dependency_injector as di
|
2015-07-24 21:12:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class User(object):
|
|
|
|
"""Example class User."""
|
|
|
|
|
|
|
|
# Users factory:
|
2015-09-02 15:14:13 +00:00
|
|
|
users_factory = di.Factory(User)
|
2015-07-24 21:12:52 +00:00
|
|
|
|
|
|
|
# Creating several User objects:
|
|
|
|
user1 = users_factory()
|
|
|
|
user2 = users_factory()
|
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert user1 is not user2
|
|
|
|
assert isinstance(user1, User) and isinstance(user2, User)
|
|
|
|
|
|
|
|
|
2015-07-25 01:57:20 +00:00
|
|
|
# Extending User:
|
2015-07-24 21:12:52 +00:00
|
|
|
class SuperUser(User):
|
|
|
|
"""Example class SuperUser."""
|
|
|
|
|
2015-07-25 01:57:20 +00:00
|
|
|
# Overriding users factory:
|
2015-09-02 15:14:13 +00:00
|
|
|
users_factory.override(di.Factory(SuperUser))
|
2015-07-24 21:12:52 +00:00
|
|
|
|
|
|
|
# Creating some more User objects using overridden users factory:
|
|
|
|
user3 = users_factory()
|
|
|
|
user4 = users_factory()
|
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert user4 is not user3
|
|
|
|
assert isinstance(user3, SuperUser) and isinstance(user4, SuperUser)
|