Update quotes in container examples

This commit is contained in:
Roman Mogylatov 2021-09-30 15:16:17 -04:00
parent b3732281a1
commit d827f93816
16 changed files with 47 additions and 47 deletions

View File

@ -9,7 +9,7 @@ class Container(containers.DeclarativeContainer):
service2 = providers.Dependency() service2 = providers.Dependency()
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
container.check_dependencies() # <-- raises error: container.check_dependencies() # <-- raises error:
# Container has undefined dependencies: "Container.service1", "Container.service2" # Container has undefined dependencies: "Container.service1", "Container.service2"

View File

@ -10,7 +10,7 @@ class Container(containers.DeclarativeContainer):
factory2 = providers.Factory(object) factory2 = providers.Factory(object)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
object1 = container.factory1() object1 = container.factory1()
@ -18,6 +18,6 @@ if __name__ == '__main__':
print(container.providers) print(container.providers)
# { # {
# 'factory1': <dependency_injector.providers.Factory(...), # "factory1": <dependency_injector.providers.Factory(...),
# 'factory2': <dependency_injector.providers.Factory(...), # "factory2": <dependency_injector.providers.Factory(...),
# } # }

View File

@ -13,7 +13,7 @@ class Service:
class SourceContainer(containers.DeclarativeContainer): class SourceContainer(containers.DeclarativeContainer):
database = providers.Singleton(sqlite3.connect, ':memory:') database = providers.Singleton(sqlite3.connect, ":memory:")
service = providers.Factory(Service, db=database) service = providers.Factory(Service, db=database)
@ -24,7 +24,7 @@ class DestinationContainer(SourceContainer):
database = providers.Singleton(mock.Mock) database = providers.Singleton(mock.Mock)
if __name__ == '__main__': if __name__ == "__main__":
container = DestinationContainer() container = DestinationContainer()
service = container.service() service = container.service()

View File

@ -9,21 +9,21 @@ class Service:
class Base(containers.DeclarativeContainer): class Base(containers.DeclarativeContainer):
dependency = providers.Dependency(instance_of=str, default='Default value') dependency = providers.Dependency(instance_of=str, default="Default value")
service = providers.Factory(Service, dependency=dependency) service = providers.Factory(Service, dependency=dependency)
@containers.copy(Base) @containers.copy(Base)
class Derived1(Base): class Derived1(Base):
dependency = providers.Dependency(instance_of=str, default='Derived 1') dependency = providers.Dependency(instance_of=str, default="Derived 1")
# @containers.copy(Base) # <-- No @copy decorator # @containers.copy(Base) # <-- No @copy decorator
class Derived2(Base): class Derived2(Base):
dependency = providers.Dependency(instance_of=str, default='Derived 2') dependency = providers.Dependency(instance_of=str, default="Derived 2")
if __name__ == '__main__': if __name__ == "__main__":
container1 = Derived1() container1 = Derived1()
service1 = container1.service() service1 = container1.service()
print(service1.dependency) # Derived 1 print(service1.dependency) # Derived 1

View File

@ -14,21 +14,21 @@ class ContainerB(ContainerA):
assert ContainerA.providers == { assert ContainerA.providers == {
'provider1': ContainerA.provider1, "provider1": ContainerA.provider1,
} }
assert ContainerB.providers == { assert ContainerB.providers == {
'provider1': ContainerA.provider1, "provider1": ContainerA.provider1,
'provider2': ContainerB.provider2, "provider2": ContainerB.provider2,
} }
assert ContainerA.cls_providers == { assert ContainerA.cls_providers == {
'provider1': ContainerA.provider1, "provider1": ContainerA.provider1,
} }
assert ContainerB.cls_providers == { assert ContainerB.cls_providers == {
'provider2': ContainerB.provider2, "provider2": ContainerB.provider2,
} }
assert ContainerA.inherited_providers == {} assert ContainerA.inherited_providers == {}
assert ContainerB.inherited_providers == { assert ContainerB.inherited_providers == {
'provider1': ContainerA.provider1, "provider1": ContainerA.provider1,
} }

View File

@ -18,7 +18,7 @@ class AuthService:
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
database = providers.Singleton(sqlite3.connect, ':memory:') database = providers.Singleton(sqlite3.connect, ":memory:")
user_service = providers.Factory( user_service = providers.Factory(
UserService, UserService,
@ -32,7 +32,7 @@ class Container(containers.DeclarativeContainer):
) )
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
user_service = container.user_service() user_service = container.user_service()

View File

@ -8,7 +8,7 @@ from dependency_injector import containers, providers
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
database = providers.Singleton(sqlite3.connect, ':memory:') database = providers.Singleton(sqlite3.connect, ":memory:")
# Overriding ``Container`` with ``OverridingContainer``: # Overriding ``Container`` with ``OverridingContainer``:
@ -18,7 +18,7 @@ class OverridingContainer(containers.DeclarativeContainer):
database = providers.Singleton(mock.Mock) database = providers.Singleton(mock.Mock)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
database = container.database() database = container.database()

View File

