From 820289a962ee6ed18c928743b8733054af2c6188 Mon Sep 17 00:00:00 2001 From: Alberto Date: Thu, 29 Sep 2022 19:16:48 +0200 Subject: [PATCH] Created simulation data parser --- Utilities/Files/__init__.py | 0 Utilities/Files/simulation_data_parser.py | 52 +++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Utilities/Files/__init__.py create mode 100644 Utilities/Files/simulation_data_parser.py diff --git a/Utilities/Files/__init__.py b/Utilities/Files/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Utilities/Files/simulation_data_parser.py b/Utilities/Files/simulation_data_parser.py new file mode 100644 index 0000000..e26c53d --- /dev/null +++ b/Utilities/Files/simulation_data_parser.py @@ -0,0 +1,52 @@ +# Python Imports +import os.path + +# Project Imports +from Utilities.Files.SimulationDataTypes.JsonDataSimulationHandler import JsonDataSimulationHandler +from Utilities.Files.SimulationDataTypes.CsvDataSimulationHandler import CsvDataSimulationHandler +from Utilities.Files.SimulationDataTypes.ParquetSimulationDataHandler import ParquetDataSimulationHandler + +handlers = {'.csv': CsvDataSimulationHandler, + '.json': JsonDataSimulationHandler, + 'parquet': ParquetDataSimulationHandler + } + + +class SimulationDataParser: + + def __init__(self, file_path): + self._file_path = file_path + self._file_extension = None + + def read_content(self): + # Check file exists + self._check_file() + # Check extension + self._check_extension() + # Get correct handler + handler = self._get_data_handler() + # Load content + handler.load_data() + # Convert it to dataframe + dataframe = handler.convert_into_dataframe() + # Return + + def _check_file(self): + exists = os.path.exists(self._file_path) + + if not exists: + print(f"File {self._file_path} does not exists.") + exit(1) + + def _check_extension(self): + return os.path.splitext(self._file_path)[1] + + def _get_data_handler(self): + handler = handlers.get(self._file_extension) + + return handler() + + + + +