Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dict provider from configuration file with additional items #775

mighelone started this conversation in General
Discussion options

I wonder if it is possible to read a generic dictionary from a config file (i.e. yaml), and then add new items to the existing dictionary.

For example, I have the following yaml file:

conf:
 a: 1
 b: 2

In the container:

@dataclass
class MyClass
 conf: dict[str, any]
class Container(containers.DeclarativeContainer):
 config = providers.Configuration()
 
 my_class_factory = providers.Factory(MyClass, config.conf)
container = Container()
container.config.from_yaml("config.yaml")
obj = container.my_class_factory()

This work, creating an object with the conf dictionary defined in the yaml file.
However, I want to add other items to the conf dictionary not defined in the file.
I only manage to do in this way:

@dataclass
class MyClass
 conf: dict[str, any]
class Container(containers.DeclarativeContainer):
 config = providers.Configuration()
 
 my_class_factory = providers.Factory(MyClass)
container = Container()
container.config.from_yaml("config.yaml")
obj = container.my_class_factory(conf={**container.config["conf"], "c": 67})

Is there a better way of doing it?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /