Skip to content
Snippets Groups Projects
Commit d9f6ec8e authored by Sebastian Lobinger's avatar Sebastian Lobinger
Browse files

add basic structure, write test and implement class, fix issue #2

parent 4945f4e8
No related branches found
No related tags found
1 merge request!1add basic structure, write test and implement class, fix issue #2
# YamlConfig
Pyhton Konfigurationsklasse für YAML Dateien.
# ConfigProvider
Pyhton Konfigurationsklasse.
## Einbindung in andere Projekte
......@@ -7,24 +7,33 @@ Dieses Projekt ist selbst als python package Strukturiert und kann daher einfach
als git submodule direkt in andere Projekte eingebunden werden.
```bash
git submodule add git@git.imp.fu-berlin.de:coding-at-fu/yaml_config.git dict_filter
git submodule add git@git.imp.fu-berlin.de:coding-at-fu/yaml_config.git config_provider
```
## Verwendung
Wurde das Projekt wie oben stehend eingebunden kann das Modul wie folgt verwendet werden:
```python
from yaml_config import yaml_config
import config_provider
import yaml
config = yaml_config.YamlConfig(path = '/path/to/conf/file.yml')
def load_config_from_yaml_file(path):
with open(path) as f:
yaml_string = f.read()
return_dict = yaml.load(yaml_string)
return return_dict
config = config_provider.ConfigProvider(
load_function = load_config_from_yaml_file,
path = '/path/to.yml')
def do_whatever():
config = yaml_config.YamlConfig.getInstance()
print (config.toDict().get('config_key'))
config = config_provider.ConfigProvider.getInstance()
print (config.dict().get('config_key'))
# wird foo ausgeben wenn config_key den Wert foo hat.
def reconfigure():
config = yaml_config.YamlConfig.getInstance()
config = config_provider.ConfigProvider.getInstance()
config.reload()
# Die Konfiguration wurde neu eingelesen.
# Die Konfiguration wurde neu eingelesen.
```
class ConfigProvider:
def __init__(self, load_function, *args, **kwargs):
self._conf_dict = load_function(*args, **kwargs)
@property
def dict(self):
return self._conf_dict
\ No newline at end of file
import unittest
import config_provider
class TestConfigProvider(unittest.TestCase):
def test_init(self):
config = config_provider.ConfigProvider(
load_function = dict, foo = 'bar')
self.assertEqual({'foo': 'bar'}, config.dict)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment