Skip to content

Commit 6a0a34b

Browse files
authored
(A002) OMParser (#412)
* [OMParser] cleanup usage of Dict * [OMParser] remove import sys * [OMParser] basic pylint fixes * [OMParser] optimise code in make_values() * [OMParser] remove unused variables
1 parent 93b7bac commit 6a0a34b

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

OMPython/OMParser.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@
3232
Version: 1.0
3333
"""
3434

35-
import sys
36-
from typing import Dict, Any
35+
from typing import Any
3736

38-
result: Dict[str, Any] = dict()
37+
result: dict[str, Any] = {}
3938

40-
inner_sets = []
41-
next_set_list = []
4239
next_set = []
4340
next_set.append('')
4441

@@ -47,10 +44,9 @@ def bool_from_string(string):
4744
"""Attempt conversion of string to a boolean """
4845
if string in {'true', 'True', 'TRUE'}:
4946
return True
50-
elif string in {'false', 'False', 'FALSE'}:
47+
if string in {'false', 'False', 'FALSE'}:
5148
return False
52-
else:
53-
raise ValueError
49+
raise ValueError
5450

5551

5652
def typeCheck(string):
@@ -67,9 +63,7 @@ def typeCheck(string):
6763
return t(string)
6864
except ValueError:
6965
continue
70-
else:
71-
print("String contains un-handled datatype")
72-
return string
66+
raise ValueError(f"String contains un-handled datatype: {repr(string)}!")
7367

7468

7569
def make_values(strings, name):
@@ -161,14 +155,9 @@ def make_values(strings, name):
161155
varValue = (varValue.replace('{', '').strip()).replace('}', '').strip()
162156
multiple_values = varValue.split(",")
163157

164-
for n in range(len(multiple_values)):
165-
each_v = multiple_values[n]
166-
multiple_values.pop(n)
167-
each_v = typeCheck(each_v)
168-
multiple_values.append(each_v)
169-
170158
if len(multiple_values) != 0:
171-
result[main_set_name]['Elements'][name]['Properties']['Results'][varName] = multiple_values
159+
multiple_values_type_checked = [typeCheck(val) for val in multiple_values]
160+
result[main_set_name]['Elements'][name]['Properties']['Results'][varName] = multiple_values_type_checked
172161
elif varName != "" and varValue != "":
173162
result[main_set_name]['Elements'][name]['Properties']['Results'][varName] = varValue
174163
else:
@@ -187,12 +176,12 @@ def delete_elements(strings):
187176
char = strings[pos]
188177
if char == "":
189178
break
190-
elif char == ",":
179+
if char == ",":
191180
break
192-
elif char == " ":
181+
if char == " ":
193182
pos = pos + 1
194183
break
195-
elif char == "{":
184+
if char == "{":
196185
break
197186
pos = pos - 1
198187
delStr = strings[pos: strings.rfind(")")]
@@ -566,8 +555,8 @@ def skip_all_inner_sets(position):
566555
break
567556
pos += 1
568557
if count != 0:
569-
print("\nParser Error: Are you missing one or more '}'s? \n")
570-
sys.exit(1)
558+
raise ValueError("Parser Error: Are you missing one or more '}}'s in string? "
559+
f"(string value: {repr(string)}")
571560

572561
if max_count >= 2:
573562
while position < end_of_main_set:
@@ -683,15 +672,14 @@ def skip_all_inner_sets(position):
683672
position += 1
684673
else:
685674
next_set[0] = ""
686-
return (len(string) - 1)
675+
return len(string) - 1
687676

688677
max_of_sets = max(last_set, last_subset)
689678
max_of_main_set = max(max_of_sets, last_subset)
690679

691680
if max_of_main_set != 0:
692681
return max_of_main_set
693-
else:
694-
return (len(string) - 1)
682+
return len(string) - 1
695683

696684
# Main entry of get_the_string()
697685
index = 0
@@ -745,8 +733,7 @@ def skip_all_inner_sets(position):
745733
else:
746734
return current_set, next_set[0]
747735
else:
748-
print("\nThe following String has no {}s to proceed\n")
749-
print(string)
736+
raise ValueError(f"The following String has no {{}}s to proceed: {repr(string)}!")
750737

751738
# End of get_the_string()
752739

@@ -835,15 +822,15 @@ def check_for_values(string):
835822
if "record SimulationResult" in string:
836823
formatSimRes(string)
837824
return result
838-
elif "record " in string:
825+
if "record " in string:
839826
formatRecords(string)
840827
return result
841828

842829
string = typeCheck(string)
843830

844831
if not isinstance(string, str):
845832
return string
846-
elif string.find("{") == -1:
833+
if string.find("{") == -1:
847834
return string
848835

849836
current_set, next_set = get_the_set(string)

0 commit comments

Comments
 (0)