Open
Description
I want to use the object given by another provider for the key of Dict provider.
However, as I have found, the value of Dict provider only can be another provider value, not key.
For example, I want to do like dict_provider of WorkersContainer. When I call summary method for dict_provider, it gives <dependency_injector.providers.Dependency>
class for the key and 3 for the value.
from dependency_injector import containers, providers
class ListWorkers:
def __init__(self, machines: list[tuple[str, int]]):
self._machines = machines
def summary(self):
for m in self._machines:
print(f"[ListWorkers] {m[0]}: {m[1]}")
class DictWorkers:
def __init__(self, machines: dict[str, int]):
self._machines = machines
def summary(self):
for machine, count in self._machines.items():
print(f"[DictWorkers] {machine}: {count}")
class BaseContainer(containers.DeclarativeContainer):
machine = providers.Object("MAC")
count = providers.Object(3)
class WorkersContainer(containers.DeclarativeContainer):
base_container = providers.DependenciesContainer()
list_provider = providers.Singleton(
ListWorkers,
providers.List(providers.List(base_container.machine, base_container.count)),
)
dict_provider = providers.Singleton(
DictWorkers, providers.Dict({base_container.machine: base_container.count})
)
if __name__ == "__main__":
base_container = BaseContainer()
workers_container = WorkersContainer(base_container=base_container)
workers_container.list_provider().summary()
workers_container.dict_provider().summary()
How can I inject object to Dict providers's key?
Metadata
Metadata
Assignees
Labels
No labels