python-dependency-injector/examples/catalogs/declarative_api.py

33 lines
1.0 KiB
Python
Raw Normal View History

2015-11-13 19:58:29 +03:00
"""Declarative catalog API example."""
2015-08-05 16:44:00 +03:00
2015-11-14 23:47:33 +03:00
from dependency_injector import catalogs
from dependency_injector import providers
2015-08-05 16:44:00 +03:00
2015-11-14 23:47:33 +03:00
class CatalogA(catalogs.DeclarativeCatalog):
2015-10-07 19:41:53 +03:00
"""Example catalog A."""
2015-08-05 16:44:00 +03:00
2015-11-14 23:47:33 +03:00
provider1 = providers.Factory(object)
""":type: providers.Provider -> object"""
2015-08-05 16:44:00 +03:00
2015-10-07 19:41:53 +03:00
class CatalogB(CatalogA):
"""Example catalog B."""
2015-08-05 16:44:00 +03:00
2015-11-14 23:47:33 +03:00
provider2 = providers.Singleton(object)
""":type: providers.Provider -> object"""
2015-08-05 16:44:00 +03:00
2015-10-07 19:41:53 +03:00
# Making some asserts for `providers` attribute:
assert CatalogA.providers == dict(provider1=CatalogA.provider1)
assert CatalogB.providers == dict(provider1=CatalogA.provider1,
provider2=CatalogB.provider2)
# Making some asserts for `cls_providers` attribute:
assert CatalogA.cls_providers == dict(provider1=CatalogA.provider1)
assert CatalogB.cls_providers == dict(provider2=CatalogB.provider2)
# Making some asserts for `inherited_providers` attribute:
assert CatalogA.inherited_providers == dict()
assert CatalogB.inherited_providers == dict(provider1=CatalogA.provider1)