Skip to content

Commit

Permalink
Merge pull request #10 from david-salac/development
Browse files Browse the repository at this point in the history
Version 0.1.5
  • Loading branch information
david-salac authored May 27, 2020
2 parents 6d3e993 + 2a4e842 commit e49e428
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 120 deletions.
58 changes: 49 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,19 @@ exported (which can lead to data losses).

#### How to change the size of the spreadsheet
You can only expand the size of the spreadsheet (it's because of the
built-in behaviour of languages). We, however, strongly recommend not to
do so.
built-in behaviour of language construction). We, however, strongly recommend
not to do so. Simplified logic looks like:
```
sheet.expand_size(
sheet.cell_indices.expand_size(
number_of_new_rows, number_of_new_columns, [new_rows_columns]
)
# Append 7 rows and 8 columns to existing sheet:
sheet.expand(
7, 8,
{
"native": ([...], [...]) # Fill 8 new values for rows, columns here
}
)
```
Parameters of the `cell_indices.expand_size` method are of the same
logic as the parameters of `Spreadsheet.create_new_sheet`.
Parameters of the `Spreadsheet.expand` method are of the same
logic and order as the parameters of `Spreadsheet.create_new_sheet`.

### Shape of the Spreadsheet object
If you want to know what is the actual size of the spreadsheet, you can
Expand Down Expand Up @@ -362,6 +364,31 @@ sheet.iloc[i,j] = sheet.fn.conditional(
)
```

### Raw statement
The raw statement represents the extreme way how to set-up value and
computation string of the cell. It should be used only to circumvent
issues with missing or defective functionality.

The raw statement is accessible using `fn` property of the Spreadsheet class
object.

The raw statement should never be used unless you really have to.

#### Example of raw statement
Consider that you need to compute an arccosine value of some cell:
```
sheet.iloc[i,j] = sheet.fn.raw(
# Value that should be used as the result (as a Cell instance):
sheet.fn.const(numpy.arccos(0.7)),
# Definition of words in each language:
{
'python_numpy': "numpy.arccos(0.7)",
'excel': "ACOS(0.7)"
# Potentialy some other languages, like 'native', etc.
}
)
```

### Offset function
The offset function represents the possibility of reading the value
that is shifted by some number rows left, and some number of columns
Expand Down Expand Up @@ -436,6 +463,10 @@ is the instance of the Cell class):
Usage: `sheet.iloc[i,j] = ~OPERAND`.
_Also available in the `fn` property of the `sheet` object.
Usage: `sheet.iloc[i,j] = sheet.fn.neg(OPERAND)`_
9. **Signum function**: returns the signum of the input value.
For example sign(-4.5) = -1, sign(5) = 1, sign(0) = 0.
Available in the `fn` property of the `sheet` object.
Usage: `sheet.iloc[i,j] = sheet.fn.sign(OPERAND)`

All unary operators are defined in the `fn` property of the Spreadsheet
object (together with brackets, that works exactly the same - see bellow).
Expand Down Expand Up @@ -608,7 +639,8 @@ sheet.to_excel(
"name": "Name",
"value": "Value",
"description": "Description"
})
}),
values_only: bool = False
)
```
The only required argument is the path to the destination file (positional
Expand All @@ -628,6 +660,8 @@ to set them up (directly from the sheet).
* `variables_sheet_header (Dict[str, str])`: Define the labels (header)
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.

##### 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 @@ -907,6 +941,7 @@ Output of the JSON format
It can be done using the interface:
```
sheet.to_excel(*,
language: Optional[str] = None,
spaces_replacement: str = ' ',
top_right_corner_text: str = "Sheet",
sep: str = ',',
Expand All @@ -917,6 +952,8 @@ sheet.to_excel(*,
```
Parameters are (all optional and key-value only):

* `language (Optional[str])`: If set-up, export the word in this
language in each cell instead of values.
* `spaces_replacement (str)`: All the spaces in the rows and columns
descriptions (labels) are replaced with this string.
* `top_right_corner_text (str)`: Text in the top right corner.
Expand Down Expand Up @@ -944,6 +981,7 @@ R_4,17,18,19,20
It can be done using the interface:
```
sheet.to_markdown(*,
language: Optional[str] = None,
spaces_replacement: str = ' ',
top_right_corner_text: str = "Sheet",
na_rep: str = '',
Expand All @@ -952,6 +990,8 @@ sheet.to_markdown(*,
```
Parameters are (all optional, all key-value only):

* `language (Optional[str])`: If set-up, export the word in this
language in each cell instead of values.
* `spaces_replacement (str)`: All the spaces in the rows and columns
descriptions (labels) are replaced with this string.
* `top_right_corner_text (str)`: Text in the top right corner.
Expand Down
34 changes: 34 additions & 0 deletions portable_spreadsheet/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,24 @@ def reference(other: 'Cell', /) -> 'Cell': # noqa E225
cell_type=CellType.computational
)

