lincolntools : Python toolbox made in Lincoln France
- Author: Lincoln Innovation
- Creation date: 2020-10-01
- see AUTHORS.md
This package belongs to the lincolntools
namespace
The goal of this package is to provide the necessary tools to create a unique and global configuration object that can be queried anywhere in a Python project.
pip install lincolntools-config
There are several possibilities to load a YAML configuration file :
- Provide the path to a file which will be processed and transformed into a Python
dict
- Provide the path to a folder that contains one or more YAML files that will be concatenated into a single object.
In the following examples we suppose our config folder has the given structure :
└── config
├── config_foo
├── foo.yaml
└── foo.template
└── config_bar
├── bar.yaml
└── bar.template
foo.yaml
foo:
foo_key : foo_value
bar.yaml
bar:
bar_key : bar_value
Code :
from lincolntools.config import Config
my_config = Config('/path/to/config')
print(my_config.conf)
# {'foo': {'foo_key': 'foo_value'}, 'bar': {'bar_key': 'bar_value'}, '_version': 1}
print(my_config.get('foo'))
# {'foo_key': 'foo_value'}
print(my_config.get('foo').get('foo_key'))
# foo_value
print(my_config.get('foo')['foo_key']) # same as above but with another syntax
# foo_value
print(my_config.flatten())
# {'foo-foo_key': 'foo_value', 'bar-bar_key': 'bar_value'}
print(my_config.dump())
# _version: 2
# bar:
# bar_key: bar_value
# foo:
# foo_key: foo_value
The Config
class is based on the Single design pattern (official documentation).
TLDR : Only one instance of Config
can be initialized during the whole program lifetime.
from lincolntools.config import Config
my_first_config = Config('/path/to/config') # ok, no other instance exists
my_other_config = Config('/path/to/config') # nok
# Exception: This is a singleton
Launch tests with the default Python version :
# in the project folder
pytest
Launch tests on multiple Python versions (see tox.ini
) :
# in the project folder
tox
Voir HISTORY.md
This package was inspired by Cookiecutter_ and the audreyr/cookiecutter-pypackage
_ project template.
- _Cookiecutter: https://github.com/audreyr/cookiecutter
- _
audreyr/cookiecutter-pypackage
: https://github.com/audreyr/cookiecutter-pypackage