Skip to content

Commit

Permalink
feat: add _file suffix to methods interacting with files (#103)
Browse files Browse the repository at this point in the history
### Summary of Changes

In `Table`:
* Rename `to_csv` to `to_csv_file`
* Rename `to_json` to `to_json_file`
* Rename `from_csv` to `from_csv_file`
* Rename `from_json` to `from_json_file`
* Rename `path_to_file` parameters to `path`

---------

Co-authored-by: lars-reimann <[email protected]>
  • Loading branch information
lars-reimann and lars-reimann authored Mar 27, 2023
1 parent 3931ce9 commit ec011e4
Show file tree
Hide file tree
Showing 46 changed files with 156 additions and 134 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/data_processing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"source": [
"from safeds.data.tabular.containers import Table\n",
"\n",
"titanic = Table.from_csv(\"data/titanic.csv\")"
"titanic = Table.from_csv_file(\"data/titanic.csv\")"
],
"metadata": {
"collapsed": false
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/data_visualization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"source": [
"from safeds.data.tabular.containers import Table\n",
"\n",
"titanic = Table.from_csv(\"data/titanic.csv\")"
"titanic = Table.from_csv_file(\"data/titanic.csv\")"
],
"metadata": {
"collapsed": false,
Expand Down
20 changes: 10 additions & 10 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Table:
# ------------------------------------------------------------------------------------------------------------------

@staticmethod
def from_csv(path: str) -> Table:
def from_csv_file(path: str) -> Table:
"""
Read data from a CSV file into a table.
Expand Down Expand Up @@ -82,7 +82,7 @@ def from_csv(path: str) -> Table:
raise ValueError(f'Could not read file from "{path}" as CSV') from exception

@staticmethod
def from_json(path: str) -> Table:
def from_json_file(path: str) -> Table:
"""
Read data from a JSON file into a table.
Expand Down Expand Up @@ -1023,37 +1023,37 @@ def scatterplot(self, x_column_name: str, y_column_name: str) -> None:
# Conversion
# ------------------------------------------------------------------------------------------------------------------

def to_csv(self, path_to_file: str) -> None:
def to_csv_file(self, path: str) -> None:
"""
Write the data from the table into a CSV file.
If the file and/or the directories do not exist they will be created.
If the file already exists it will be overwritten.
Parameters
----------
path_to_file : str
path : str
The path to the output file.
"""
Path(os.path.dirname(path_to_file)).mkdir(parents=True, exist_ok=True)
Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True)
data_to_csv = self._data.copy()
data_to_csv.columns = self._schema.get_column_names()
data_to_csv.to_csv(path_to_file, index=False)
data_to_csv.to_csv(path, index=False)

def to_json(self, path_to_file: str) -> None:
def to_json_file(self, path: str) -> None:
"""
Write the data from the table into a JSON file.
If the file and/or the directories do not exist, they will be created.
If the file already exists it will be overwritten.
Parameters
----------
path_to_file : str
path : str
The path to the output file.
"""
Path(os.path.dirname(path_to_file)).mkdir(parents=True, exist_ok=True)
Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True)
data_to_json = self._data.copy()
data_to_json.columns = self._schema.get_column_names()
data_to_json.to_json(path_to_file)
data_to_json.to_json(path)

def to_columns(self) -> list[Column]:
"""
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/safeds/data/tabular/containers/_row/test_has_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@


def test_has_column_positive() -> None:
table = Table.from_csv(resolve_resource_path("test_table_has_column.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv"))
row = table.to_rows()[0]
assert row.has_column("A")


def test_has_column_negative() -> None:
table = Table.from_csv(resolve_resource_path("test_table_has_column.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv"))
row = table.to_rows()[0]
assert not row.has_column("C")
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@


def test_table_column_drop() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
transformed_table = table.drop_columns(["A"])
assert transformed_table.schema.has_column(
"B"
) and not transformed_table.schema.has_column("A")


def test_table_column_drop_warning() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
with pytest.raises(UnknownColumnNameError):
table.drop_columns(["C"])
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
)
def test_drop_duplicate_rows(path: str) -> None:
expected_table = Table(pd.DataFrame(data={"A": [1, 4], "B": [2, 5]}))
table = Table.from_csv(resolve_resource_path(path))
table = Table.from_csv_file(resolve_resource_path(path))
result_table = table.drop_duplicate_rows()
assert expected_table == result_table
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def test_from_columns() -> None:
table_expected = Table.from_csv(resolve_resource_path("test_column_table.csv"))
table_expected = Table.from_csv_file(resolve_resource_path("test_column_table.csv"))
columns_table: list[Column] = [
Column(pd.Series([1, 4]), "A"),
Column(pd.Series([2, 5]), "B"),
Expand Down
18 changes: 18 additions & 0 deletions tests/safeds/data/tabular/containers/_table/test_from_csv_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from safeds.data.tabular.containers import Table
from tests.fixtures import resolve_resource_path


def test_from_csv_file_valid() -> None:
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
assert (
table.get_column("A").get_value(0) == 1
and table.get_column("B").get_value(0) == 2
)


def test_from_csv_file_invalid() -> None:
with pytest.raises(FileNotFoundError):
Table.from_csv_file(
resolve_resource_path("test_table_from_csv_file_invalid.csv")
)
20 changes: 20 additions & 0 deletions tests/safeds/data/tabular/containers/_table/test_from_json_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from safeds.data.tabular.containers import Table
from tests.fixtures import resolve_resource_path


def test_from_json_file_valid() -> None:
table = Table.from_json_file(
resolve_resource_path("test_table_from_json_file.json")
)
assert (
table.get_column("A").get_value(0) == 1
and table.get_column("B").get_value(0) == 2
)


def test_from_json_file_invalid() -> None:
with pytest.raises(FileNotFoundError):
Table.from_json_file(
resolve_resource_path("test_table_from_json_file_invalid.json")
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def test_from_rows() -> None:
table_expected = Table.from_csv(resolve_resource_path("test_row_table.csv"))
table_expected = Table.from_csv_file(resolve_resource_path("test_row_table.csv"))
rows_is: list[Row] = table_expected.to_rows()
table_is: Table = Table.from_rows(rows_is)

Expand Down
6 changes: 3 additions & 3 deletions tests/safeds/data/tabular/containers/_table/test_get_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@


def test_get_row() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
val = table.get_row(0)
assert val.get_value("A") == 1 and val.get_value("B") == 2


def test_get_row_negative_index() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
with pytest.raises(IndexOutOfBoundsError):
table.get_row(-1)


def test_get_row_out_of_bounds_index() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
with pytest.raises(IndexOutOfBoundsError):
table.get_row(5)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@


def test_has_column_positive() -> None:
table = Table.from_csv(resolve_resource_path("test_table_has_column.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv"))
assert table.has_column("A")


def test_has_column_negative() -> None:
table = Table.from_csv(resolve_resource_path("test_table_has_column.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv"))
assert not table.has_column("C")
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@


def test_keep_columns() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
transformed_table = table.keep_only_columns(["A"])
assert transformed_table.schema.has_column(
"A"
) and not transformed_table.schema.has_column("B")


def test_keep_columns_warning() -> None:
table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv"))
with pytest.raises(UnknownColumnNameError):
table.keep_only_columns(["C"])
16 changes: 0 additions & 16 deletions tests/safeds/data/tabular/containers/_table/test_read_csv.py

This file was deleted.

16 changes: 0 additions & 16 deletions tests/safeds/data/tabular/containers/_table/test_read_json.py

This file was deleted.

8 changes: 6 additions & 2 deletions tests/safeds/data/tabular/containers/_table/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
def test_rename_valid(
name_from: str, name_to: str, column_one: str, column_two: str
) -> None:
table: Table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table: Table = Table.from_csv_file(
resolve_resource_path("test_table_from_csv_file.csv")
)
renamed_table = table.rename_column(name_from, name_to)
assert renamed_table.schema.has_column(column_one)
assert renamed_table.schema.has_column(column_two)
Expand All @@ -27,6 +29,8 @@ def test_rename_valid(
],
)
def test_rename_invalid(name_from: str, name_to: str, error: Exception) -> None:
table: Table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv"))
table: Table = Table.from_csv_file(
resolve_resource_path("test_table_from_csv_file.csv")
)
with pytest.raises(error):
table.rename_column(name_from, name_to)
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
],
)
def test_replace_valid(column_name: str, path: str) -> None:
input_table: Table = Table.from_csv(
input_table: Table = Table.from_csv_file(
resolve_resource_path("test_table_replace_column_input.csv")
)
expected: Table = Table.from_csv(resolve_resource_path(path))
expected: Table = Table.from_csv_file(resolve_resource_path(path))

column = Column(pd.Series(["d", "e", "f"]), column_name)

Expand All @@ -43,7 +43,7 @@ def test_replace_invalid(
column_name: str,
error: type[Exception],
) -> None:
input_table: Table = Table.from_csv(
input_table: Table = Table.from_csv_file(
resolve_resource_path("test_table_replace_column_input.csv")
)
column = Column(pd.Series(column_values), column_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


def test_table_add_column_valid() -> None:
input_table = Table.from_csv(
input_table = Table.from_csv_file(
resolve_resource_path("test_table_add_column_valid_input.csv")
)
expected = Table.from_csv(
expected = Table.from_csv_file(
resolve_resource_path("test_table_add_column_valid_output.csv")
)
column = Column(pd.Series(["a", "b", "c"]), "C")
Expand All @@ -28,7 +28,7 @@ def test_table_add_column_valid() -> None:
def test_table_add_column_(
column_values: list[str], column_name: str, error: type[Exception]
) -> None:
input_table = Table.from_csv(
input_table = Table.from_csv_file(
resolve_resource_path("test_table_add_column_valid_input.csv")
)
column = Column(pd.Series(column_values), column_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[([1, 4], "A", 0), ([2, 5], "B", 1)],
)
def test_to_columns(values: list[int], name: str, index: int) -> None:
table = Table.from_csv(resolve_resource_path("test_column_table.csv"))
table = Table.from_csv_file(resolve_resource_path("test_column_table.csv"))
columns_list: list[Column] = table.to_columns()

column_expected: Column = Column(pd.Series(values, name=name), name)
Expand Down
15 changes: 15 additions & 0 deletions tests/safeds/data/tabular/containers/_table/test_to_csv_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from tempfile import NamedTemporaryFile

import pandas as pd
from safeds.data.tabular.containers import Table


def test_to_csv_file() -> None:
table = Table(pd.DataFrame(data={"col1": ["col1_1"], "col2": ["col2_1"]}))
with NamedTemporaryFile() as tmp_table_file:
tmp_table_file.close()
with open(tmp_table_file.name, "w", encoding="utf-8") as tmp_file:
table.to_csv_file(tmp_file.name)
with open(tmp_table_file.name, "r", encoding="utf-8") as tmp_file:
table_r = Table.from_csv_file(tmp_file.name)
assert table == table_r
15 changes: 15 additions & 0 deletions tests/safeds/data/tabular/containers/_table/test_to_json_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from tempfile import NamedTemporaryFile

import pandas as pd
from safeds.data.tabular.containers import Table


def test_to_json_file() -> None:
table = Table(pd.DataFrame(data={"col1": ["col1_1"], "col2": ["col2_1"]}))
with NamedTemporaryFile() as tmp_table_file:
tmp_table_file.close()
with open(tmp_table_file.name, "w", encoding="utf-8") as tmp_file:
table.to_json_file(tmp_file.name)
with open(tmp_table_file.name, "r", encoding="utf-8") as tmp_file:
table_r = Table.from_json_file(tmp_file.name)
assert table == table_r
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def test_to_rows() -> None:
table = Table.from_csv(resolve_resource_path("test_row_table.csv"))
table = Table.from_csv_file(resolve_resource_path("test_row_table.csv"))
expected_schema: TableSchema = TableSchema(
{
"A": IntColumnType(),
Expand Down
Loading

0 comments on commit ec011e4

Please sign in to comment.