@staticmethod
def raw(other: 'Cell', words: Dict[str, str], /) -> 'Cell': # noqa: E225
"""Add the raw statement and use the value of input cell.
Args:
other (Cell): Input cell that defines value and type of output.
words (Dict[str, str]): Word for each language (language is a key,
word is a value)
Returns:
Cell: Expression with defined word
"""
return Cell(value=other.value,
words=WordConstructor.raw(other, words),
cell_indices=other.cell_indices,
cell_type=CellType.computational
)

@staticmethod
def variable(other: 'Cell', /) -> 'Cell': # noqa E225
"""The cell as a variable.
Expand Down Expand Up @@ -828,6 +846,22 @@ def sqrt(other: 'Cell', /) -> 'Cell': # noqa E225
cell_type=CellType.computational
)

@staticmethod
def signum(other: 'Cell', /) -> 'Cell': # noqa E225
"""Signum function of the value in the cell.
Args:
other (Cell): Argument of the signum function.
Returns:
Cell: signum of the input numeric value
"""
return Cell(value=np.sign(other.value),
words=WordConstructor.signum(other),
cell_indices=other.cell_indices,
cell_type=CellType.computational
)

@staticmethod
def logicalNegation(other: 'Cell', /) -> 'Cell': # noqa E225
"""Logical negation of the value in the cell.
Expand Down
25 changes: 16 additions & 9 deletions portable_spreadsheet/cell_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,27 @@ def expand_size(self,
if new_rows_labels is not None:
expanded.rows_labels.extend(new_rows_labels)
else:
expanded.rows_labels = list(
range(expanded.number_of_rows + new_number_of_rows)
)
expanded.rows_labels = [
str(i)
for i in range(expanded.number_of_rows + new_number_of_rows)
]
if new_columns_labels is not None:
expanded.columns_labels.extend(new_columns_labels)
else:
expanded.columns_labels = list(
range(expanded.number_of_columns + new_number_of_columns)
)
expanded.columns_labels = [
str(i)
for i in range(
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 = list(
range(expanded.number_of_columns + new_number_of_columns)
)
expanded.columns_labels = [
str(i)
for i in range(
expanded.number_of_columns + new_number_of_columns
)
]
# 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
18 changes: 18 additions & 0 deletions portable_spreadsheet/grammars.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@
"prefix": "SQRT(",
"suffix": ")",
},
"signum": {
"prefix": "SIGN(",
"suffix": ")",
},
# LOGICAL OPERATIONS
"equal-to": {
"prefix": "",
Expand Down Expand Up @@ -474,6 +478,10 @@
"prefix": "square root of ",
"suffix": "",
},
"signum": {
"prefix": "signum of ",
"suffix": "",
},
# LOGICAL OPERATIONS
"equal-to": {
"prefix": "",
Expand Down Expand Up @@ -746,6 +754,10 @@
"prefix": "np.sqrt(",
"suffix": ")",
},
"signum": {
"prefix": "np.sign(",
"suffix": ")",
},
# LOGICAL OPERATIONS
"equal-to": {
"prefix": "",
Expand Down Expand Up @@ -1088,6 +1100,12 @@
"prefix": str,
"suffix": str,
},
# Signum function (-1 if value is < 0, 0 if val == 0, 1 if val > 0)
"signum": {
"prefix": str,
"suffix": str,
},

# === LOGICAL OPERATIONS (returns true or false) ===
# Equal to
"equal-to": {
Expand Down
Loading

0 comments on commit e49e428

Please sign in to comment.