Skip to content

Commit bc73a6c

Browse files
zzrilmegalinter-botMarsmaennchen221patrikguempelsibre28
authored
feat: Maintain tagging in methods inherited from Table class (#332)
Closes #58. ### Summary of Changes * Overrode methods in `TaggedTable` inherited from `Table` to maintain tagging. * Introduced new exceptions to be thrown when the overriden method cannot be applied * Added tests. --------- Co-authored-by: megalinter-bot <[email protected]> Co-authored-by: Alexander Gréus <[email protected]> Co-authored-by: Alexander <[email protected]> Co-authored-by: patrikguempel <[email protected]> Co-authored-by: Simon Breuer <[email protected]>
1 parent 8e9065c commit bc73a6c

40 files changed

+2845
-128
lines changed

src/safeds/data/tabular/containers/_table.py

+41-6
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,22 @@ def summary(self) -> Table:
797797
# Transformations
798798
# ------------------------------------------------------------------------------------------------------------------
799799

800+
# This method is meant as a way to "cast" instances of subclasses of `Table` to a proper `Table`, dropping any
801+
# additional constraints that might have to hold in the subclass.
802+
# Override accordingly in subclasses.
803+
def _as_table(self: Table) -> Table:
804+
"""
805+
Transform the table to an instance of the Table class.
806+
807+
The original table is not modified.
808+
809+
Returns
810+
-------
811+
table: Table
812+
The table, as an instance of the Table class.
813+
"""
814+
return self
815+
800816
def add_column(self, column: Column) -> Table:
801817
"""
802818
Return the original table with the provided column attached at the end.
@@ -888,8 +904,9 @@ def add_row(self, row: Row) -> Table:
888904
"""
889905
Add a row to the table.
890906
907+
If the table happens to be empty beforehand, respective columns will be added automatically.
908+
891909
This table is not modified.
892-
If the table happens to be empty beforehand, respective features will be added automatically.
893910
894911
Parameters
895912
----------
@@ -1077,6 +1094,8 @@ def keep_only_columns(self, column_names: list[str]) -> Table:
10771094
------
10781095
UnknownColumnNameError
10791096
If any of the given columns does not exist.
1097+
IllegalSchemaModificationError
1098+
If removing the columns would violate an invariant in the subclass.
10801099
10811100
Examples
10821101
--------
@@ -1120,6 +1139,8 @@ def remove_columns(self, column_names: list[str]) -> Table:
11201139
------
11211140
UnknownColumnNameError
11221141
If any of the given columns does not exist.
1142+
IllegalSchemaModificationError
1143+
If removing the columns would violate an invariant in the subclass.
11231144
11241145
Examples
11251146
--------
@@ -1158,6 +1179,11 @@ def remove_columns_with_missing_values(self) -> Table:
11581179
table : Table
11591180
A table without the columns that contain missing values.
11601181
1182+
Raises
1183+
------
1184+
IllegalSchemaModificationError
1185+
If removing the columns would violate an invariant in the subclass.
1186+
11611187
Examples
11621188
--------
11631189
>>> from safeds.data.tabular.containers import Table
@@ -1182,6 +1208,11 @@ def remove_columns_with_non_numerical_values(self) -> Table:
11821208
table : Table
11831209
A table without the columns that contain non-numerical values.
11841210
1211+
Raises
1212+
------
1213+
IllegalSchemaModificationError
1214+
If removing the columns would violate an invariant in the subclass.
1215+
11851216
Examples
11861217
--------
11871218
>>> from safeds.data.tabular.containers import Table
@@ -1331,7 +1362,9 @@ def rename_column(self, old_name: str, new_name: str) -> Table:
13311362

13321363
def replace_column(self, old_column_name: str, new_columns: list[Column]) -> Table:
13331364
"""
1334-
Return a copy of the table with the specified old column replaced by a list of new columns. Keeps the order of columns.
1365+
Return a copy of the table with the specified old column replaced by a list of new columns.
1366+
1367+
The order of columns is kept.
13351368
13361369
This table is not modified.
13371370
@@ -1352,12 +1385,12 @@ def replace_column(self, old_column_name: str, new_columns: list[Column]) -> Tab
13521385
------
13531386
UnknownColumnNameError
13541387
If the old column does not exist.
1355-
13561388
DuplicateColumnNameError
13571389
If at least one of the new columns already exists and the existing column is not affected by the replacement.
1358-
13591390
ColumnSizeError
13601391
If the size of at least one of the new columns does not match the amount of rows.
1392+
IllegalSchemaModificationError
1393+
If replacing the column would violate an invariant in the subclass.
13611394
13621395
Examples
13631396
--------
@@ -1475,7 +1508,7 @@ def sort_columns(
14751508
"""
14761509
Sort the columns of a `Table` with the given comparator and return a new `Table`.
14771510
1478-
The original table is not modified. The comparator is a function that takes two columns `col1` and `col2` and
1511+
The comparator is a function that takes two columns `col1` and `col2` and
14791512
returns an integer:
14801513
14811514
* If `col1` should be ordered before `col2`, the function should return a negative number.
@@ -1519,7 +1552,7 @@ def sort_rows(self, comparator: Callable[[Row, Row], int]) -> Table:
15191552
"""
15201553
Sort the rows of a `Table` with the given comparator and return a new `Table`.
15211554
1522-
The original table is not modified. The comparator is a function that takes two rows `row1` and `row2` and
1555+
The comparator is a function that takes two rows `row1` and `row2` and
15231556
returns an integer:
15241557
15251558
* If `row1` should be ordered before `row2`, the function should return a negative number.
@@ -1695,6 +1728,8 @@ def transform_table(self, transformer: TableTransformer) -> Table:
16951728
------
16961729
TransformerNotFittedError
16971730
If the transformer has not been fitted yet.
1731+
IllegalSchemaModificationError
1732+
If replacing the column would violate an invariant in the subclass.
16981733
16991734
Examples
17001735
--------

0 commit comments

Comments
 (0)