Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
def test_cell_key_val_gen():
    """Unit test for cell_key_val_gen"""

    list_of_lists = [range(10), range(10)]
    gen = __csv.cell_key_val_gen(list_of_lists, (100, 100, 10))
    for row, col, value in gen:
        assert col == value
Ejemplo n.º 3
0
def test_cell_key_val_gen():
    """Unit test for cell_key_val_gen"""

    list_of_lists = [range(10), range(10)]
    gen = __csv.cell_key_val_gen(list_of_lists, (100, 100, 10))
    for row, col, value in gen:
        assert col == value
Ejemplo n.º 4
0
    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()