Skip to content

Commit d87d4d5

Browse files
committed
adding yaml file parsing for config load with cache and util get method
1 parent 757aaf6 commit d87d4d5

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ansible/example/yml-parse/config.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# YAML config for Laika feature seg notebooks and training environment
2+
# setup default values for developers as working starting point to execute training scripts
3+
# provide human readable and machine diffable explaination of what are a training's parameters
4+
5+
input:
6+
root_media: "/data/GDrive/featureseg"
7+
8+
training:
9+
img_width: 224
10+
img_height: 224
11+
num_points: 24
12+
13+
14+
15+
output:
16+
model_folder: "/data/GDrive/featureseg/models"
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# yaml test script to load and verify our configs
2+
# todo arg parse
3+
import yaml
4+
from functools import reduce
5+
6+
7+
class Config:
8+
def __init__(self, filename="config.yml"):
9+
self.filename = filename
10+
self.config = self.load_config()
11+
12+
def load_config(self, filename=None):
13+
if not filename:
14+
filename = self.filename
15+
with open(filename) as f:
16+
config = yaml.load(f, Loader=yaml.SafeLoader)
17+
return config
18+
19+
def get(self, key, config=None):
20+
if not config:
21+
config = self.config
22+
return reduce(lambda c, k: c[k], key.split('.'), config)
23+
24+
25+
if __name__ == "__main__":
26+
cfg = Config()
27+
config = cfg.load_config("config.yml")
28+
print(config)
29+
print(config['input']['root_media'])
30+
print(cfg.get('input.root_media', config))
31+
print(cfg.get('input.root_media'))
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyyaml

0 commit comments

Comments
 (0)