cdgriffith/Box

Feature request: add additional values from file

thmsklngr opened this issue · 3 comments

Hi,
I'm really happy that I found your excellent package, it makes my life a lot easier. One thing I may miss is the possibility to load extra attributes from a file into an existing instance.

Example:
I load a main configuration file and want to add additional configuration values from a plugin configuration file into the same Box instance. I created a much smaller solution myself, where I added a function add_from_file and instead of creating of a new instance I simply updated the existing one with the values coming from the second file.

This may be easier than creating two independent instances and then updating the first one with the values from the second one.
What do you think?

Regards, Thomas

Hey Thomas,

Thanks for the idea, only issue is that I am having a hard time following what you really want. (My brain is scrambled between what you mean between attribute / value / configuration file.)

Could you provide a simplified code example of what you have to do now and how you would like it to behave?

Much appreciated!

Ok, I'll try to explain it a bit better. I hope that I can do that, I'm not English native. :)

First I try initialize a new Box instance by getting - lets say - default values from a configuration file:

>>> config = Box.from_yaml('/path/to/my/default/config.yml')
>>> print(config)
{'key1':'value1','key2':'value2', ...}

Then, further on, I'd like to load additional values from another configuration file, maybe for a plugin. Of course, I could to that by reading the additional values to its own Box instance and update the first one:

>>> config2 = Box.from_yaml('/path/to/my/plugin/config.yml')
>>> print(config2)
{'foo':'bar', 'baz':'gus', ...}
>>> config.update(config2)
>>> print(config)
{'key1': 'value1', 'key2': 'value2', 'foo': 'bar', 'baz': 'gus'}

The main idea behind my feature request now is to skip creating an additional instance and load the additional values directly to the first instance:

>>> config.additional_values_from_yaml('/path/to/my/plugin/config.yml')  # name of function picked randomly :)
>>> print(config)
{'key1': 'value1', 'key2': 'value2', 'foo': 'bar', 'baz': 'gus'}

Configuration files are just an example, I deal a lot of it with them.

I hope it's now clear what I mean. :)

Regards, Thomas

please checkout dynaconf which uses this project for their configuration storage

https://www.dynaconf.com/