Skip to content

views.clean_data.table_logic

autosize_columns(widget)

Resize all columns so that cell contents become visible.

Source code in src/views/clean_data/table_logic.py
91
92
93
def autosize_columns(widget: QTableWidget) -> None:
    """Resize all columns so that cell contents become visible."""
    widget.resizeColumnsToContents()

delete_cols(widget, cols)

Remove all columns listed in cols.

Steps
  1. Iterate over cols in reverse order so indices remain valid.
  2. Remove each column from the widget.
  3. Resize columns afterward for cleanliness.
Source code in src/views/clean_data/table_logic.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def delete_cols(widget: QTableWidget, cols: set[int]) -> None:
    """
    Remove all columns listed in cols.

    Steps:
      1. Iterate over cols in reverse order so indices remain valid.
      2. Remove each column from the widget.
      3. Resize columns afterward for cleanliness.
    """
    # Remove each specified column
    for c in sorted(cols, reverse=True):
        widget.removeColumn(c)

    # Adjust columns after deletion
    autosize_columns(widget)

delete_rows(widget, rows)

Remove all rows listed in rows.

Steps
  1. Iterate over rows in reverse order so indices remain valid.
  2. Remove each row from the widget.
  3. Resize columns afterward for cleanliness.
Source code in src/views/clean_data/table_logic.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def delete_rows(widget: QTableWidget, rows: set[int]) -> None:
    """
    Remove all rows listed in rows.

    Steps:
      1. Iterate over rows in reverse order so indices remain valid.
      2. Remove each row from the widget.
      3. Resize columns afterward for cleanliness.
    """
    # Remove each specified row
    for r in sorted(rows, reverse=True):
        widget.removeRow(r)

    # Adjust columns after deletion
    autosize_columns(widget)

fill_table(widget, data)

Replace the entire table with the provided 2D list.

Steps
  1. Clear any existing contents from widget.
  2. Resize the table to match data.
  3. Insert each cell value into the table widget.
  4. Resize columns so values are fully visible.
Source code in src/views/clean_data/table_logic.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def fill_table(widget: QTableWidget, data: List[List[str]]) -> None:
    """
    Replace the entire table with the provided 2D list.

    Steps:
      1. Clear any existing contents from widget.
      2. Resize the table to match data.
      3. Insert each cell value into the table widget.
      4. Resize columns so values are fully visible.
    """
    # Determine dimensions of new table
    rows = len(data)
    cols = len(data[0]) if rows else 0

    # Clear existing contents and set new size
    widget.clear()
    widget.setRowCount(rows)
    widget.setColumnCount(cols)
    widget.setHorizontalHeaderLabels([str(i) for i in range(cols)])

    # Populate each cell with the provided value
    for r, row in enumerate(data):
        for c, val in enumerate(row):
            widget.setItem(r, c, QTableWidgetItem(val))

    # Adjust column widths to fit content
    widget.resizeColumnsToContents()

get_table_data(widget)

Return the table contents as a list of rows.

Steps
  1. Iterate over every row and column of widget.
  2. Collect the text of each cell (empty string if no item).
  3. Assemble and return the resulting two-dimensional list.
Source code in src/views/clean_data/table_logic.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def get_table_data(widget: QTableWidget) -> List[List[str]]:
    """
    Return the table contents as a list of rows.

    Steps:
      1. Iterate over every row and column of widget.
      2. Collect the text of each cell (empty string if no item).
      3. Assemble and return the resulting two-dimensional list.
    """
    data: List[List[str]] = []
    rows = widget.rowCount()
    cols = widget.columnCount()

    # Extract text from each cell
    for r in range(rows):
        row_vals: List[str] = []
        for c in range(cols):
            item = widget.item(r, c)
            row_vals.append(item.text() if item else "")
        data.append(row_vals)

    return data

insert_col(widget)

Insert an empty column at the current position.

Steps
  1. Determine the current column index; append to end if none selected.
  2. Insert a new empty column and resize columns.
Source code in src/views/clean_data/table_logic.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def insert_col(widget: QTableWidget) -> None:
    """
    Insert an empty column at the current position.

    Steps:
      1. Determine the current column index; append to end if none selected.
      2. Insert a new empty column and resize columns.
    """
    # Choose insertion index
    c = widget.currentColumn()
    widget.insertColumn(c if c >= 0 else widget.columnCount())

    # Ensure columns fit after insertion
    autosize_columns(widget)

insert_row(widget)

Insert an empty row at the current position.

Steps
  1. Determine the current row index; append to bottom if no selection.
  2. Insert a new empty row and resize columns.
Source code in src/views/clean_data/table_logic.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def insert_row(widget: QTableWidget) -> None:
    """
    Insert an empty row at the current position.

    Steps:
      1. Determine the current row index; append to bottom if no selection.
      2. Insert a new empty row and resize columns.
    """
    # Choose insertion index
    r = widget.currentRow()
    widget.insertRow(r if r >= 0 else widget.rowCount())

    # Ensure columns fit after insertion
    autosize_columns(widget)

transpose_data(data)

Return a transposed copy of the provided data.

Steps
  1. Use zip(*data) to swap rows and columns.
  2. Convert the tuples back into lists.
Source code in src/views/clean_data/table_logic.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def transpose_data(data: List[List[str]]) -> List[List[str]]:
    """
    Return a transposed copy of the provided data.

    Steps:
      1. Use zip(*data) to swap rows and columns.
      2. Convert the tuples back into lists.
    """
    # Return empty list if no data
    if not data:
        return []

    # Transpose via unpacking and re-listing
    return [list(col) for col in zip(*data)]

transpose_table(widget)

Transpose the table if it contains any data.

Steps
  1. Extract current data with get_table_data.
  2. If the table is not empty, fill it again with the transposed data.
Source code in src/views/clean_data/table_logic.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def transpose_table(widget: QTableWidget) -> None:
    """
    Transpose the table if it contains any data.

    Steps:
      1. Extract current data with get_table_data.
      2. If the table is not empty, fill it again with the transposed data.
    """
    # Read current table contents
    data = get_table_data(widget)
    if not data:
        return

    # Refill with transposed data
    fill_table(widget, transpose_data(data))

update_table_data(widget, data)

Update the table in place to match the given data.

Steps
  1. Iterate through data row by row.
  2. Create missing QTableWidgetItem objects as needed.
  3. Update text of existing items only when it differs to avoid flicker.
  4. Call 'autosize_columns' afterward to adjust column widths.
Source code in src/views/clean_data/table_logic.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def update_table_data(widget: QTableWidget, data: List[List[str]]) -> None:
    """
    Update the table in place to match the given data.

    Steps:
      1. Iterate through data row by row.
      2. Create missing QTableWidgetItem objects as needed.
      3. Update text of existing items only when it differs to avoid flicker.
      4. Call 'autosize_columns' afterward to adjust column widths.
    """
    # Synchronize each cell with the new data
    for r, row in enumerate(data):
        for c, val in enumerate(row):
            itm = widget.item(r, c)
            if itm is None:
                itm = QTableWidgetItem()
                widget.setItem(r, c, itm)
            if itm.text() != val:
                itm.setText(val)

    # Adjust columns after updates
    autosize_columns(widget)