Ejemplo n.º 1
0
 def named_column_at(self, name):
     """Get a column by its name"""
     index = name
     if compact.is_string(type(index)):
         index = self.colnames.index(name)
     column_array = self.column_at(index)
     return column_array
Ejemplo n.º 2
0
    def set_named_row_at(self, name, row_array):
        """
        Take the first column as row names

        Given name to identify the row index, set the row to
        the given array except the row name.
        """
        index = name
        if compact.is_string(type(index)):
            index = self.rownames.index(name)
        self.set_row_at(index, row_array)
Ejemplo n.º 3
0
    def set_named_column_at(self, name, column_array):
        """
        Take the first row as column names

        Given name to identify the column index, set the column to
        the given array except the column name.
        """
        index = name
        if compact.is_string(type(index)):
            index = self.colnames.index(name)
        self.set_column_at(index, column_array)
Ejemplo n.º 4
0
 def __setitem__(self, aslice, a_row):
     """Override the operator to set items"""
     if compact.is_string(type(aslice)):
         self._ref.set_named_row_at(aslice, a_row)
     elif isinstance(aslice, slice):
         my_range = utils.analyse_slice(
             aslice, self._ref.number_of_rows())
         for i in my_range:
             self._ref.set_row_at(i, a_row)
     else:
         self._ref.set_row_at(aslice, a_row)
Ejemplo n.º 5
0
def make_names_unique(alist):
    """Append the number of occurences to duplicated names"""
    duplicates = {}
    new_names = []
    for item in alist:
        if not compact.is_string(type(item)):
            item = str(item)
        if item in duplicates:
            duplicates[item] = duplicates[item] + 1
            new_names.append("%s-%d" % (item, duplicates[item]))
        else:
            duplicates[item] = 0
            new_names.append(item)
    return new_names
Ejemplo n.º 6
0
    def is_my_business(self, action, **keywords):
        status = SourceInfo.is_my_business(self, action, **keywords)
        if status:
            file_name = keywords.get("file_name", None)
            if file_name:
                if is_string(type(file_name)):
                    file_type = find_file_type_from_file_name(file_name,
                                                              action)
                else:
                    raise IOError("Wrong file name")
            else:
                file_type = keywords.get("file_type")

            status = self.can_i_handle(action, file_type)
        return status
Ejemplo n.º 7
0
    def __getitem__(self, aslice):
        """By default, this class recognize from top to bottom
        from left to right"""
        index = aslice
        if compact.is_string(type(aslice)):
            return self._ref.named_row_at(aslice)
        elif isinstance(aslice, slice):
            my_range = utils.analyse_slice(aslice, self._ref.number_of_rows())
            results = []
            for i in my_range:
                results.append(self._ref.row_at(i))
            return results

        if abs(index) in self._ref.row_range():
            return self._ref.row_at(index)
        else:
            raise IndexError
Ejemplo n.º 8
0
 def __getitem__(self, aslice):
     """By default, this class recognize from top to bottom
     from left to right"""
     index = aslice
     if compact.is_string(type(aslice)):
         return self._ref.named_row_at(aslice)
     elif isinstance(aslice, slice):
         my_range = utils.analyse_slice(aslice,
                                        self._ref.number_of_rows())
         results = []
         for i in my_range:
             results.append(self._ref.row_at(i))
         return results
     if index in self._ref.row_range():
         return self._ref.row_at(index)
     else:
         raise IndexError
Ejemplo n.º 9
0
    def is_my_business(cls, action, **keywords):
        status = super(FileSource, cls).is_my_business(action, **keywords)
        if status:
            file_name = keywords.get(params.FILE_NAME, None)
            if file_name:
                if is_string(type(file_name)):
                    file_type = file_name.split(".")[-1]
                else:
                    raise IOError("Wrong file name")
            else:
                file_type = keywords.get(params.FILE_TYPE)

            if cls.can_i_handle(action, file_type):
                status = True
            else:
                status = False
        return status
Ejemplo n.º 10
0
 def __setitem__(self, aslice, c):
     """Override the operator to set items"""
     is_sheet = (compact.is_string(type(aslice))
                 and hasattr(self.__ref, 'set_named_column_at'))
     if is_sheet:
         self.__ref.set_named_column_at(aslice, c)
     elif isinstance(aslice, slice):
         my_range = utils.analyse_slice(aslice,
                                        self.__ref.number_of_columns())
         for i in my_range:
             self.__ref.set_column_at(i, c)
     elif isinstance(aslice, str):
         index = utils.excel_column_index(aslice)
         self.__ref.set_column_at(index, c)
     elif isinstance(aslice, int):
         self.__ref.set_column_at(aslice, c)
     else:
         raise IndexError
