Code owners
Assign users and groups as approvers for specific file changes. Learn more.
cost_estimation.py 4.47 KiB
"""Calculation module main file."""
# Import standard modules.
import logging
import traceback
from sys import argv, exit
# Import own modules.
from runmodule import run_module
from src.datapreprocessing import data_preprocessing
from src.datapostprocessing import data_postprocessing
def main():
"""Execute the main program for cost estimation.
This function serves as the main entry point for performing the cost estimation.
It goes through the following key steps:
(1) Preprocessing - Acquire necessary data and paths: Call the 'data_preprocessing' function from
'datapreprocessing.py' to set up data and routing information.
(2) Run (main processing) - Execute code depending on method layers: Execute the 'run_module' function from the
'methodexecutionpackage' library. The 'run_module' function is responsible for the programs primary logic.
(3) Postprocessing - Write data to the aircraft exchange file and generate plots and reports: Call the
'data_postprocessing' function from 'datapostprocessing.py' to handle postprocessing tasks. This step receives
data from both the preprocessing and the main processing step.
Note: The 'routing_dict' dictionary is used to manage the routing and execution of different program components.
:raises Exception: Raised to handle other exceptions
:return: None
"""
# Initialize exception string and runtime output logger.
tool_name = 'cost estimation'
tool_version = '0.5.0'
runtime_output = logging.getLogger('module_logger')
try:
"""Preprocessing: Acquire necessary data and paths."""
# Run 'data_preprocessing' function from 'datapreprocessing.py'.
paths_and_names, routing_dict, runtime_output = data_preprocessing('cost_estimation_conf.xml', tool_version, argv)
runtime_output.print('Cost estimation started...')
"""Run: Execute code depending on method layers."""
# Execute 'run_module' function from 'methodexecutionpackage' library. This function is responsible for the main
# logic of the program.
run_output_dict = run_module(paths_and_names, routing_dict, runtime_output)
"""Postprocessing: Write data to aircraft exchange file and generate plots and reports."""
# Run 'data_postprocessing' function from 'datapostprocessing.py' to handle postprocessing tasks. Receives data
# from preprocessing and main processing step.
data_postprocessing(paths_and_names, routing_dict, run_output_dict, runtime_output)
runtime_output.print('Operating cost estimation finished.')
except Exception as e: # pylint: disable=broad-exception-caught
# Handle other exceptions.
runtime_output.critical(exception_string_msg(e, tool_name))
exit(1)
def exception_string_msg(error, tool_name: str):
"""Generate exception message.
Generate a formatted string detailing the type and location of an exception, along with an error message, for
diagnostic purposes. This function is particularly useful for logging or displaying comprehensive error information
when an exception occurs in a specific module or function.
:param exception error: Caught exception object from which details will be extracted
:param str tool_name: Name of the tool or module where the error occurred, used in the final error message
:return str: String including error type, file name, function/method name, line number, code that caused the error,
and error message.
"""
error_type = str(type(error).__name__)
error_trace = traceback.extract_tb(error.__traceback__)
error_file, error_line, error_func, error_code = error_trace[-1]
error_file = error_file.split('/')[-1]
exception_string = f"{error_type}: \n"
exception_string += f" - File : {error_file} \n"
exception_string += f" - Function / Method: {error_func} \n"
exception_string += f" - Line : {error_line} \n"
exception_string += f" - Code : {error_code} \n"
exception_string += f" - Error message : {str(error)} \n"
return exception_string + f"Main execution of {tool_name} module failed! \n" \
f"Program aborted."
if __name__ == "__main__":
main()