Ejemplo n.º 1
0
    def __getitem__(self, key):
        if isinstance(key, slice):
            start = key.start
            stop = key.stop
        else:
            start = key
            stop = key

        row_sliced = self._row_sliced

        if isinstance(start, str):
            start = col_index(start) - self.start_col
            row_sliced = True
        if isinstance(stop, str):
            stop = col_index(stop) - self.start_col
            row_sliced = True

        if not row_sliced:
            return OOSheet(self._generate_selector(self.start_col,
                                                   self.end_col,
                                                   self.start_row + start,
                                                   self.start_row + stop),
                           _row_sliced = True)
        else:
            return OOSheet(self._generate_selector(self.start_col + start,
                                                   self.start_col + stop,
                                                   self.start_row,
                                                   self.end_row))
Ejemplo n.º 2
0
    def _position(self, descriptor):
        col = re.findall('^([A-Z]+)', descriptor)[0]
        row = descriptor[len(col):]
            
        col = col_index(col)
        row = int(row) - 1

        return col, row
Ejemplo n.º 3
0
    def shift_until(self, col, row, *args, **kwargs):
        """
        Moves the selector in direction given by "col" and "row" parameters, until a condition is satisfied.

        If selector is a single cell, than a value can be given as parameter and shift will be done until
        that exact value is found.
        
        For multiple cells selectors, the parameters can be in one of the following forms:
        
        - column_LABEL = value
        - row_NUMBER = value
        - column_LABEL_satisfies = lambda
        - row_NUMBER_satisfies = lambda

        If column is given as condition, then shift must be horizontal, and vice-versa.
        
        If matching against a value, the type of the value given will be checked and either "value", "string"
        or "date" property of cell will be used.
        
        If matching against a lambda function, a single-cell OOSheet object will be given as parameter
        to the lambda function.
        """
        
        assert col != 0 or row != 0
        
        try:
            value = args[0]
            assert self.cell is not None
            while not self._cell_matches(self.cell, value):
                self.shift(col, row)
            return self
        except IndexError:
            pass

        assert len(kwargs.keys()) == 1
        ref = kwargs.keys()[0]
        value = kwargs[ref]

        reftype, position = ref.split('_')[:2]

        if ref.endswith('_satisfies'):
            condition = value
        else:
            condition = lambda s: s._cell_matches(s.cell, value)

        assert reftype in ('row', 'column')

        if reftype == 'row':
            assert row == 0
            ref_row = int(position) - 1
            if col > 0:
                ref_col = self.end_col
            else:
                ref_col = self.start_col
        else:
            assert col == 0
            ref_col = col_index(position)
            if row > 0:
                ref_row = self.end_row
            else:
                ref_row = self.start_row

        cell = OOSheet(self._generate_selector(ref_col, ref_col, ref_row, ref_row))
        while not condition(cell):
            self.shift(col, row)
            cell.shift(col, row)

        return self