Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Table.delete_row method #610

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/magicgui/widgets/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,39 @@
for row, d in enumerate(data):
self._set_rowi(row, d)

def delete_row(
self,
*,
index: int | Sequence[int] | None = None,
header: Any | Sequence[Any] | None = None,
) -> None:
"""Delete row(s) by index or header.

Parameters
----------
index : int or Sequence[int], optional
Index or indices of row(s) to delete.
header : Any or Sequence[Any], optional
Header or headers of row(s) to delete.
"""
indices: set[int] = set()
if index is not None:
if isinstance(index, Sequence):
indices.update(index)

Check warning on line 290 in src/magicgui/widgets/_table.py

View check run for this annotation

Codecov / codecov/patch

src/magicgui/widgets/_table.py#L290

Added line #L290 was not covered by tests
else:
indices.add(index)
if header is not None:
if isinstance(header, str) or not isinstance(header, Sequence):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, I needed a double-take to understand why this wasn't just not isinstance(header, Sequence). That's annoying 😂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, classic :) string is a sequence but not "that" kind :)

header = (header,)
row_headers = self.row_headers
for h in header:
try:
indices.add(row_headers.index(h))
except ValueError as e:
raise KeyError(f"{h!r} is not a valid row header") from e

Check warning on line 301 in src/magicgui/widgets/_table.py

View check run for this annotation

Codecov / codecov/patch

src/magicgui/widgets/_table.py#L300-L301

Added lines #L300 - L301 were not covered by tests
for i in sorted(indices, reverse=True):
self._del_rowi(i)

@property
def data(self) -> DataView:
"""Return DataView object for this table."""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,12 @@ def test_item_delegate(qapp):
data = ["1.2", "1.23456789", "0.000123", "1234567", "0.0", "1", "s"]
results = [_format_number(v, 4) for v in data]
assert results == ["1.2000", "1.2346", "1.230e-04", "1.235e+06", "0.0000", "1", "s"]


def test_row_delete(qapp) -> None:
table = Table(value=_TABLE_DATA["split"])
assert table.data.to_list() == [[1, 2, 3], [4, 5, 6]]
table.delete_row(index=0)
assert table.data.to_list() == [[4, 5, 6]]
table.delete_row(header="r2")
assert table.data.to_list() == []