Skip to content

Commit

Permalink
PENGSOL-483: Format float numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Makar committed Jan 7, 2025
1 parent c9d28ff commit 548500f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
60 changes: 40 additions & 20 deletions scripts/excel_dwh_filler/script_components/tab_processors/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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. "
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 548500f

Please sign in to comment.