views.clean_data.dataprep_logic
Core utilities used by the DataPrep window.
This module contains the logic used by the DataPrep view. It loads table data, creates repeating selections and manages copy/paste patterns. Everything here is independent of Qt so the functions can be tested on their own.
DataPrepLogic
Helper object that performs all non-Qt work for DataPrep.
The object provides small, easily testable methods for loading data, calculating "every Nth" selections and keeping a copy buffer for paste operations. At the end the table can be turned back into a pandas.DataFrame.
Attributes:
| Name | Type | Description |
|---|---|---|
_df_source |
Internal DataFrame holding the source data. |
|
_copy_pattern |
List of tuples storing copied cell positions and values. |
Source code in src/views/clean_data/dataprep_logic.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
__init__()
Initialize internal buffers for later operations.
Steps
- Create an empty DataFrame placeholder for the source table.
- Prepare an empty list that stores copied cell patterns.
Source code in src/views/clean_data/dataprep_logic.py
46 47 48 49 50 51 52 53 54 55 56 | |
compute_selection(row_n, col_n, row_shift, col_shift, mode, row_count, col_count)
Build a set of cell coordinates according to the selection settings.
Steps
- Determine base row and column indices based on row_n and col_n.
- Apply row_shift and col_shift while keeping indices within bounds.
- Combine rows and columns either with logical 'AND' or 'OR' depending on mode.
- Return the resulting coordinate set.
Source code in src/views/clean_data/dataprep_logic.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
convert_table_to_dataframe(table_data, column_count)
staticmethod
Convert the raw table data back into a pandas DataFrame.
Steps
- Slice each row to column_count elements to drop extras.
- Create the DataFrame with numeric column labels as strings.
Source code in src/views/clean_data/dataprep_logic.py
286 287 288 289 290 291 292 293 294 295 296 | |
get_paste_data(anchor_row, anchor_col, table_data)
Insert the previously copied cells into a cloned table.
Parameters anchor_row (int) : Zero‐based row index at which to begin pasting the copy buffer. anchor_col (int) : Zero‐based column index at which to begin pasting the copy buffer. table_data (list[list[str]]) : The original 2D table data into which the copy buffer will be pasted.
Returns
list[list[str]]
: A deep‐copied version of table_data with the copied cells inserted
at the specified anchor, leaving the original data unmodified.
Paste Algorithm
1. If the copy buffer is empty, returns the original table_data unchanged.
2. Creates a deep copy of table_data for safe in-place modifications.
3. Determines the minimal row and column indices in the copy buffer
to compute relative offsets.
4. For each entry (r, c, value) in the copy buffer:
- Computes target coordinates
python
target_row = anchor_row + (r - min_row)
target_col = anchor_col + (c - min_col)
- If (target_row, target_col) lies within bounds, writes value there.
5. Returns the modified copy.
Example
# Suppose _copy_pattern = [(2,1,"X"), (3,2,"Y")]
table = [
["", "", ""],
["", "", ""],
["", "", ""],
["", "", ""],
]
# Paste at anchor (0,0) yields:
# [
# ["", "X", ""],
# ["", "", "Y"],
# ["", "", ""],
# ["", "", ""],
# ]
new_table = instance.get_paste_data(0, 0, table)
Source code in src/views/clean_data/dataprep_logic.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
load_csv(path)
Read a CSV file from disk and convert it into the internal DataFrame.
Steps
- Try to guess the delimiter by reading a small sample with csv.Sniffer.
- If that fails, fall back to ";" or "," based on sample content.
- Load the entire file with pandas, forcing all values to strings and disabling header parsing.
- Forward the resulting DataFrame to load_dataframe for renaming of columns.
Source code in src/views/clean_data/dataprep_logic.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
load_dataframe(df)
Copy the incoming DataFrame and give it numeric column names.
Steps
- Duplicate the provided DataFrame so the original remains unchanged.
- Rename the columns sequentially as strings ("0", "1", ...).
- Store the sanitized DataFrame for later use.
- Return the sanitized copy for display in the table widget.
Source code in src/views/clean_data/dataprep_logic.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
pattern_matches(dest_coordinates)
Verify that a selection matches the stored copy pattern layout.
Steps
- Return False immediately if no pattern was stored.
- Compare relative offsets of dest_coordinates and _copy_pattern.
- Return True only when both offset sets are identical.
Source code in src/views/clean_data/dataprep_logic.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
prepare_copy_data(selected, table_data)
Remember the currently selected cells for later paste operations.
Steps
- If selected is empty, clear any existing copy pattern.
- Otherwise, build _copy_pattern as a list of (row, col, value) tuples.
Source code in src/views/clean_data/dataprep_logic.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |