diff --git a/README.md b/README.md index a5d9c33..d7db9f2 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,10 @@ added on the beginning of the sheet as a offset for labels. 8. `warning_logger (Callable[[str], None]])`: Function that logs the warnings (or `None` if logging should be skipped). 9. `values_only (bool)`: If set to True, only values are computed and -nothing can be exported (makes script run faster) +nothing can be exported (makes script run faster). +10. `system_languages: Tuple[str, ...]`: Defines system languages that should +be always included. If you want to optimize performance, this tuple should +be as small as possible. First two are the most important because they define labels for the columns and rows indices. The warnings mention above occurs when the slices are diff --git a/portable_spreadsheet/__init__.py b/portable_spreadsheet/__init__.py index db7ccdb..09449d6 100644 --- a/portable_spreadsheet/__init__.py +++ b/portable_spreadsheet/__init__.py @@ -7,5 +7,5 @@ from .grammar_utils import GrammarUtils # noqa from .skipped_label import SkippedLabel # noqa -__version__ = "2.1.9" +__version__ = "2.2.0" __status__ = "Production" diff --git a/portable_spreadsheet/cell_indices.py b/portable_spreadsheet/cell_indices.py index 6ce9a19..3f53767 100644 --- a/portable_spreadsheet/cell_indices.py +++ b/portable_spreadsheet/cell_indices.py @@ -2,7 +2,7 @@ import copy from .grammars import GRAMMARS -from .cell_indices_templates import cell_indices_generators, system_languages +from .cell_indices_templates import cell_indices_generators # ==== TYPES ==== # mapping from language to list, used for mapping from language to list rows @@ -26,7 +26,9 @@ def __init__(self, columns_help_text: List[str] = None, excel_append_row_labels: bool = True, excel_append_column_labels: bool = True, - warning_logger: Optional[Callable[[str], None]] = None + warning_logger: Optional[Callable[[str], None]] = None, + values_only: bool = False, + system_languages: Tuple[str, ...] = ('excel', 'python_numpy') ): """Create cell indices object. @@ -47,6 +49,11 @@ def __init__(self, on the beginning of the sheet as a offset for labels. warning_logger (Optional[Callable[[str], None]]): Function that logs the warnings (or None if skipped). + values_only (bool): Set the sheet to store only values and not to + constructs words - makes computation faster but disable all + export functionality + system_languages (Tuple[str, ...]): System languages that are + always included. """ # Quick sanity check: if rows_labels is not None \ @@ -101,8 +108,36 @@ def __init__(self, self.number_of_columns: int = number_of_columns self.excel_append_row_labels: bool = excel_append_row_labels self.excel_append_column_labels: bool = excel_append_column_labels + # Define user defined names for rows and columns + self.rows_labels: list = copy.deepcopy(rows_labels) + self.columns_labels: list = copy.deepcopy(columns_labels) + # Or define auto generated aliases as an integer sequence from 0 + if rows_labels is None: + self.rows_labels = [str(_row_n) for _row_n in + range(number_of_rows)] + if columns_labels is None: + self.columns_labels = [str(_col_n) for _col_n in + range(number_of_columns)] + # String representation of indices + self.rows_labels_str: List[str] = \ + [str(lb) for lb in self.rows_labels] + self.columns_labels_str: List[str] = \ + [str(lb) for lb in self.columns_labels] + # assign the help texts + self.rows_help_text: List[str] = copy.deepcopy(rows_help_text) + self.columns_help_text: List[str] = copy.deepcopy(columns_help_text) + # This set the sheet to store only values and not to constructs words + # it is used by Cell class + self.values_only: bool = values_only + self.rows: T_lg_ar = {} self.columns: T_lg_ar = {} + self.user_defined_languages: List[str] = [] + + if values_only: + # Optimisation + return + # Append the system languages for language, generator in cell_indices_generators.items(): if language not in system_languages: @@ -120,35 +155,14 @@ def __init__(self, offset_column) self.rows[language] = rows self.columns[language] = cols + # Append the not-system languages and user defined languages - self.user_defined_languages: List[str] = [] if rows_columns is not None: for language, values in rows_columns.items(): rows, cols = values self.rows[language] = rows self.columns[language] = cols self.user_defined_languages.append(language) - # Define user defined names for rows and columns - self.rows_labels: list = copy.deepcopy(rows_labels) - self.columns_labels: list = copy.deepcopy(columns_labels) - # Or define auto generated aliases as an integer sequence from 0 - if rows_labels is None: - self.rows_labels = [str(_row_n) for _row_n in - range(number_of_rows)] - if columns_labels is None: - self.columns_labels = [str(_col_n) for _col_n in - range(number_of_columns)] - # String representation of indices - self.rows_labels_str: List[str] = \ - [str(lb) for lb in self.rows_labels] - self.columns_labels_str: List[str] = \ - [str(lb) for lb in self.columns_labels] - # assign the help texts - self.rows_help_text: List[str] = copy.deepcopy(rows_help_text) - self.columns_help_text: List[str] = copy.deepcopy(columns_help_text) - # This set the sheet to store only values and not to constructs words - # it is used by Cell class - self.values_only: bool = False @property def shape(self) -> Tuple[int, int]: @@ -177,6 +191,9 @@ def expand_size(self, new_columns_labels: List[str] = None, new_rows_help_text: List[str] = None, new_columns_help_text: List[str] = None, + values_only: bool = False, + system_languages: Tuple[str, ...] = ('excel', + 'python_numpy') ) -> 'CellIndices': """Expand the size of the table. @@ -193,6 +210,11 @@ def expand_size(self, be added. new_columns_help_text (List[str]): List of help texts for each column to be added. + values_only (bool): Set the sheet to store only values and not to + constructs words - makes computation faster but disable all + export functionality + system_languages (Tuple[str, ...]): System languages that are + always included. """ expanded = copy.deepcopy(self) # Quick sanity check: @@ -217,6 +239,9 @@ def expand_size(self, raise ValueError("Number of columns help texts has to be the same " "as number of columns!") # ------------------- + if values_only: + system_languages = tuple() + # Append the system languages for language, generator in cell_indices_generators.items(): if language not in system_languages: diff --git a/portable_spreadsheet/cell_indices_templates.py b/portable_spreadsheet/cell_indices_templates.py index a4668ab..7f338a0 100644 --- a/portable_spreadsheet/cell_indices_templates.py +++ b/portable_spreadsheet/cell_indices_templates.py @@ -86,4 +86,3 @@ def native_generator(rows: int, "python_numpy": python_numpy_generator, "native": native_generator, } -system_languages = ['excel', 'python_numpy'] diff --git a/portable_spreadsheet/sheet.py b/portable_spreadsheet/sheet.py index 57731e3..3b6f01f 100644 --- a/portable_spreadsheet/sheet.py +++ b/portable_spreadsheet/sheet.py @@ -81,7 +81,8 @@ def create_new_sheet( excel_append_row_labels: bool = True, excel_append_column_labels: bool = True, warning_logger: Optional[Callable[[str], None]] = None, - values_only: bool = False + values_only: bool = False, + system_languages: Tuple[str, ...] = ('excel', 'python_numpy'), ) -> 'Sheet': """Direct way of creating instance. @@ -108,10 +109,15 @@ def create_new_sheet( values_only (bool): Set the sheet to store only values and not to constructs words - makes computation faster but disable all export functionality + system_languages (Tuple[str, ...]): System languages that are + always included. Returns: Sheet: New instance of spreadsheet. """ + if values_only: + system_languages = tuple() + class_index = CellIndices( number_of_rows, number_of_columns, @@ -122,9 +128,10 @@ def create_new_sheet( columns_help_text=columns_help_text, excel_append_row_labels=excel_append_row_labels, excel_append_column_labels=excel_append_column_labels, - warning_logger=warning_logger + warning_logger=warning_logger, + values_only=values_only, + system_languages=system_languages ) - class_index.values_only = values_only return cls(class_index, warning_logger, name) def _initialise_array(self) -> T_sheet: @@ -380,7 +387,9 @@ def expand(self, new_rows_labels: List[Union[str, SkippedLabel]] = None, new_columns_labels: List[Union[str, SkippedLabel]] = None, new_rows_help_text: List[str] = None, - new_columns_help_text: List[str] = None + new_columns_help_text: List[str] = None, + values_only: bool = False, + system_languages: Tuple[str, ...] = ('excel', 'python_numpy') ): """Expand the size of the table. @@ -399,6 +408,11 @@ def expand(self, be added. new_columns_help_text (List[str]): List of help texts for each column to be added. + values_only (bool): Set the sheet to store only values and not to + constructs words - makes computation faster but disable all + export functionality + system_languages (Tuple[str, ...]): System languages that are + always included. """ self.expand_using_cell_indices( self.cell_indices.expand_size( @@ -409,7 +423,9 @@ def expand(self, new_rows_labels=new_rows_labels, new_columns_labels=new_columns_labels, new_rows_help_text=new_rows_help_text, - new_columns_help_text=new_columns_help_text + new_columns_help_text=new_columns_help_text, + values_only=values_only, + system_languages=system_languages ) ) diff --git a/setup.py b/setup.py index 49856a8..e4122dc 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="portable-spreadsheet", - version="2.1.9", + version="2.2.0", author="David Salac", author_email="info@davidsalac.eu", description="A simple spreadsheet that keeps tracks of each operation of each cell in defined languages. Logic allows exporting sheets to Excel files (and see how each cell is computed), to the JSON strings with a description of computation of each cell (e. g. in the native language). Other formats, like HTML, CSV and Markdown (MD), are also implemented (user can define own format). It also allows reconstructing behaviours in native Python with NumPy.", # noqa diff --git a/tests/test_spreadsheet.py b/tests/test_spreadsheet.py index 1f9a7ed..535a584 100644 --- a/tests/test_spreadsheet.py +++ b/tests/test_spreadsheet.py @@ -67,6 +67,35 @@ def test_create_new_sheet(self): """Test the instance sheet""" self.assertTrue(isinstance(self.sheet, Sheet)) + def test_expand_sheet_just_values(self): + """Test the expanding of the sheet size if it has only values""" + sheet = Sheet.create_new_sheet( + self.nr_row, self.nr_col, + rows_labels=self.rows_labels, + columns_labels=self.columns_labels, + rows_help_text=self.rows_help_text, + columns_help_text=self.columns_help_text, + excel_append_row_labels=True, + excel_append_column_labels=True, + warning_logger=lambda message: self.warnings.append(message), + values_only=True + ) + self.assertTupleEqual(sheet.shape, (20, 30)) + expand_row = 3 + expand_col = 5 + new_rows_labels = [f'LeR_{r_i}' for r_i in range(expand_row)] + new_columns_labels = [f'LeC_{c_i}' for c_i in range(expand_col)] + new_rows_help_text = [f'HeR_{r_i}' for r_i in range(expand_row)] + new_columns_help_text = [f'HeC_{c_i}' for c_i in range(expand_col)] + # Test expansion + sheet.expand(3, 5, + new_rows_labels=new_rows_labels, + new_columns_labels=new_columns_labels, + new_rows_help_text=new_rows_help_text, + new_columns_help_text=new_columns_help_text + ) + self.assertTupleEqual(sheet.shape, (23, 35)) + def test_expand_sheet(self): """Test the expanding of the sheet size""" # Try to expand without all parameters