From 6d7dacd67e34dd403e61ab793bda5b5e63363540 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Wed, 1 May 2024 18:01:45 +0200 Subject: [PATCH 1/4] feat: callback `Row.sort_column` takes four parameters instead of two tuples --- src/safeds/data/tabular/containers/_row.py | 20 +++++++++++++++---- .../data/tabular/containers/test_row.py | 6 +++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/safeds/data/tabular/containers/_row.py b/src/safeds/data/tabular/containers/_row.py index d34b5385e..a8b1f6c83 100644 --- a/src/safeds/data/tabular/containers/_row.py +++ b/src/safeds/data/tabular/containers/_row.py @@ -476,13 +476,20 @@ def get_column_type(self, column_name: str) -> ColumnType: def sort_columns( self, - comparator: Callable[[tuple, tuple], int] = lambda col1, col2: (col1[0] > col2[0]) - (col1[0] < col2[0]), + comparator: Callable[[str, Any, str, Any], int] = + lambda name_1, _value_1, name_2, _value_2: (name_1[0] > name_2[0]) - (name_1[0] < name_2[0]), ) -> Row: """ Sort the columns of a `Row` with the given comparator and return a new `Row`. - The original row is not modified. The comparator is a function that takes two tuples of (ColumnName, - Value) `col1` and `col2` and returns an integer: + The original row is not modified. The comparator is a function with four parameters: + + * `name_1` is the name of the first column. + * `value_1` is the value of the first column. + * `name_2` is the name of the second column. + * `value_2` is the value of the second column. + + It should return an integer, indicating the desired order of the columns: * If `col1` should be ordered before `col2`, the function should return a negative number. * If `col1` should be ordered after `col2`, the function should return a positive number. @@ -500,7 +507,12 @@ def sort_columns( new_row: A new row with sorted columns. """ - sorted_row_dict = dict(sorted(self.to_dict().items(), key=functools.cmp_to_key(comparator))) + sorted_row_dict = dict( + sorted( + self.to_dict().items(), + key=functools.cmp_to_key(lambda col1, col2: comparator(col1[0], col1[1], col2[0], col2[1])), + ), + ) return Row.from_dict(sorted_row_dict) # ------------------------------------------------------------------------------------------------------------------ diff --git a/tests/safeds/data/tabular/containers/test_row.py b/tests/safeds/data/tabular/containers/test_row.py index acb6f9e99..a54c3c356 100644 --- a/tests/safeds/data/tabular/containers/test_row.py +++ b/tests/safeds/data/tabular/containers/test_row.py @@ -553,12 +553,12 @@ class TestSortColumns: [ ( Row({"b": 1, "a": 2}), - lambda col1, col2: (col1[0] > col2[0]) - (col1[0] < col2[0]), + lambda name_1, _value_1, name_2, _value_2: (name_1 > name_2) - (name_1 < name_2), Row({"a": 2, "b": 1}), ), ( Row({"a": 2, "b": 1}), - lambda col1, col2: (col2[0] > col1[0]) - (col2[0] < col1[0]), + lambda name_1, _value_1, name_2, _value_2: (name_2 > name_1) - (name_2 < name_1), Row({"b": 1, "a": 2}), ), (Row(), lambda col1, col2: (col1[0] > col2[0]) - (col1[0] < col2[0]), Row()), @@ -569,7 +569,7 @@ class TestSortColumns: "empty rows", ], ) - def test_should_sort_columns(self, row: Row, comparator: Callable[[tuple, tuple], int], expected: Row) -> None: + def test_should_sort_columns(self, row: Row, comparator: Callable[[str, Any, str, Any], int], expected: Row) -> None: row = row.sort_columns(comparator) assert row == expected From e2ed568eb4e1ee8e99b6abace22d01fb3c6016c7 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Wed, 1 May 2024 18:06:32 +0200 Subject: [PATCH 2/4] style: fix mypy error --- src/safeds/data/tabular/containers/_row.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/safeds/data/tabular/containers/_row.py b/src/safeds/data/tabular/containers/_row.py index a8b1f6c83..d0b35c74f 100644 --- a/src/safeds/data/tabular/containers/_row.py +++ b/src/safeds/data/tabular/containers/_row.py @@ -507,10 +507,13 @@ def sort_columns( new_row: A new row with sorted columns. """ + def cmp(column_1: tuple[str, Any], column_2: tuple[str, Any]) -> int: + return comparator(column_1[0], column_1[1], column_2[0], column_2[1]) + sorted_row_dict = dict( sorted( self.to_dict().items(), - key=functools.cmp_to_key(lambda col1, col2: comparator(col1[0], col1[1], col2[0], col2[1])), + key=functools.cmp_to_key(cmp), ), ) return Row.from_dict(sorted_row_dict) From 818567a5669d201bc48bb3a39075e615932878b4 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Wed, 1 May 2024 16:08:13 +0000 Subject: [PATCH 3/4] style: apply automated linter fixes --- src/safeds/data/tabular/containers/_row.py | 7 +++++-- tests/safeds/data/tabular/containers/test_row.py | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/safeds/data/tabular/containers/_row.py b/src/safeds/data/tabular/containers/_row.py index d0b35c74f..5a83d5bbb 100644 --- a/src/safeds/data/tabular/containers/_row.py +++ b/src/safeds/data/tabular/containers/_row.py @@ -476,8 +476,10 @@ def get_column_type(self, column_name: str) -> ColumnType: def sort_columns( self, - comparator: Callable[[str, Any, str, Any], int] = - lambda name_1, _value_1, name_2, _value_2: (name_1[0] > name_2[0]) - (name_1[0] < name_2[0]), + comparator: Callable[[str, Any, str, Any], int] = lambda name_1, _value_1, name_2, _value_2: ( + name_1[0] > name_2[0] + ) + - (name_1[0] < name_2[0]), ) -> Row: """ Sort the columns of a `Row` with the given comparator and return a new `Row`. @@ -507,6 +509,7 @@ def sort_columns( new_row: A new row with sorted columns. """ + def cmp(column_1: tuple[str, Any], column_2: tuple[str, Any]) -> int: return comparator(column_1[0], column_1[1], column_2[0], column_2[1]) diff --git a/tests/safeds/data/tabular/containers/test_row.py b/tests/safeds/data/tabular/containers/test_row.py index a54c3c356..6aaa529de 100644 --- a/tests/safeds/data/tabular/containers/test_row.py +++ b/tests/safeds/data/tabular/containers/test_row.py @@ -569,7 +569,9 @@ class TestSortColumns: "empty rows", ], ) - def test_should_sort_columns(self, row: Row, comparator: Callable[[str, Any, str, Any], int], expected: Row) -> None: + def test_should_sort_columns( + self, row: Row, comparator: Callable[[str, Any, str, Any], int], expected: Row, + ) -> None: row = row.sort_columns(comparator) assert row == expected From e0ef9690f1daaa205264d3b9e298c4ed4987fd47 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Wed, 1 May 2024 16:09:50 +0000 Subject: [PATCH 4/4] style: apply automated linter fixes --- tests/safeds/data/tabular/containers/test_row.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/safeds/data/tabular/containers/test_row.py b/tests/safeds/data/tabular/containers/test_row.py index 6aaa529de..292c0486b 100644 --- a/tests/safeds/data/tabular/containers/test_row.py +++ b/tests/safeds/data/tabular/containers/test_row.py @@ -570,7 +570,10 @@ class TestSortColumns: ], ) def test_should_sort_columns( - self, row: Row, comparator: Callable[[str, Any, str, Any], int], expected: Row, + self, + row: Row, + comparator: Callable[[str, Any, str, Any], int], + expected: Row, ) -> None: row = row.sort_columns(comparator) assert row == expected