finish the parsing need to be test

This commit is contained in:
Maoake Teriierooiterai
2026-03-19 16:00:19 +01:00
parent 6b90e5fce5
commit a4d55d5692
3 changed files with 77 additions and 6 deletions
+1 -1
View File
@@ -3,4 +3,4 @@ HEIGHT=100
ENTRY=0,0 ENTRY=0,0
EXIT=19,14 EXIT=19,14
OUTPUT_FILE=maze.txt OUTPUT_FILE=maze.txt
PERFECT=True PERFECT=Truee
+71 -4
View File
@@ -1,6 +1,4 @@
class DataMaze: class DataMaze:
def __init__(self) -> None:
pass
@staticmethod @staticmethod
def get_data(name_file: str) -> str: def get_data(name_file: str) -> str:
@@ -14,5 +12,74 @@ class DataMaze:
return data return data
@staticmethod @staticmethod
def transform_data(data: str): def transform_data(data: str) -> dict:
pass tmp = data.split("\n")
tmp2 = [
value.split("=", 1) for value in tmp
]
data_t = {
value[0]: value[1] for value in tmp2
}
return data_t
@staticmethod
def verif_key_data(data: dict) -> dict:
key_test = {
"WIDTH", "HEIGHT", "ENTRY", "EXIT", "OUTPUT_FILE", "PERFECT"
}
set_key = {
key for key in data.keys()
}
try:
res_key = {}
if len(set_key) != len(key_test):
raise Exception("Missing some data the len do not correspond")
res_key = {key for key in set_key if key not in key_test}
if len(res_key) != 0:
raise Exception("Some Key "
f"do not correspond the keys: {res_key}")
return data
except Exception as e:
print(f"{e}")
exit()
@staticmethod
def verif_value_data(data: dict):
key_int = {"WIDTH", "HEIGHT"}
key_tuple = {"ENTRY", "EXIT"}
key_bool = {"PERFECT"}
try:
res: dict = {}
for key in key_int:
res.update({key: int(data[key])})
for key in key_tuple:
res.update({key: DataMaze.convert_tuple(data[key])})
for key in key_bool:
res.update({key: DataMaze.convert_bool(data[key])})
return res
except ValueError as e:
print("Error on the method verif_value_data"
f" in the class DataMaze: {e}")
@staticmethod
def convert_tuple(data: str) -> tuple:
try:
data_t = data.split(",")
x, y = data_t
tup = (int(x), int(y))
return tup
except ValueError as e:
print(f"On the convert Tuple: {e}")
exit()
@staticmethod
def convert_bool(data: str) -> bool:
try:
if data != "True" and data != "False":
raise ValueError("This is not True or False")
if data == "True":
return True
return False
except ValueError as e:
print(f"Error on the convert_bool in class DataMaze: {e}")
exit()
+5 -1
View File
@@ -3,7 +3,11 @@ from parsing.Parsing import DataMaze
def main() -> None: def main() -> None:
print("Unit Testing for parsing:") print("Unit Testing for parsing:")
DataMaze.get_data("config.txt") data = DataMaze.get_data("config.txt")
data_t = DataMaze.transform_data(data)
data_t = DataMaze.verif_key_data(data_t)
data_t = DataMaze.verif_value_data(data_t)
print(data_t)
if __name__ == "__main__": if __name__ == "__main__":