From 548500ffd09fd694f8f18c8559e6ffca521f2c92 Mon Sep 17 00:00:00 2001 From: Dawid Makar Date: Tue, 7 Jan 2025 13:21:34 +0100 Subject: [PATCH] PENGSOL-483: Format float numbers. --- .../script_components/tab_processors/basic.py | 60 ++++++++++++------- .../tab_processors/specified.py | 1 - 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/scripts/excel_dwh_filler/script_components/tab_processors/basic.py b/scripts/excel_dwh_filler/script_components/tab_processors/basic.py index 40243ae..47ca340 100644 --- a/scripts/excel_dwh_filler/script_components/tab_processors/basic.py +++ b/scripts/excel_dwh_filler/script_components/tab_processors/basic.py @@ -4,6 +4,7 @@ import logging from typing import Any +from openpyxl.styles import numbers from openpyxl.worksheet import worksheet from script_components import helpers @@ -13,6 +14,12 @@ class BaseExcelTabProcessor(abc.ABC): DATE_FORMAT = "%Y-%m-%d" DATE_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" + PYTHON_TYPE_TO_EXCEL_TYPE = { + "str": numbers.FORMAT_TEXT, + "int": numbers.FORMAT_NUMBER, + "float": numbers.FORMAT_NUMBER_COMMA_SEPARATED1, + } + def __init__(self, data: list[Any], tab: worksheet.Worksheet): self.data = data self.tab = tab @@ -25,25 +32,48 @@ def run_tab_processing(self) -> None: def clear_data_from_tab(self) -> None: raise NotImplementedError - def _format_not_convertible_to_string_values(self, value): + def _determine_data_type_and_value(self, value) -> dict: + data_type = None + match value: case bool(): value = int(value) + data_type = "int" case float(): if value.is_integer(): value = int(value) + data_type = "int" else: value = helpers.as_text(value) - value = self._change_separator_from_dot_to_comma(value) + data_type = "float" case datetime.datetime(): value = value.strftime(self.DATE_TIME_FORMAT) + data_type = "str" case datetime.date(): value = value.strftime(self.DATE_FORMAT) - return value + data_type = "str" + + if data_type is None: + data_type = "str" + + return {"data_type": data_type, "value": value} - def _change_separator_from_dot_to_comma(self, float_as_str: str) -> str: - float_with_comma_separator = helpers.as_text(float_as_str).replace(".", ",") - return float_with_comma_separator + def _place_value_at_location(self, cell_location: str, row_data: dict) -> None: + """ + Places the value in the Excel tab at the specified cell location. + Example: + cell_location = "A1" + row_data = {"data_type": "int", "value": "123"} + """ + formatted_row_value = helpers.as_text(row_data["value"]) + cell = self.tab[cell_location] + cell.value = formatted_row_value + + if row_data["data_type"] in ["int", "float"]: + number_data_format = "n" + cell.data_type = number_data_format + + cell.number_format = self.PYTHON_TYPE_TO_EXCEL_TYPE[row_data["data_type"]] class SingleColumnExcelTabProcessor(BaseExcelTabProcessor): @@ -64,19 +94,12 @@ def _fill_excel_column_with_values(self) -> None: excel_row_index = copy.copy(self.FIRST_ROW_WITH_VALUE_TO_FILL) for row_datum in self.data_tuple: - transformed_row_datum = self._format_not_convertible_to_string_values( - row_datum - ) - formatted_row_datum = helpers.as_text(transformed_row_datum) - + transformed_row_data = self._determine_data_type_and_value(row_datum) cell_location = f"{self.VALUE_COLUMN}{excel_row_index}" - self.__place_value_at_location(cell_location, formatted_row_datum) + self._place_value_at_location(cell_location, transformed_row_data) excel_row_index += 1 - def __place_value_at_location(self, cell_location: str, formatted_row_datum: str): - self.tab[cell_location] = helpers.as_text(formatted_row_datum) - def clear_data_from_tab(self) -> None: logging.warning( "Clearing data for the single-column values tabs not implemented. " @@ -99,11 +122,8 @@ def _fill_excel_rows_with_values(self) -> None: column_index = next(excel_column_names_iterator) cell_location = f"{column_index}{excel_row_index}" - transformed_cell_datum = self._format_not_convertible_to_string_values( - cell_datum - ) - formatted_cell_datum = helpers.as_text(transformed_cell_datum) - self.tab[cell_location] = helpers.as_text(formatted_cell_datum) + transformed_cell_datum = self._determine_data_type_and_value(cell_datum) + self._place_value_at_location(cell_location, transformed_cell_datum) excel_row_index += 1 def clear_data_from_tab(self) -> None: diff --git a/scripts/excel_dwh_filler/script_components/tab_processors/specified.py b/scripts/excel_dwh_filler/script_components/tab_processors/specified.py index a30ea5f..05ffd75 100644 --- a/scripts/excel_dwh_filler/script_components/tab_processors/specified.py +++ b/scripts/excel_dwh_filler/script_components/tab_processors/specified.py @@ -34,7 +34,6 @@ def _fill_excel_column_with_values(self) -> None: row_datum = int(row_datum) else: row_datum = helpers.as_text(row_datum) - row_datum = self._change_separator_from_dot_to_comma(row_datum) elif isinstance(row_datum, datetime.date): row_datum = row_datum.strftime(self.DATE_FORMAT) cell_location = f"{self.VALUE_COLUMN}{excel_row_index}"