standard installation
$ pip install git+https://github.com/caseypeat/overwriteable-config
or for tweaking this project
$ git clone https://github.com/caseypeat/overwriteable-config
$ pip install -e ./overwritable-configThis is a barebones overwriteable config project. Most notably, it's function is to import and merge an arbitary number of configurations with overwrite priority given to later configurations. The imported configuration can then be used as a dictionary subclass with dot notation access.
To demonstrate this lets take two configuration files "a.yaml" and "b.yaml"
# a.yaml
network:
width: 256
height: 8
training:
batch_size: 64
num_iters: 100
# b.yaml
network:
width: 128
activation: 'ReLU'
optimizer:
learning_rate: 1e-3And pass them as arguments to a script using this project
$ python example.py ./a.yaml ./b.yaml
# example.py
from config import cfg
print('Network width: ', cfg.network.width)
print('Network height: ',cfg.network.height)
print('Learning rate: ', cfg.optimizer.learning_rate)
print()
print(cfg.as_block())
print()
print(cfg)We get the following output
Network width: 128
Network height: 8
Learning rate: 0.001
network:
width: 128
height: 8
activation: ReLU
training:
batch_size: 64
num_iters: 100
optimizer:
learning_rate: 1e-3
{'network': {'width': 128, 'height': 8, 'activation': 'ReLU'}, 'training': {'batch_size': 64, 'num_iters': 100}, 'optimizer': {'learning_rate': '1e-3'}}
Note that the combined set of atributes from both "a.yaml" and "b.yaml" are present, and the conflicting atribute "network.width" is overwritten by the later "b.yaml" value.
This example can be run from the "./example" directory after installation with the command
$ python example.py ./a.yaml ./b.yaml
or without installation from the root directory with
$ python -m example.example ./example/a.yaml ./example/b.yaml
- This project does not currently have extensive testing of edge cases or protections against overwritting critial attributes. So if one is looking for a way to break this, they need not look very hard...
- importing "config" will use all command line arguments passed, so this project is currently incompatible with using command line arguments for anything else.
- Scientific notation will be detected and converted to float when accessed via dot notation, but not when accessed using the standard dictionary square brackets.
>>> cfg.optimizer.learning_rate
0.001
>>> cfg['optimizer']['learning_rate']
'1e-3'