def __init__(self, column_index, formatter): """Constructor :param column_index: to which column or what columns to apply the formatter :param formatter: the target format, or a custom functional formatter """ self.indices = column_index if isinstance(column_index, str): def func(r, c, v): return c == column_index elif isinstance(column_index, list): if len(column_index) == 0: raise IndexError(MESSAGE_DATA_ERROR_EMPTY_COLUMN_LIST) if is_array_type(column_index, str): def func(r, c, v): return c in column_index else: raise IndexError(MESSAGE_DATA_ERROR_COLUMN_LIST_STRING_TYPE) else: raise NotImplementedError("%s is not supported" % type(column_index)) Formatter.__init__(self, func, formatter)
def extend_rows(self, rows): """Inserts two dimensional data after the bottom row""" if isinstance(rows, list): if compact.is_array_type(rows, list): for r in rows: self._extend_row(r) else: self._extend_row(rows) self.__width, self.__array = uniform(self.__array) else: raise TypeError("Cannot use %s" % type(rows))
def select(self, indices): """Delete row indices other than specified Examples: >>> import pyexcel as pe >>> data = [[1],[2],[3],[4],[5],[6],[7],[9]] >>> sheet = pe.Sheet(data) >>> sheet pyexcel sheet: +---+ | 1 | +---+ | 2 | +---+ | 3 | +---+ | 4 | +---+ | 5 | +---+ | 6 | +---+ | 7 | +---+ | 9 | +---+ >>> sheet.row.select([1,2,3,5]) >>> sheet pyexcel sheet: +---+ | 2 | +---+ | 3 | +---+ | 4 | +---+ | 6 | +---+ """ new_indices = [] if compact.is_array_type(indices, str): new_indices = utils.names_to_indices(indices, self._ref.rownames) else: new_indices = indices to_remove = [] for index in self._ref.row_range(): if index not in new_indices: to_remove.append(index) self._ref.filter(row_indices=to_remove)
def extend_columns(self, columns): """Inserts two dimensional data after the rightmost column This is how it works: Given:: s s s t t Get:: s s s + t t """ if not isinstance(columns, list): raise TypeError(constants.MESSAGE_DATA_ERROR_DATA_TYPE_MISMATCH) incoming_data = columns if not compact.is_array_type(columns, list): incoming_data = [columns] incoming_data = transpose(incoming_data) self._extend_columns_with_rows(incoming_data)
def __init__(self, row_index, formatter): """Constructor :param row_index: to which row or what rows to apply the formatter :param formatter: the target format, or a custom functional formatter """ self.indices = row_index if isinstance(row_index, int): def func(r, c, v): return r == row_index elif isinstance(row_index, list): if len(row_index) == 0: raise IndexError(MESSAGE_DATA_ERROR_EMPTY_COLUMN_LIST) if is_array_type(row_index, int): def func(r, c, v): return r in row_index else: raise IndexError(MESSAGE_DATA_ERROR_COLUMN_LIST_INTEGER_TYPE) else: raise NotImplementedError("%s is not supported" % type(row_index)) Formatter.__init__(self, func, formatter)
def select(self, indices): """ Examples: >>> import pyexcel as pe >>> data = [[1,2,3,4,5,6,7,9]] >>> sheet = pe.Sheet(data) >>> sheet pyexcel sheet: +---+---+---+---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | +---+---+---+---+---+---+---+---+ >>> sheet.column.select([1,2,3,5]) >>> sheet pyexcel sheet: +---+---+---+---+ | 2 | 3 | 4 | 6 | +---+---+---+---+ >>> data = [[1,2,3,4,5,6,7,9]] >>> sheet = pe.Sheet(data) >>> sheet pyexcel sheet: +---+---+---+---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | +---+---+---+---+---+---+---+---+ >>> sheet.column.select([1,2,3,5]) >>> sheet pyexcel sheet: +---+---+---+---+ | 2 | 3 | 4 | 6 | +---+---+---+---+ >>> data = [ ... ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], ... [1,2,3,4,5,6,7,9], ... ] >>> sheet = pe.Sheet(data, name_columns_by_row=0) >>> sheet pyexcel sheet: +---+---+---+---+---+---+---+---+ | a | b | c | d | e | f | g | h | +===+===+===+===+===+===+===+===+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | +---+---+---+---+---+---+---+---+ >>> del sheet.column['a', 'b', 'i', 'f'] # doctest:+ELLIPSIS Traceback (most recent call last): ... ValueError: ... >>> sheet.column.select(['a', 'c', 'e', 'h']) >>> sheet pyexcel sheet: +---+---+---+---+ | a | c | e | h | +===+===+===+===+ | 1 | 3 | 5 | 9 | +---+---+---+---+ """ new_indices = [] if compact.is_array_type(indices, str): new_indices = utils.names_to_indices(indices, self.__ref.colnames) else: new_indices = indices to_remove = [] for index in self.__ref.column_range(): if index not in new_indices: to_remove.append(index) self.__ref.filter(column_indices=to_remove)
def _extend_row(self, row): array = copy.deepcopy(row) if compact.is_array_type(array, list): self.__array += array else: self.__array.append(array)
def select(self, indices): """ Examples: >>> import pyexcel as pe >>> data = [[1,2,3,4,5,6,7,9]] >>> sheet = pe.Sheet(data) >>> sheet pyexcel sheet: +---+---+---+---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | +---+---+---+---+---+---+---+---+ >>> sheet.column.select([1,2,3,5]) >>> sheet pyexcel sheet: +---+---+---+---+ | 2 | 3 | 4 | 6 | +---+---+---+---+ >>> data = [[1,2,3,4,5,6,7,9]] >>> sheet = pe.Sheet(data) >>> sheet pyexcel sheet: +---+---+---+---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | +---+---+---+---+---+---+---+---+ >>> sheet.column.select([1,2,3,5]) >>> sheet pyexcel sheet: +---+---+---+---+ | 2 | 3 | 4 | 6 | +---+---+---+---+ >>> data = [ ... ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], ... [1,2,3,4,5,6,7,9], ... ] >>> sheet = pe.Sheet(data, name_columns_by_row=0) >>> sheet pyexcel sheet: +---+---+---+---+---+---+---+---+ | a | b | c | d | e | f | g | h | +===+===+===+===+===+===+===+===+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | +---+---+---+---+---+---+---+---+ >>> del sheet.column['a', 'b', 'i', 'f'] # doctest:+ELLIPSIS Traceback (most recent call last): ... ValueError: ... >>> sheet.column.select(['a', 'c', 'e', 'h']) >>> sheet pyexcel sheet: +---+---+---+---+ | a | c | e | h | +===+===+===+===+ | 1 | 3 | 5 | 9 | +---+---+---+---+ """ new_indices = [] if compact.is_array_type(indices, str): new_indices = utils.names_to_indices(indices, self._ref.colnames) else: new_indices = indices to_remove = [] for index in self._ref.column_range(): if index not in new_indices: to_remove.append(index) self._ref.filter(column_indices=to_remove)