@ -8,10 +8,10 @@ from dependency_injector import containers, providers
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
database = providers.Singleton(sqlite3.connect, ':memory:') database = providers.Singleton(sqlite3.connect, ":memory:")
if __name__ == '__main__': if __name__ == "__main__":
container = Container(database=mock.Mock(sqlite3.Connection)) container = Container(database=mock.Mock(sqlite3.Connection))
database = container.database() database = container.database()

View File

@ -3,7 +3,7 @@
from dependency_injector import containers, providers from dependency_injector import containers, providers
if __name__ == '__main__': if __name__ == "__main__":
container = containers.DynamicContainer() container = containers.DynamicContainer()
container.factory1 = providers.Factory(object) container.factory1 = providers.Factory(object)
container.factory2 = providers.Factory(object) container.factory2 = providers.Factory(object)
@ -13,6 +13,6 @@ if __name__ == '__main__':
print(container.providers) print(container.providers)
# { # {
# 'factory1': <dependency_injector.providers.Factory(...), # "factory1": <dependency_injector.providers.Factory(...),
# 'factory2': <dependency_injector.providers.Factory(...), # "factory2": <dependency_injector.providers.Factory(...),
# } # }

View File

@ -13,20 +13,20 @@ class AuthService:
def populate_container(container, providers_config): def populate_container(container, providers_config):
for provider_name, provider_info in providers_config.items(): for provider_name, provider_info in providers_config.items():
provided_cls = globals().get(provider_info['class']) provided_cls = globals().get(provider_info["class"])
provider_cls = getattr(providers, provider_info['provider_class']) provider_cls = getattr(providers, provider_info["provider_class"])
setattr(container, provider_name, provider_cls(provided_cls)) setattr(container, provider_name, provider_cls(provided_cls))
if __name__ == '__main__': if __name__ == "__main__":
services_config = { services_config = {
'user': { "user": {
'class': 'UserService', "class": "UserService",
'provider_class': 'Factory', "provider_class": "Factory",
}, },
'auth': { "auth": {
'class': 'AuthService', "class": "AuthService",
'provider_class': 'Factory', "provider_class": "Factory",
}, },
} }
services = containers.DynamicContainer() services = containers.DynamicContainer()

View File

@ -21,14 +21,14 @@ class Container(containers.DeclarativeContainer):
__self__ = providers.Self() __self__ = providers.Self()
service1 = providers.Factory(Service, name='Service 1') service1 = providers.Factory(Service, name="Service 1")
service2 = providers.Factory(Service, name='Service 2') service2 = providers.Factory(Service, name="Service 2")
service3 = providers.Factory(Service, name='Service 3') service3 = providers.Factory(Service, name="Service 3")
dispatcher = providers.Singleton(ServiceDispatcher, __self__) dispatcher = providers.Singleton(ServiceDispatcher, __self__)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
dispatcher = container.dispatcher() dispatcher = container.dispatcher()

View File

@ -21,7 +21,7 @@ class OverridingContainer(containers.DeclarativeContainer):
service = providers.Factory(ServiceStub) service = providers.Factory(ServiceStub)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
overriding_container = OverridingContainer() overriding_container = OverridingContainer()

View File

@ -9,7 +9,7 @@ class Container(containers.DeclarativeContainer):
service2 = providers.Singleton(object) service2 = providers.Singleton(object)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
service1 = container.service1() service1 = container.service1()

View File

@ -14,7 +14,7 @@ class Container(containers.DeclarativeContainer):
sub = providers.Container(SubContainer) sub = providers.Container(SubContainer)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
service1 = container.service() service1 = container.service()

View File

@ -8,7 +8,7 @@ class Container(containers.DeclarativeContainer):
service = providers.Singleton(object) service = providers.Singleton(object)
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
service1 = container.service() service1 = container.service()

View File

@ -34,15 +34,15 @@ class Container(containers.DeclarativeContainer):
) )
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
for provider in container.traverse(): for provider in container.traverse():
print(provider) print(provider)
# <dependency_injector.providers.Configuration('config') at 0x10d37d200> # <dependency_injector.providers.Configuration("config") at 0x10d37d200>
# <dependency_injector.providers.Factory(<class '__main__.Service'>) at 0x10d3a2820> # <dependency_injector.providers.Factory(<class "__main__.Service">) at 0x10d3a2820>
# <dependency_injector.providers.Resource(<function init_database at 0x10bd2cb80>) at 0x10d346b40> # <dependency_injector.providers.Resource(<function init_database at 0x10bd2cb80>) at 0x10d346b40>
# <dependency_injector.providers.ConfigurationOption('config.cache_hosts') at 0x10d37d350> # <dependency_injector.providers.ConfigurationOption("config.cache_hosts") at 0x10d37d350>
# <dependency_injector.providers.Resource(<function init_cache at 0x10be373a0>) at 0x10d346bc0> # <dependency_injector.providers.Resource(<function init_cache at 0x10be373a0>) at 0x10d346bc0>
# <dependency_injector.providers.ConfigurationOption('config.database_url') at 0x10d37d2e0> # <dependency_injector.providers.ConfigurationOption("config.database_url") at 0x10d37d2e0>