Skip to content

Commit

Permalink
Merge pull request #42 from david-salac/Optimisation
Browse files Browse the repository at this point in the history
Optimisation
  • Loading branch information
david-salac authored Mar 15, 2021
2 parents fad89e5 + 48e9193 commit 1e9651b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 33 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion portable_spreadsheet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
73 changes: 49 additions & 24 deletions portable_spreadsheet/cell_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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 \
Expand Down Expand Up @@ -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:
Expand All @@ -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]:
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion portable_spreadsheet/cell_indices_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,3 @@ def native_generator(rows: int,
"python_numpy": python_numpy_generator,
"native": native_generator,
}
system_languages = ['excel', 'python_numpy']
26 changes: 21 additions & 5 deletions portable_spreadsheet/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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
)
)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/test_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1e9651b

Please sign in to comment.