-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
import_yaml_test.py
143 lines (122 loc) · 3.56 KB
/
import_yaml_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
Gabriel Staples
14 Apr. 2020
Do some yaml file importing to learn how yaml configuration files work.
Run command:
python3 import_yaml_test.py
- OR (if in the root dir of this repo):
python3 python/yaml_import/import_yaml_test.py
References:
1. https://rollout.io/blog/yaml-tutorial-everything-you-need-get-started/
2. PyYAML documentation (scroll down to near the very bottom to see the full reference
documentation for all functions): https://pyyaml.org/wiki/PyYAMLDocumentation
"""
import os
import yaml
if __name__ == '__main__':
# Obtain the path to the directory this file is contained in
# See: https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory/5137509#5137509
dir_path = os.path.dirname(os.path.realpath(__file__))
print("Directory this Python file is in = \"{}\"\n".format(dir_path))
print("====================\n"
"1ST YAML FILE IMPORT\n"
"====================\n")
# Open 1st yaml config file
stream = open("{}/my_config1.yaml".format(dir_path), 'r')
dictionary = yaml.load(stream)
# print manually
for key, value in dictionary.items():
print(key + " : " + str(value))
# print automagically
print()
print(yaml.dump(dictionary))
print("====================\n"
"2ND YAML FILE IMPORT\n"
"====================\n")
# Open 2nd yaml config file & update the dictionary
stream = open("{}/my_config2.yaml".format(dir_path), 'r')
dictionary.update(yaml.load(stream))
# print manually
for key, value in dictionary.items():
print(key + " : " + str(value))
# print automagically
print()
print(yaml.dump(dictionary))
"""
SAMPLE OUTPUT:
$ python3 python/yaml_import/import_yaml_test.py
Directory this Python file is in = "/home/gabriel/GS/dev/eRCaGuy_hello_world/python/yaml_import"
====================
1ST YAML FILE IMPORT
====================
doe : a deer, a female deer
ray : a drop of golden sun
pi : 3.14159
xmas1 : True
xmas2 : True
xmas3 : True
xmas4 : True
xmas5 : True
xmas6 : True
xmas7 : True
xmas8 : tRue
french-hens : 3
calling-birds : ['huey', 'dewey', 'louie', 'fred']
xmas-fifth-day : {'calling-birds': 'four', 'french-hens': 3, 'golden-rings': 5, 'partridges': {'count': 1, 'location': 'a pear tree'}, 'turtle-doves': 'two'}
another-list : ['hey', 'there', 'how', 'are', 'you doing', 'today?']
another-list: [hey, there, how, are, you doing, 'today?']
calling-birds: [huey, dewey, louie, fred]
doe: a deer, a female deer
french-hens: 3
pi: 3.14159
ray: a drop of golden sun
xmas-fifth-day:
calling-birds: four
french-hens: 3
golden-rings: 5
partridges: {count: 1, location: a pear tree}
turtle-doves: two
xmas1: true
xmas2: true
xmas3: true
xmas4: true
xmas5: true
xmas6: true
xmas7: true
xmas8: tRue
====================
2ND YAML FILE IMPORT
====================
doe : a deer, a female deer
ray : hello there
pi : [23.7, 87.12]
xmas1 : True
xmas2 : True
xmas3 : True
xmas4 : True
xmas5 : True
xmas6 : True
xmas7 : True
xmas8 : tRue
french-hens : 3
calling-birds : ['huey', 'dewey', 'louie', 'fred']
xmas-fifth-day : {'partridges': {'color': 'green'}}
another-list : ['it is nice', 'to meet you.']
another-list: [it is nice, to meet you.]
calling-birds: [huey, dewey, louie, fred]
doe: a deer, a female deer
french-hens: 3
pi: [23.7, 87.12]
ray: hello there
xmas-fifth-day:
partridges: {color: green}
xmas1: true
xmas2: true
xmas3: true
xmas4: true
xmas5: true
xmas6: true
xmas7: true
xmas8: tRue
"""