Parse a ".yml" file, set default values if needs to, and check given
values.
Attributes
self.__key : list
A list of key to access parameters.
self.__value : list
Value linked to parameters.
self.__category : dict
A dictionary used to print parameters names and values link to a
category name used as a key.
self.__yaml : dict
The parsed YAML file.
self.__clean_yaml : dict
A deep copy of the parsed YAML file, which deletion are applied on.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 | class ParseYaml:
"""Parse a ".yml" file, set default values if needs to, and check given
values.
Attributes
---------
self.__key : `list`
A list of key to access parameters.
self.__value : `list`
Value linked to parameters.
self.__category : `dict`
A dictionary used to print parameters names and values link to a
category name used as a key.
self.__yaml : `dict`
The parsed YAML file.
self.__clean_yaml : `dict`
A deep copy of the parsed YAML file, which deletion are applied on.
"""
def __init__(self, file_path: str, verbose: bool = False):
"""Initialize attributes contained parsed ".yaml" file values.
Parameters
----------
file_path : `str`
The path to the file to be parsed.
verbose : `bool`, optional
Use a verbose mode indicating when default values are used, by
default `False` so do not print anything else.
"""
# Initialize attributes.
self.__key: list = []
self.__value: list = []
self.__category: dict = {}
self.__verbose: bool = verbose
self.__yaml: dict = {}
if file_path is not None:
# Parsing user file.
with open(file_path, "r", encoding="utf-8") as file:
self.__yaml: dict = safe_load(file)
if self.__verbose:
if self.__yaml == {}:
print("> No parameter file given, switch to default values.")
else:
print(f'> "{file_path}" parameter file is parsed successfully.')
self.__clean_yaml: dict = deepcopy(self.__yaml)
# pylint: disable=too-many-branches
# Here, quite difficult to reduce branches, due to looking in nested
# dictionaries.
def __setitem__(self, key: str, parameter: SetParameter):
"""Add a new key/parameter value pair.
Parameters
----------
key : `str`
The key to assign a parameter.
parameter : `SetParameter`
The parameter value, which is a object.
Raises
------
KeyError
Throw when the use try to assign a parameter value to an already
existing key.
ValueError
Throw this error when the default path given to the parameter value
is incomplete.
"""
# Not allowing key overwriting.
if key in self.__key:
raise KeyError(
"[Err##] Cannot overwrite existing key/parameter pair."
)
yaml: dict = self.__yaml
set_key: list = parameter.path().split(":")
value_set: bool = False
param_key = set_key[-1]
# Check the "path" of a value.
for key_i in set_key:
if key_i not in yaml:
continue
yaml = yaml[key_i]
if key_i != param_key:
continue
parameter.value(yaml)
value_set = True
is_pop = True
# Deleting validated parameters in the YAML file.
while set_key != [] and value_set and is_pop:
clean_yaml: dict = self.__clean_yaml
is_pop = None
# Parse the YAML path parameter.
for key_i in set_key:
# Last value detected.
if key_i == param_key:
# Delete a value in the `clean_yaml` and pop a value in the
# YAML path.
del clean_yaml[key_i]
set_key.remove(key_i)
is_pop = True
break
# Empty path detected.
if clean_yaml[key_i] == {}:
# Delete a value in the `clean_yaml` and pop a value in the
# YAML path.
del clean_yaml[key_i]
set_key.remove(key_i)
is_pop = True
break
clean_yaml = clean_yaml[key_i]
if self.__verbose:
if value_set:
print(f'> Value set for "{parameter.path()}".')
else:
print(
f'> Path "{parameter.path()}" not found in the '
"parameter file, keeping the default value."
)
if isinstance(parameter.value(), dict):
raise ValueError(
f'[Err##] The path "{parameter.path()}" is '
"incomplete, error."
)
parameter.check_error()
self.__key += [key]
self.__value += [parameter.value()]
if parameter.category() in self.__category:
self.__category[parameter.category()] += [
(parameter.path(), parameter.value())
]
else:
self.__category[parameter.category()] = [
(parameter.path(), parameter.value())
]
# pylint: enable=too-many-branches
def __getitem__(self, key: str) -> SetParameter:
"""Return a parameter value corresponding to a given key.
Parameters
----------
key : `str`
The key to fetch a parameter.
Returns
-------
`SetParameter`
The fetched parameter.
"""
if key not in self.__key:
raise KeyError(f"[Err##] The key {key} does not exist.")
return self.__value[self.__key.index(key)]
def keys(self) -> list:
"""Return keys linked to this object.
Returns
-------
`list`
The keys.
"""
return self.__key
def values(self) -> list:
"""Return parameters values linked to this object.
Returns
-------
`list`
The values.
"""
return self.__value
def items(self) -> zip:
"""Return keys, paired to their parameters values, linked to this
object.
Returns
-------
`zip`
The pairs key/value.
"""
return zip(self.__key, self.__value)
def clean(self):
"""Delete the parsed ".yaml" file.
Raises
------
`ValueError`
Throw an exception if a value in the YAML file have not been added.
"""
del self.__yaml, self.__verbose
if self.__clean_yaml != {}:
raise ValueError(
"[Err##] The YAML have been parsed, but there "
"are unassigned values. Please check the input "
"files and its indentation. The remaining values "
f"are:\n{dump(self.__clean_yaml)}"
)
def __str__(self) -> str:
"""Redefine the `print()` behaviour of this class.
Returns
-------
`str`
The message to print.
"""
to_print: str = ""
for key, pair in self.__category.items():
to_print += f"\033[7m {key.upper()} \033[0m\n"
for path, value in pair:
to_print += f"- {path} = {value}\n"
return to_print[:-1]
|
__getitem__(key)
Return a parameter value corresponding to a given key.
Parameters
key : str
The key to fetch a parameter.
Returns
SetParameter
The fetched parameter.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 | def __getitem__(self, key: str) -> SetParameter:
"""Return a parameter value corresponding to a given key.
Parameters
----------
key : `str`
The key to fetch a parameter.
Returns
-------
`SetParameter`
The fetched parameter.
"""
if key not in self.__key:
raise KeyError(f"[Err##] The key {key} does not exist.")
return self.__value[self.__key.index(key)]
|
__init__(file_path, verbose=False)
Initialize attributes contained parsed ".yaml" file values.
Parameters
file_path : str
The path to the file to be parsed.
bool, optional
Use a verbose mode indicating when default values are used, by
default False so do not print anything else.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
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 | def __init__(self, file_path: str, verbose: bool = False):
"""Initialize attributes contained parsed ".yaml" file values.
Parameters
----------
file_path : `str`
The path to the file to be parsed.
verbose : `bool`, optional
Use a verbose mode indicating when default values are used, by
default `False` so do not print anything else.
"""
# Initialize attributes.
self.__key: list = []
self.__value: list = []
self.__category: dict = {}
self.__verbose: bool = verbose
self.__yaml: dict = {}
if file_path is not None:
# Parsing user file.
with open(file_path, "r", encoding="utf-8") as file:
self.__yaml: dict = safe_load(file)
if self.__verbose:
if self.__yaml == {}:
print("> No parameter file given, switch to default values.")
else:
print(f'> "{file_path}" parameter file is parsed successfully.')
self.__clean_yaml: dict = deepcopy(self.__yaml)
|
__setitem__(key, parameter)
Add a new key/parameter value pair.
Parameters
key : str
The key to assign a parameter.
SetParameter
The parameter value, which is a object.
Raises
KeyError
Throw when the use try to assign a parameter value to an already
existing key.
ValueError
Throw this error when the default path given to the parameter value
is incomplete.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 | def __setitem__(self, key: str, parameter: SetParameter):
"""Add a new key/parameter value pair.
Parameters
----------
key : `str`
The key to assign a parameter.
parameter : `SetParameter`
The parameter value, which is a object.
Raises
------
KeyError
Throw when the use try to assign a parameter value to an already
existing key.
ValueError
Throw this error when the default path given to the parameter value
is incomplete.
"""
# Not allowing key overwriting.
if key in self.__key:
raise KeyError(
"[Err##] Cannot overwrite existing key/parameter pair."
)
yaml: dict = self.__yaml
set_key: list = parameter.path().split(":")
value_set: bool = False
param_key = set_key[-1]
# Check the "path" of a value.
for key_i in set_key:
if key_i not in yaml:
continue
yaml = yaml[key_i]
if key_i != param_key:
continue
parameter.value(yaml)
value_set = True
is_pop = True
# Deleting validated parameters in the YAML file.
while set_key != [] and value_set and is_pop:
clean_yaml: dict = self.__clean_yaml
is_pop = None
# Parse the YAML path parameter.
for key_i in set_key:
# Last value detected.
if key_i == param_key:
# Delete a value in the `clean_yaml` and pop a value in the
# YAML path.
del clean_yaml[key_i]
set_key.remove(key_i)
is_pop = True
break
# Empty path detected.
if clean_yaml[key_i] == {}:
# Delete a value in the `clean_yaml` and pop a value in the
# YAML path.
del clean_yaml[key_i]
set_key.remove(key_i)
is_pop = True
break
clean_yaml = clean_yaml[key_i]
if self.__verbose:
if value_set:
print(f'> Value set for "{parameter.path()}".')
else:
print(
f'> Path "{parameter.path()}" not found in the '
"parameter file, keeping the default value."
)
if isinstance(parameter.value(), dict):
raise ValueError(
f'[Err##] The path "{parameter.path()}" is '
"incomplete, error."
)
parameter.check_error()
self.__key += [key]
self.__value += [parameter.value()]
if parameter.category() in self.__category:
self.__category[parameter.category()] += [
(parameter.path(), parameter.value())
]
else:
self.__category[parameter.category()] = [
(parameter.path(), parameter.value())
]
|
__str__()
Redefine the print() behaviour of this class.
Returns
str
The message to print.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 | def __str__(self) -> str:
"""Redefine the `print()` behaviour of this class.
Returns
-------
`str`
The message to print.
"""
to_print: str = ""
for key, pair in self.__category.items():
to_print += f"\033[7m {key.upper()} \033[0m\n"
for path, value in pair:
to_print += f"- {path} = {value}\n"
return to_print[:-1]
|
clean()
Delete the parsed ".yaml" file.
Raises
ValueError
Throw an exception if a value in the YAML file have not been added.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 | def clean(self):
"""Delete the parsed ".yaml" file.
Raises
------
`ValueError`
Throw an exception if a value in the YAML file have not been added.
"""
del self.__yaml, self.__verbose
if self.__clean_yaml != {}:
raise ValueError(
"[Err##] The YAML have been parsed, but there "
"are unassigned values. Please check the input "
"files and its indentation. The remaining values "
f"are:\n{dump(self.__clean_yaml)}"
)
|
items()
Return keys, paired to their parameters values, linked to this
object.
Returns
zip
The pairs key/value.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
224
225
226
227
228
229
230
231
232
233 | def items(self) -> zip:
"""Return keys, paired to their parameters values, linked to this
object.
Returns
-------
`zip`
The pairs key/value.
"""
return zip(self.__key, self.__value)
|
keys()
Return keys linked to this object.
Returns
list
The keys.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
204
205
206
207
208
209
210
211
212 | def keys(self) -> list:
"""Return keys linked to this object.
Returns
-------
`list`
The keys.
"""
return self.__key
|
values()
Return parameters values linked to this object.
Returns
list
The values.
Source code in src/smiffer/parse_yaml/class_parse_yaml.py
214
215
216
217
218
219
220
221
222 | def values(self) -> list:
"""Return parameters values linked to this object.
Returns
-------
`list`
The values.
"""
return self.__value
|