Ejemplo n.º 11
0
 def __setitem__(self, aslice, a_column):
     """Override the operator to set items"""
     is_sheet = (compact.is_string(type(aslice)) and
                 hasattr(self._ref, 'set_named_column_at'))
     if is_sheet:
         self._ref.set_named_column_at(aslice, a_column)
     elif isinstance(aslice, slice):
         my_range = utils.analyse_slice(aslice,
                                        self._ref.number_of_columns())
         for i in my_range:
             self._ref.set_column_at(i, a_column)
     elif isinstance(aslice, str):
         index = utils.excel_column_index(aslice)
         self._ref.set_column_at(index, a_column)
     elif isinstance(aslice, int):
         self._ref.set_column_at(aslice, a_column)
     else:
         raise IndexError
Ejemplo n.º 12
0
    def is_my_business(self, action, **keywords):
        status = SourceInfo.is_my_business(self, action, **keywords)
        if status:
            file_name = keywords.get("file_name", None)
            if file_name:
                file_type = keywords.get("force_file_type")

                if file_type is None:
                    if is_string(type(file_name)):
                        file_type = find_file_type_from_file_name(
                            file_name, action)
                    else:
                        raise IOError("Unsupported file type")
            else:
                file_type = keywords.get("file_type")

            status = self.can_i_handle(action, file_type)
        return status
Ejemplo n.º 13
0
 def __getitem__(self, aslice):
     """By default, this class recognize from top to bottom
     from left to right"""
     index = aslice
     is_sheet = (compact.is_string(type(aslice)) and
                 hasattr(self._ref, 'named_column_at'))
     if is_sheet:
         return self._ref.named_column_at(aslice)
     elif isinstance(aslice, slice):
         my_range = utils.analyse_slice(aslice,
                                        self._ref.number_of_columns())
         results = []
         for i in my_range:
             results.append(self._ref.column_at(i))
         return results
     elif isinstance(aslice, str):
         index = utils.excel_column_index(aslice)
     if index in self._ref.column_range():
         return self._ref.column_at(index)
     else:
         raise IndexError
Ejemplo n.º 14
0
 def __getitem__(self, aslice):
     """By default, this class recognize from top to bottom
     from left to right"""
     index = aslice
     is_sheet = (compact.is_string(type(aslice))
                 and hasattr(self.__ref, 'named_column_at'))
     if is_sheet:
         return self.__ref.named_column_at(aslice)
     elif isinstance(aslice, slice):
         my_range = utils.analyse_slice(aslice,
                                        self.__ref.number_of_columns())
         results = []
         for i in my_range:
             results.append(self.__ref.column_at(i))
         return results
     elif isinstance(aslice, str):
         index = utils.excel_column_index(aslice)
     if index in self.__ref.column_range():
         return self.__ref.column_at(index)
     else:
         raise IndexError
Ejemplo n.º 15
0
    def __delitem__(self, aslice):
        """Override the operator to delete items

        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 |
            +---+---+---+---+---+---+---+---+
            >>> del sheet.column[1,2,3,5]
            >>> sheet
            pyexcel sheet:
            +---+---+---+---+
            | 1 | 5 | 7 | 9 |
            +---+---+---+---+
            >>> 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: ...
            >>> del sheet.column['a', 'c', 'e', 'h']
            >>> sheet
            pyexcel sheet:
            +---+---+---+---+
            | b | d | f | g |
            +===+===+===+===+
            | 2 | 4 | 6 | 7 |
            +---+---+---+---+

        """
        is_sheet = (compact.is_string(type(aslice)) and
                    hasattr(self._ref, 'delete_named_column_at'))
        if is_sheet:
            self._ref.delete_named_column_at(aslice)
        elif compact.is_tuple_consists_of_strings(aslice):
            indices = utils.names_to_indices(list(aslice),
                                             self._ref.colnames)
            self._ref.delete_columns(indices)
        elif isinstance(aslice, slice):
            my_range = utils.analyse_slice(aslice,
                                           self._ref.number_of_columns())
            self._ref.delete_columns(my_range)
        elif isinstance(aslice, str):
            index = utils.excel_column_index(aslice)
            self._ref.delete_columns([index])
        elif isinstance(aslice, tuple):
            indices = list(aslice)
            self._ref.filter(column_indices=indices)
        elif isinstance(aslice, list):
            indices = aslice
            self._ref.filter(column_indices=indices)
        elif isinstance(aslice, int):
            self._ref.delete_columns([aslice])
        elif isinstance(aslice, types.LambdaType):
            self._delete_columns_by_content(aslice)
        elif isinstance(aslice, types.FunctionType):
            self._delete_columns_by_content(aslice)
        else:
            raise IndexError
