-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Convert between Excel file and
Table
(#233)
Closes #138. Closes #139 . ### Summary of Changes Created the methods to_excel_file() and from_excel_file() in Class Table. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Co-authored-by: patrikguempel <patrikguempel@gmail.com> Co-authored-by: Lars Reimann <mail@larsreimann.com> Co-authored-by: patrikguempel <128832338+patrikguempel@users.noreply.github.com>
- Loading branch information
1 parent
4bc4c09
commit 0d7a998
Showing
6 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
40 changes: 40 additions & 0 deletions
40
tests/safeds/data/tabular/containers/_table/test_from_excel_file.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
|
||
from tests.helpers import resolve_resource_path | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("path", "expected"), | ||
[ | ||
( | ||
resolve_resource_path("./dummy_excel_file.xlsx"), | ||
Table.from_dict( | ||
{ | ||
"A": [1], | ||
"B": [2], | ||
}, | ||
), | ||
), | ||
( | ||
Path(resolve_resource_path("./dummy_excel_file.xlsx")), | ||
Table.from_dict( | ||
{ | ||
"A": [1], | ||
"B": [2], | ||
}, | ||
), | ||
), | ||
], | ||
ids=["string path", "object path"], | ||
) | ||
def test_should_create_table_from_excel_file(path: str | Path, expected: Table) -> None: | ||
table = Table.from_excel_file(path) | ||
assert table == expected | ||
|
||
|
||
def test_should_raise_if_file_not_found() -> None: | ||
with pytest.raises(FileNotFoundError): | ||
Table.from_excel_file(resolve_resource_path("test_table_from_excel_file_invalid.xls")) |
26 changes: 26 additions & 0 deletions
26
tests/safeds/data/tabular/containers/_table/test_to_excel_file.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
|
||
from safeds.data.tabular.containers import Table | ||
|
||
|
||
def test_should_create_csv_file_from_table_by_str() -> None: | ||
table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}) | ||
with NamedTemporaryFile(suffix=".xlsx") as tmp_table_file: | ||
tmp_table_file.close() | ||
with Path(tmp_table_file.name).open("w", encoding="utf-8") as tmp_file: | ||
table.to_excel_file(tmp_file.name) | ||
with Path(tmp_table_file.name).open("r", encoding="utf-8") as tmp_file: | ||
table_r = Table.from_excel_file(tmp_file.name) | ||
assert table == table_r | ||
|
||
|
||
def test_should_create_csv_file_from_table_by_path() -> None: | ||
table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}) | ||
with NamedTemporaryFile(suffix=".xlsx") as tmp_table_file: | ||
tmp_table_file.close() | ||
with Path(tmp_table_file.name).open("w", encoding="utf-8") as tmp_file: | ||
table.to_excel_file(Path(tmp_file.name)) | ||
with Path(tmp_table_file.name).open("r", encoding="utf-8") as tmp_file: | ||
table_r = Table.from_excel_file(Path(tmp_file.name)) | ||
assert table == table_r |