How to use the ipysheet.utils.transpose function in ipysheet

To help you get started, we’ve selected a few ipysheet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github QuantStack / ipysheet / ipysheet / test_all.py View on Github external
def test_transpose():
    assert transpose([[1, 2]]) == [[1], [2]]
    assert transpose([[1, 2], [3, 4]]) == [[1, 3], [2, 4]]
    assert transpose([[1], [2]]) == [[1, 2]]
github QuantStack / ipysheet / ipysheet / test_all.py View on Github external
r.value = 1
    with pytest.raises(ValueError):
        r.value = [1, 2, 3]
    with pytest.raises(ValueError):
        r.value = [[1, 2]]
    assert r.value == transpose(valueT)

    rT = ipysheet.cell_range(valueT, transpose=True)  # 3 rows, 2 columns
    with pytest.raises(ValueError):
        rT.value = 1
    with pytest.raises(ValueError):
        rT.value = [1, 2, 3]
    with pytest.raises(ValueError):
        rT.value = [[1, 2]]
    rT.value = transpose(value)
    assert rT.value == transpose(value)

    sheet = ipysheet.sheet(rows=3, columns=4)
    assert len(sheet.cells) == 0
    with ipysheet.hold_cells():
        ipysheet.cell_range(value)
        ipysheet.cell_range(value)
        assert len(sheet.cells) == 0
    assert len(sheet.cells) == 2
github QuantStack / ipysheet / ipysheet / test_all.py View on Github external
ipysheet.cell_range([[0, 1, 9]], column_end=2)  # 3 rows, 3 columns
    ipysheet.cell_range([[0, 1, 9]], column_start=1)  # 1 rows, 3 columns
    with pytest.raises(ValueError):
        ipysheet.cell_range([[0, 1], [2, 3], [4, 5], [6, 7]])  # 4 rows, 2 columns
    with pytest.raises(ValueError):
        ipysheet.cell_range([[0, 1, 2, 3, 4], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]])  # 3 rows, 5 columns
    with pytest.raises(ValueError):
        ipysheet.cell_range([[0, 1, 2, 3, 4], [2], [3, 4, 5, 6, 7]])  # not well shaped
    with pytest.raises(ValueError):
        ipysheet.cell_range([])  # empty rows
    with pytest.raises(ValueError):
        ipysheet.cell_range([[], []])  # empty columns

    value = [[0, 1], [2, 3], [4, 5]]
    valueT = [[0, 2, 4], [1, 3, 5]]  # it's transpose
    assert value == transpose(valueT)
    r = ipysheet.cell_range(value)  # 3 rows, 2 columns
    with pytest.raises(ValueError):
        r.value = 1
    with pytest.raises(ValueError):
        r.value = [1, 2, 3]
    with pytest.raises(ValueError):
        r.value = [[1, 2]]
    assert r.value == transpose(valueT)

    rT = ipysheet.cell_range(valueT, transpose=True)  # 3 rows, 2 columns
    with pytest.raises(ValueError):
        rT.value = 1
    with pytest.raises(ValueError):
        rT.value = [1, 2, 3]
    with pytest.raises(ValueError):
        rT.value = [[1, 2]]
github QuantStack / ipysheet / ipysheet / test_all.py View on Github external
with pytest.raises(ValueError):
        r.value = 1
    with pytest.raises(ValueError):
        r.value = [1, 2, 3]
    with pytest.raises(ValueError):
        r.value = [[1, 2]]
    assert r.value == transpose(valueT)

    rT = ipysheet.cell_range(valueT, transpose=True)  # 3 rows, 2 columns
    with pytest.raises(ValueError):
        rT.value = 1
    with pytest.raises(ValueError):
        rT.value = [1, 2, 3]
    with pytest.raises(ValueError):
        rT.value = [[1, 2]]
    rT.value = transpose(value)
    assert rT.value == transpose(value)

    sheet = ipysheet.sheet(rows=3, columns=4)
    assert len(sheet.cells) == 0
    with ipysheet.hold_cells():
        ipysheet.cell_range(value)
        ipysheet.cell_range(value)
        assert len(sheet.cells) == 0
    assert len(sheet.cells) == 2
github QuantStack / ipysheet / ipysheet / sheet.py View on Github external
value = adapt_value(value)
        if self.squeeze_row:
            value = [value]
        try:
            len(value)
        except TypeError:
            raise ValueError('value shape is incorrect')
        if self.squeeze_column:
            value = [[k] for k in value]
        # print(self.squeeze_row, self.squeeze_column, value)
        try:
            len(value[0])
        except TypeError:
            raise ValueError('value shape is incorrect')
        if self.transpose:  # we just work with the 'correct' shape
            value = transpose(value)
        row_length = self.row_end - self.row_start + 1
        if row_length != len(value):
            raise ValueError("length or array (%d) doesn't match number of rows (%d)" % (len(value), row_length))
        column_length = self.column_end - self.column_start + 1
        for row in value:
            if column_length != len(row):
                raise ValueError("not a regular matrix, columns lengths differ")
        return original_value
github QuantStack / ipysheet / ipysheet / easy.py View on Github external
transpose : bool
        Interpret the value array as value[column_index][row_index]
    squeeze_row : bool
        Take out the row dimensions, meaning only value[column_index] is used
    squeeze_column : bool
        Take out the column dimensions, meaning only value[row_index] is used

    Returns
    -------
    Range
        A CellRange widget.
    """
    global _cells
    # instead of an if statements, we just use T to transpose or not when needed
    value_original = value
    T = (lambda x: x) if not transpose else default_transpose
    # we work with the optionally transposed values for simplicity
    value = T(value)
    if squeeze_row:
        value = [value]
    if squeeze_column:
        value = [[k] for k in value]
    if row_end is None:
        row_end = row_start + len(value) - 1
    row_length = row_end - row_start + 1
    if row_length != len(value):
        raise ValueError("length or array doesn't match number of rows")
    if row_length == 0:
        raise ValueError("0 rows not supported")
    if column_end is None:
        column_end = column_start + len(value[0]) - 1
    column_length = column_end - column_start + 1