def fill_cells(self, dialect, has_header, choices=True): """Fills the grid for preview of csv data Parameters ---------- dialect: csv,dialect \tDialect used for csv reader choices: Bool \tCreate and show choices """ # Get columns from csv first_line = get_first_line(self.csvfilepath, dialect) self.shape[1] = no_cols = len(first_line) if no_cols > self.GetNumberCols(): missing_cols = no_cols - self.GetNumberCols() self.AppendCols(missing_cols) elif no_cols < self.GetNumberCols(): obsolete_cols = self.GetNumberCols() - no_cols self.DeleteCols(pos=no_cols - 1, numCols=obsolete_cols) # Retrieve type choices digest_keys = self.get_digest_keys() # Is a header present? --> Import as strings in first line if has_header: for i, header in enumerate(first_line): self.SetCellValue(0, i, header) if choices: # Add Choices for col in xrange(self.shape[1]): choice_renderer = ChoiceRenderer(self) choice_editor = wx.grid.GridCellChoiceEditor( self.digest_types.keys(), False) self.SetCellRenderer(has_header, col, choice_renderer) self.SetCellEditor(has_header, col, choice_editor) self.SetCellValue(has_header, col, digest_keys[col]) # Fill in the rest of the lines self.dtypes = [] for key in self.get_digest_keys(): try: self.dtypes.append(self.digest_types[key]) except KeyError: self.dtypes.append(types.NoneType) topleft = (has_header + 1, 0) digest_gen = csv_digest_gen(self.csvfilepath, dialect, has_header, self.dtypes) for row, col, val in cell_key_val_gen(digest_gen, self.shape, topleft): self.SetCellValue(row, col, val) self.Refresh()
def test_get_first_line(filepath, first_line): """Unit test for get_first_line""" dialect, __header = __csv.sniff(filepath) __first_line = __csv.get_first_line(filepath, dialect) assert __first_line == first_line