Skip to content

Commit

Permalink
Merge pull request #21 from david-salac/development
Browse files Browse the repository at this point in the history
Version 1.0.0
  • Loading branch information
david-salac authored Aug 19, 2020
2 parents 4430976 + 968c93d commit ba718e2
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 87 deletions.
67 changes: 58 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 @@ -303,6 +304,12 @@ or set of rows and columns). Following code, select the third column:
```python
sheet.iloc[:,2]
```
On the other hand
```python
sheet.loc[:,'Handy column']
```
selects all the rows in the columns with the label _'Handy column'_.

You can again set the values in the slice to some constant, or the array
of constants, or to another cell, or to the result of some computation.
```python
Expand All @@ -312,6 +319,33 @@ sheet.iloc[:,2] = sheet.iloc[1,3] # Just a reference to a cell
```
Technically the slice is the instance of `CellSlice` class.

There are two ways how to slice, either using `.loc` or `.iloc` attribute.
Where `iloc` uses integer position and `loc` uses label of the position
(as a string).

By default the right-most value is excluded when defining slices. If you want
to use right-most value indexing, use one of the methods described below.

#### Slicing using method (with the right-most value included option)
Sometimes, it is quite helpful to use a slice that includes the right-most
value. There are two functions for this purpose:
1. `sheet.iloc.get_slice(ROW_INDEX, COLUMN_INDEX, include_right=[True/False])`:
This way is equivalent to the one presented above with square brackets `[]`.
The difference is the key-value attribute `include_right` that enables the
possibility of including the right-most value of the slice (default value is
False). If you want to use slice as your index, you need to pass some `slice`
object to one (or both) of the indices. For example:
`sheet.iloc.get_slice(slice(0, 7), 3, include_right=True])` selects first nine
rows (because 8th row - right-most one - is included) from the fourth column
of the sheet _(remember, all is indexed from zero)_.

2. `sheet.iloc.set_slice(ROW_INDEX, COLUMN_INDEX, VALUE,
include_right=[True/False])`: this command set slice to _VALUE_ in the similar
logic as when you call `get_slice` method (see the first point).

There are again two possibilities, either to use `iloc` with integer position
or to use `loc` with labels.

#### Aggregate functions
The slice itself can be used for computations using aggregate functions.

Expand Down Expand Up @@ -677,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 @@ -699,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 @@ -754,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 @@ -996,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 @@ -1011,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 @@ -1034,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 @@ -1047,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 @@ -1071,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 @@ -1085,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
3 changes: 2 additions & 1 deletion 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__ = "0.1.15"
__version__ = "1.0.0"
__status__ = "Production"
22 changes: 12 additions & 10 deletions portable_spreadsheet/cell_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,20 @@ def __init__(self,
self.columns[language] = cols
self.user_defined_languages.append(language)
# Define user defined names for rows and columns
self.rows_labels: List[str] = copy.deepcopy(rows_labels)
self.columns_labels: List[str] = copy.deepcopy(columns_labels)
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)
Expand Down Expand Up @@ -282,14 +287,11 @@ def expand_size(self,
expanded.number_of_columns + new_number_of_columns
)
]
# Or define auto generated aliases as an integer sequence from 0
if new_columns_labels is None:
expanded.columns_labels = [
str(i)
for i in range(
expanded.number_of_columns + new_number_of_columns
)
]
# String representation of indices
expanded.rows_labels_str: List[str] = \
[str(lb) for lb in expanded.rows_labels]
expanded.columns_labels_str: List[str] = \
[str(lb) for lb in expanded.columns_labels]
# assign the help texts
if expanded.rows_help_text is not None and new_number_of_rows > 0:
if new_rows_help_text is None:
Expand Down
Loading

0 comments on commit ba718e2

Please sign in to comment.