Make DataTable.data private (it's now _data)

This commit is contained in:
Darren Burns
2023-02-14 11:00:43 +00:00
parent 27503b15bf
commit 8a6e44b010
2 changed files with 17 additions and 17 deletions

View File

@@ -427,7 +427,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
classes: str | None = None, classes: str | None = None,
) -> None: ) -> None:
super().__init__(name=name, id=id, classes=classes) super().__init__(name=name, id=id, classes=classes)
self.data: dict[RowKey, dict[ColumnKey, CellType]] = {} self._data: dict[RowKey, dict[ColumnKey, CellType]] = {}
"""Contains the cells of the table, indexed by row key and column key. """Contains the cells of the table, indexed by row key and column key.
The final positioning of a cell on screen cannot be determined solely by this The final positioning of a cell on screen cannot be determined solely by this
structure. Instead, we must check _row_locations and _column_locations to find structure. Instead, we must check _row_locations and _column_locations to find
@@ -562,7 +562,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
column_key = ColumnKey(column_key) column_key = ColumnKey(column_key)
try: try:
self.data[row_key][column_key] = value self._data[row_key][column_key] = value
except KeyError: except KeyError:
raise CellDoesNotExist( raise CellDoesNotExist(
f"No cell exists for row_key={row_key!r}, column_key={column_key!r}." f"No cell exists for row_key={row_key!r}, column_key={column_key!r}."
@@ -598,7 +598,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
order they currently appear on screen.""" order they currently appear on screen."""
for row_metadata in self.ordered_rows: for row_metadata in self.ordered_rows:
row_key = row_metadata.key row_key = row_metadata.key
row = self.data[row_key] row = self._data[row_key]
yield row[column_key] yield row[column_key]
def get_cell(self, row_key: RowKey, column_key: ColumnKey) -> CellType: def get_cell(self, row_key: RowKey, column_key: ColumnKey) -> CellType:
@@ -612,7 +612,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
The value of the cell identified by the row and column keys. The value of the cell identified by the row and column keys.
""" """
try: try:
cell_value = self.data[row_key][column_key] cell_value = self._data[row_key][column_key]
except KeyError: except KeyError:
raise CellDoesNotExist( raise CellDoesNotExist(
f"No cell exists for row_key={row_key!r}, column_key={column_key!r}." f"No cell exists for row_key={row_key!r}, column_key={column_key!r}."
@@ -739,7 +739,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
def _highlight_row(self, row_index: int) -> None: def _highlight_row(self, row_index: int) -> None:
"""Apply highlighting to the row at the given index, and post event.""" """Apply highlighting to the row at the given index, and post event."""
self.refresh_row(row_index) self.refresh_row(row_index)
is_valid_row = row_index < len(self.data) is_valid_row = row_index < len(self._data)
if is_valid_row: if is_valid_row:
row_key = self._row_locations.get_key(row_index) row_key = self._row_locations.get_key(row_index)
self.post_message_no_wait( self.post_message_no_wait(
@@ -805,7 +805,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
console = self.app.console console = self.app.console
label_width = measure(console, column.label, 1) label_width = measure(console, column.label, 1)
content_width = column.content_width content_width = column.content_width
cell_value = self.data[row_key][column_key] cell_value = self._data[row_key][column_key]
new_content_width = measure(console, default_cell_formatter(cell_value), 1) new_content_width = measure(console, default_cell_formatter(cell_value), 1)
@@ -896,7 +896,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
""" """
self._clear_caches() self._clear_caches()
self._y_offsets.clear() self._y_offsets.clear()
self.data.clear() self._data.clear()
self.rows.clear() self.rows.clear()
if columns: if columns:
self.columns.clear() self.columns.clear()
@@ -977,7 +977,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
row_index = self.row_count row_index = self.row_count
# Map the key of this row to its current index # Map the key of this row to its current index
self._row_locations[row_key] = row_index self._row_locations[row_key] = row_index
self.data[row_key] = { self._data[row_key] = {
column.key: cell column.key: cell
for column, cell in zip_longest(self.ordered_columns, cells) for column, cell in zip_longest(self.ordered_columns, cells)
} }
@@ -1178,7 +1178,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
# Ensure we order the cells in the row based on current column ordering # Ensure we order the cells in the row based on current column ordering
row_key = self._row_locations.get_key(row_index) row_key = self._row_locations.get_key(row_index)
cell_mapping: dict[ColumnKey, CellType] = self.data.get(row_key, {}) cell_mapping: dict[ColumnKey, CellType] = self._data.get(row_key, {})
ordered_row: list[CellType] = [] ordered_row: list[CellType] = []
for column in ordered_columns: for column in ordered_columns:
@@ -1513,7 +1513,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
return result return result
ordered_rows = sorted( ordered_rows = sorted(
self.data.items(), key=sort_by_column_keys, reverse=reverse self._data.items(), key=sort_by_column_keys, reverse=reverse
) )
self._row_locations = TwoWayDict( self._row_locations = TwoWayDict(
{key: new_index for new_index, (key, _) in enumerate(ordered_rows)} {key: new_index for new_index, (key, _) in enumerate(ordered_rows)}

View File

@@ -157,10 +157,10 @@ async def test_add_rows():
row_keys = table.add_rows(ROWS) row_keys = table.add_rows(ROWS)
# We're given a key for each row # We're given a key for each row
assert len(row_keys) == len(ROWS) assert len(row_keys) == len(ROWS)
assert len(row_keys) == len(table.data) assert len(row_keys) == len(table._data)
assert table.row_count == len(ROWS) assert table.row_count == len(ROWS)
# Each key can be used to fetch a row from the DataTable # Each key can be used to fetch a row from the DataTable
assert all(key in table.data for key in row_keys) assert all(key in table._data for key in row_keys)
async def test_add_rows_user_defined_keys(): async def test_add_rows_user_defined_keys():
@@ -179,14 +179,14 @@ async def test_add_rows_user_defined_keys():
# Ensure the data in the table is mapped as expected # Ensure the data in the table is mapped as expected
first_row = {key_a: ROWS[0][0], key_b: ROWS[0][1]} first_row = {key_a: ROWS[0][0], key_b: ROWS[0][1]}
assert table.data[algernon_key] == first_row assert table._data[algernon_key] == first_row
assert table.data["algernon"] == first_row assert table._data["algernon"] == first_row
second_row = {key_a: ROWS[1][0], key_b: ROWS[1][1]} second_row = {key_a: ROWS[1][0], key_b: ROWS[1][1]}
assert table.data["charlie"] == second_row assert table._data["charlie"] == second_row
third_row = {key_a: ROWS[2][0], key_b: ROWS[2][1]} third_row = {key_a: ROWS[2][0], key_b: ROWS[2][1]}
assert table.data[auto_key] == third_row assert table._data[auto_key] == third_row
first_row = Row(algernon_key, height=1) first_row = Row(algernon_key, height=1)
assert table.rows[algernon_key] == first_row assert table.rows[algernon_key] == first_row
@@ -260,7 +260,7 @@ async def test_clear():
assert table.hover_coordinate == Coordinate(0, 0) assert table.hover_coordinate == Coordinate(0, 0)
# Ensure that the table has been cleared # Ensure that the table has been cleared
assert table.data == {} assert table._data == {}
assert table.rows == {} assert table.rows == {}
assert table.row_count == 0 assert table.row_count == 0
assert len(table.columns) == 1 assert len(table.columns) == 1