Skip to content

Commit

Permalink
Export modified and documentation updated
Browse files Browse the repository at this point in the history
  • Loading branch information
david-salac committed Aug 19, 2020
1 parent dcc638f commit 968c93d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 40 deletions.
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ sheet = ps.Spreadsheet.create_new_sheet(
```

Other (keywords) arguments:
1. `rows_labels (List[str])`: _(optional)_ List of masks (aliases)
for row names.
2. `columns_labels (List[str])`: _(optional)_ List of masks (aliases)
for column names.
1. `rows_labels (List[Union[str, SkippedLabel]])`: _(optional)_ List of masks
(aliases) for row names.
2. `columns_labels (List[Union[str, SkippedLabel]])`: _(optional)_ List of
masks (aliases) for column names. If the instance of SkippedLabel is
used, the export skips this label.
3. `rows_help_text (List[str])`: _(optional)_ List of help texts for each row.
4. `columns_help_text (List[str])`: _(optional)_ List of help texts for each
column.
column. If the instance of SkippedLabel is used, the export skips this label.
5. `excel_append_row_labels (bool)`: _(optional)_ If True, one column is added
on the beginning of the sheet as a offset for labels.
6. `excel_append_column_labels (bool)`: _(optional)_ If True, one row is
Expand Down Expand Up @@ -710,7 +711,8 @@ sheet.to_excel(
"value": "Value",
"description": "Description"
}),
values_only: bool = False
values_only: bool = False,
skipped_label_replacement: str = ''
)
```
The only required argument is the path to the destination file (positional
Expand All @@ -732,6 +734,8 @@ for the sheet with variables (first row in the sheet). Dictionary should look
like: `{"name": "Name", "value": "Value", "description": "Description"}`.
* `values_only (bool)`: If true, only values (and not formulas) are
exported.
* `skipped_label_replacement (str)`: Replacement for the SkippedLabel
instances.

##### Setting the format/style for Excel cells
There is a possibility to set the style/format of each cell in the grid
Expand Down Expand Up @@ -787,6 +791,9 @@ skipped, default value is false (NaN values are included).
* `append_dict (dict)`: Append this dictionary to output.
* `generate_schema (bool)`: If true, returns the JSON schema.

All the rows and columns with labels that are instances of SkippedLabel are
entirely skipped.

**The return value is:**

Dictionary with keys: 1. column/row, 2. row/column, 3. language or
Expand Down Expand Up @@ -1029,7 +1036,8 @@ sheet.to_excel(*,
sep: str = ',',
line_terminator: str = '\n',
na_rep: str = '',
skip_labels: bool = False
skip_labels: bool = False,
skipped_label_replacement: str = ''
)
```
Parameters are (all optional and key-value only):
Expand All @@ -1044,6 +1052,8 @@ language in each cell instead of values.
* `na_rep (str)`: Replacement for the missing data.
* `skip_labels (bool)`: If true, first row and column with labels is
skipped
* `skipped_label_replacement (str)`: Replacement for the SkippedLabel
instances.

**The return value is:**

Expand All @@ -1067,7 +1077,8 @@ sheet.to_markdown(*,
spaces_replacement: str = ' ',
top_right_corner_text: str = "Sheet",
na_rep: str = '',
skip_labels: bool = False
skip_labels: bool = False,
skipped_label_replacement: str = ''
)
```
Parameters are (all optional, all key-value only):
Expand All @@ -1080,6 +1091,8 @@ descriptions (labels) are replaced with this string.
* `na_rep (str)`: Replacement for the missing data.
* `skip_labels (bool)`: If true, first row and column with labels is
skipped
* `skipped_label_replacement (str)`: Replacement for the SkippedLabel
instances.

**The return value is:**

Expand All @@ -1104,7 +1117,8 @@ sheet.to_html_table(*,
top_right_corner_text: str = "Sheet",
na_rep: str = '',
language_for_description: str = None,
skip_labels: bool = False
skip_labels: bool = False,
skipped_label_replacement: str = ''
)
```
Parameters are (all optional, all key-value only):
Expand All @@ -1118,6 +1132,8 @@ of each computational cell is inserted as word of this language
(if the property description is not set).
* `skip_labels (bool)`: If true, first row and column with labels is
skipped
* `skipped_label_replacement (str)`: Replacement for the SkippedLabel
instances.

**The return value is:**

Expand Down
1 change: 1 addition & 0 deletions portable_spreadsheet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .cell_slice import CellSlice # noqa
from .grammars import GRAMMARS # noqa
from .grammar_utils import GrammarUtils # noqa
from .skipped_label import SkippedLabel # noqa

__version__ = "1.0.0"
__status__ = "Production"
84 changes: 62 additions & 22 deletions portable_spreadsheet/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def to_excel(self,
"description": "Description"
}),
values_only: bool = False,
skipped_label_value: str = ''
skipped_label_replacement: str = ''
) -> None:
"""Export the values inside Spreadsheet instance to the
Excel 2010 compatible .xslx file
Expand All @@ -182,7 +182,7 @@ def to_excel(self,
for the sheet with variables (first row in the sheet).
values_only (bool): If true, only values (and not formulas) are
exported.
skipped_label_value (str): Replacement for the SkippedLabel
skipped_label_replacement (str): Replacement for the SkippedLabel
instances.
"""
# Quick sanity check
Expand Down Expand Up @@ -285,7 +285,7 @@ def to_excel(self,
col_idx + self.export_offset[1]
].replace(' ', spaces_replacement)
if isinstance(col_lbl, SkippedLabel):
col_lbl = skipped_label_value
col_lbl = skipped_label_replacement
worksheet.write(0,
col_idx + int(
self.cell_indices.excel_append_row_labels
Expand All @@ -300,7 +300,7 @@ def to_excel(self,
row_idx + self.export_offset[0]
].replace(' ', spaces_replacement)
if isinstance(row_lbl, SkippedLabel):
row_lbl = skipped_label_value
row_lbl = skipped_label_replacement
worksheet.write(row_idx + int(
self.cell_indices.excel_append_column_labels
),
Expand Down Expand Up @@ -426,8 +426,14 @@ def to_dictionary(self,
# Export the spreadsheet to the dictionary (that can by JSON-ified)
values = {x_start_key: {}}
for idx_x in range(x_range):
if isinstance(x[idx_x], SkippedLabel):
# Skip labels that are intended to be skipped
continue
y_values = {y_start_key: {}}
for idx_y in range(y_range):
if isinstance(y[idx_y], SkippedLabel):
# Skip labels that are intended to be skipped
continue
# Select the correct cell
if by_row:
cell = self._get_cell_at(idx_x, idx_y)
Expand Down Expand Up @@ -481,6 +487,9 @@ def to_dictionary(self,
# Add row description (and labels)
data['rows'] = []
for idx, x_label in enumerate(x):
if isinstance(x[idx_x], SkippedLabel):
# Skip labels that are intended to be skipped
continue
metadata: dict = {"name": x_label}
if x_helptext is not None:
# Add the help text (description) for the row
Expand All @@ -494,6 +503,9 @@ def to_dictionary(self,
# Add column description (and labels)
data['columns'] = []
for idx, y_label in enumerate(y):
if isinstance(y[idx_y], SkippedLabel):
# Skip labels that are intended to be skipped
continue
metadata = {"name": y_label}
if y_helptext is not None:
# Add the help text (description) for the column
Expand Down Expand Up @@ -763,6 +775,7 @@ def generate_json_schema() -> dict:

def to_string_of_values(self) -> str:
"""Export values inside table to the Python array definition string.
(Mainly helpful for debugging purposes)
Returns:
str: Python list definition string.
Expand All @@ -787,7 +800,8 @@ def to_2d_list(self, *,
top_right_corner_text: str = "Sheet",
skip_labels: bool = False,
na_rep: Optional[object] = None,
spaces_replacement: str = ' ',) -> List[List[object]]:
spaces_replacement: str = ' ',
skipped_label_replacement: str = '') -> List[List[object]]:
"""Export values 2 dimensional Python array.
Args:
Expand All @@ -800,6 +814,8 @@ def to_2d_list(self, *,
equals to None).
skip_labels (bool): If true, first row and column with labels is
skipped
skipped_label_replacement (str): Replacement for the SkippedLabel
instances.
Returns:
List[List[object]]: Python array.
Expand All @@ -816,17 +832,21 @@ def to_2d_list(self, *,
row.append(top_right_corner_text)
# Insert labels of columns:
for col_i in range(self.shape[1]):
col = self.cell_indices.columns_labels[
col_lbl = self.cell_indices.columns_labels[
col_i + self.export_offset[1]
]
value = col.replace(' ', spaces_replacement)
row.append(value)
].replace(' ', spaces_replacement)
if isinstance(col_lbl, SkippedLabel):
col_lbl = skipped_label_replacement
row.append(col_lbl)
else:
if not skip_labels:
# Insert labels of rows
row.append(self.cell_indices.rows_labels[
row_lbl = self.cell_indices.rows_labels[
row_idx + self.export_offset[0]
].replace(' ', spaces_replacement))
].replace(' ', spaces_replacement)
if isinstance(row_lbl, SkippedLabel):
row_lbl = skipped_label_replacement
row.append(row_lbl)
for col_idx in range(self.shape[1]):
# Append actual values:
cell_at_position = self._get_cell_at(row_idx, col_idx)
Expand Down Expand Up @@ -854,6 +874,7 @@ def to_csv(self, *,

sep: str = ',',
line_terminator: str = '\n',
skipped_label_replacement: str = ''
) -> str:
"""Export values to the string in the CSV logic
Expand All @@ -868,14 +889,19 @@ def to_csv(self, *,
na_rep (str): Replacement for the missing data.
skip_labels (bool): If true, first row and column with labels is
skipped
skipped_label_replacement (str): Replacement for the SkippedLabel
instances.
Returns:
str: CSV of the values
"""
sheet_as_array = self.to_2d_list(
top_right_corner_text=top_right_corner_text, na_rep=na_rep,
skip_labels=skip_labels, spaces_replacement=spaces_replacement,
language=language
top_right_corner_text=top_right_corner_text,
na_rep=na_rep,
skip_labels=skip_labels,
spaces_replacement=spaces_replacement,
language=language,
skipped_label_replacement=skipped_label_replacement
)
export = ""
for row_idx in range(len(sheet_as_array)):
Expand All @@ -892,7 +918,8 @@ def to_markdown(self, *,
top_right_corner_text: str = "Sheet",
skip_labels: bool = False,
na_rep: Optional[object] = '',
spaces_replacement: str = ' '
spaces_replacement: str = ' ',
skipped_label_replacement: str = ''
):
"""Export values to the string in the Markdown (MD) file logic
Expand All @@ -905,14 +932,19 @@ def to_markdown(self, *,
na_rep (str): Replacement for the missing data.
skip_labels (bool): If true, first row and column with labels is
skipped
skipped_label_replacement (str): Replacement for the SkippedLabel
instances.
Returns:
str: Markdown (MD) compatible table of the values
"""
sheet_as_array = self.to_2d_list(
top_right_corner_text=top_right_corner_text, na_rep=na_rep,
skip_labels=skip_labels, spaces_replacement=spaces_replacement,
language=language
top_right_corner_text=top_right_corner_text,
na_rep=na_rep,
skip_labels=skip_labels,
spaces_replacement=spaces_replacement,
language=language,
skipped_label_replacement=skipped_label_replacement
)
export = ""
for row_idx in range(len(sheet_as_array)):
Expand Down Expand Up @@ -989,7 +1021,8 @@ def to_html_table(self, *,
top_right_corner_text: str = "Sheet",
na_rep: str = '',
language_for_description: str = None,
skip_labels: bool = False) -> str:
skip_labels: bool = False,
skipped_label_replacement: str = '') -> str:
"""Export values to the string in the HTML table logic
Args:
Expand All @@ -1002,6 +1035,8 @@ def to_html_table(self, *,
language (if the property description is not set).
skip_labels (bool): If true, first row and column with labels is
skipped
skipped_label_replacement (str): Replacement for the SkippedLabel
instances.
Returns:
str: HTML table definition
Expand All @@ -1023,7 +1058,9 @@ def to_html_table(self, *,
export += "<th>"
col = self.cell_indices.columns_labels[
col_i + self.export_offset[1]
]
].replace(' ', spaces_replacement)
if isinstance(col, SkippedLabel):
col = skipped_label_replacement
if (help_text := # noqa 203
self.cell_indices.columns_help_text) is not None:
title_attr = ' title="{}"'.format(
Expand All @@ -1032,7 +1069,7 @@ def to_html_table(self, *,
else:
title_attr = ""
export += f'<a href="javascript:;" {title_attr}>'
export += col.replace(' ', spaces_replacement)
export += col
export += "</a>"
export += "</th>"
else:
Expand All @@ -1047,9 +1084,12 @@ def to_html_table(self, *,
title_attr = ""
export += "<td>"
export += f'<a href="javascript:;" {title_attr}>'
export += self.cell_indices.rows_labels[
row_lbl = self.cell_indices.rows_labels[
row_idx + self.export_offset[0]
].replace(' ', spaces_replacement)
if isinstance(row_lbl, SkippedLabel):
row_lbl = skipped_label_replacement
export += row_lbl
export += "</a>"
export += "</td>"

Expand Down
3 changes: 2 additions & 1 deletion portable_spreadsheet/skipped_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def replace(self, old, new, count: int = -1):
if self.label is None:
return
self.label = self.label.replace(old, new, count)
return self

def __str__(self):
"""Overload to string method."""
if self.label is None:
return None
return ""
return self.label
Loading

0 comments on commit 968c93d

Please sign in to comment.