Ejemplo n.º 16
0
    def __delitem__(self, aslice):
        """Override the operator to delete items

        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 |
            +---+---+---+---+---+---+---+---+
            >>> del sheet.column[1,2,3,5]
            >>> sheet
            pyexcel sheet:
            +---+---+---+---+
            | 1 | 5 | 7 | 9 |
            +---+---+---+---+
            >>> 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: ...
            >>> del sheet.column['a', 'c', 'e', 'h']
            >>> sheet
            pyexcel sheet:
            +---+---+---+---+
            | b | d | f | g |
            +===+===+===+===+
            | 2 | 4 | 6 | 7 |
            +---+---+---+---+

        """
        is_sheet = (compact.is_string(type(aslice))
                    and hasattr(self.__ref, 'delete_named_column_at'))
        if is_sheet:
            self.__ref.delete_named_column_at(aslice)
        elif compact.is_tuple_consists_of_strings(aslice):
            indices = utils.names_to_indices(list(aslice), self.__ref.colnames)
            self.__ref.delete_columns(indices)
        elif isinstance(aslice, slice):
            my_range = utils.analyse_slice(aslice,
                                           self.__ref.number_of_columns())
            self.__ref.delete_columns(my_range)
        elif isinstance(aslice, str):
            index = utils.excel_column_index(aslice)
            self.__ref.delete_columns([index])
        elif isinstance(aslice, tuple):
            indices = list(aslice)
            self.__ref.filter(column_indices=indices)
        elif isinstance(aslice, list):
            indices = aslice
            self.__ref.filter(column_indices=indices)
        elif isinstance(aslice, int):
            self.__ref.delete_columns([aslice])
        elif isinstance(aslice, types.LambdaType):
            self._delete_columns_by_content(aslice)
        elif isinstance(aslice, types.FunctionType):
            self._delete_columns_by_content(aslice)
        else:
            raise IndexError
Ejemplo n.º 17
0
    def __delitem__(self, locator):
        """Override the operator to delete items

        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 |
            +---+
            >>> del sheet.row[1,2,3,5]
            >>> sheet
            pyexcel sheet:
            +---+
            | 1 |
            +---+
            | 5 |
            +---+
            | 7 |
            +---+
            | 9 |
            +---+

        """
        if compact.is_string(type(locator)):
            self._ref.delete_named_row_at(locator)
        elif compact.is_tuple_consists_of_strings(locator):
            indices = utils.names_to_indices(list(locator),
                                             self._ref.rownames)
            self._ref.delete_rows(indices)
        elif isinstance(locator, slice):
            my_range = utils.analyse_slice(locator,
                                           self._ref.number_of_rows())
            self._ref.delete_rows(my_range)
        elif isinstance(locator, tuple):
            self._ref.filter(row_indices=(list(locator)))
        elif isinstance(locator, list):
            self._ref.filter(row_indices=locator)
        elif isinstance(locator, types.LambdaType):
            self._delete_rows_by_content(locator)
        elif isinstance(locator, types.FunctionType):
            self._delete_rows_by_content(locator)
        else:
            self._ref.delete_rows([locator])
Ejemplo n.º 18
0
    def __delitem__(self, locator):
        """Override the operator to delete items

        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 |
            +---+
            >>> del sheet.row[1,2,3,5]
            >>> sheet
            pyexcel sheet:
            +---+
            | 1 |
            +---+
            | 5 |
            +---+
            | 7 |
            +---+
            | 9 |
            +---+

        """
        if compact.is_string(type(locator)):
            self.ref.delete_named_row_at(locator)
        elif compact.is_tuple_consists_of_strings(locator):
            indices = utils.names_to_indices(list(locator),
                                             self.ref.rownames)
            self.ref.delete_rows(indices)
        elif isinstance(locator, slice):
            my_range = utils.analyse_slice(locator,
                                           self.ref.number_of_rows())
            self.ref.delete_rows(my_range)
        elif isinstance(locator, tuple):
            self.ref.filter(row_indices=(list(locator)))
        elif isinstance(locator, list):
            self.ref.filter(row_indices=locator)
        elif isinstance(locator, types.LambdaType):
            self._delete_rows_by_content(locator)
        elif isinstance(locator, types.FunctionType):
            self._delete_rows_by_content(locator)
        else:
            self.ref.delete_rows([locator])