Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about views #62

Open
agorina opened this issue Oct 1, 2019 · 4 comments
Open

Question about views #62

agorina opened this issue Oct 1, 2019 · 4 comments

Comments

@agorina
Copy link

agorina commented Oct 1, 2019

I am not sure I fully understand the concept of views and combining them. Here is my use case:

I am creating a config using this code and yaml file:

config = LazyConfig('qti-scoring-engine', "app")

Here is an example of the config in the yaml:

uvicorn:
  host: 127.0.0.1
  port: 8080
  workers: 1

Then augmenting it with the arguments:

 config.set_args(args, dots=True)

the arguments I am passing are

parser.add_argument('--uvicorn.host', help='a host', required=False)
parser.add_argument('--uvicorn.port', help='a port', type=int, required=False)

When I then look up the configuration using the below line it returns only the entries from the second view created by the config.set_args:

config['uvicorn'].get()

How could I get all the values returned by merging both views?

When I examine the config in the debugger it has two views, and config.flatten() returns the properly merged configuration.

Any help is very much appreciated.

@sampsyo
Copy link
Member

sampsyo commented Oct 1, 2019

Good question! Views get combined when you ask for a specific point in the "key space." That is, when you ask for config['uvicorn']['host'].get(), it looks for the topmost match in the stack of values at uvicorn.host. But if you ask for config['uvicorn'], it again dutifully walks the stack of data sources and returns the first one that has a value for uvicorn—namely, the dict containing the overrides.

Would it work to use the individual values directly—i.e., to use config['uvicorn']['host'].get() and config['uvicorn']['port'].get(), etc., instead of trying to pull out a dict all at once? This way, you can also do validation on the individual values—for example, config['uvicorn']['port'].get(int).

@agorina
Copy link
Author

agorina commented Oct 1, 2019

Thank you for the quick response!

Unfortunately getting each setting individually is not going to work for what I am trying to achieve. I want to pass the whole set of options to uvicorn and logging_config.dictConfig. A work around I see for now is to call lazy_cfg.flatten() that seems to returned a fully merged map of configurations. My configuration is not large. Do you see any problems with this approach?

@sampsyo
Copy link
Member

sampsyo commented Oct 1, 2019

Nope, no problems at all! That (flatten) does seem like the right thing if you need to get the dict out at that point.

@agorina
Copy link
Author

agorina commented Oct 1, 2019

Thank you!

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

No branches or pull requests

2 participants