Ejemplo n.º 1
0
def test_check_new_headers():
    """Check that we get good error messages when setting bad headers."""
    table = Table(value=_TABLE_DATA["dict"])
    with pytest.raises(ValueError) as e:
        table.column_headers = ("a", "b", "c", "d")
        assert "Length mismatch" in str(e)
    with pytest.raises(ValueError) as e:
        table.row_headers = ("a", "b", "c", "d")
        assert "Length mismatch" in str(e)
Ejemplo n.º 2
0
def test_table(key):
    """Test a few ways to input tables."""
    input = _TABLE_DATA[key]
    table = Table(value=input)
    if key == "split":
        assert table.value == input
    if key not in ("tuple", "data"):
        # make sure the output is the same as the input
        assert table.to_dict(key) == input
        # can also test equality of table widgets
        assert Table(value=table.to_dict(key)) == table
        table.row_headers = ("x", "x")
        table.column_headers = ("a", "a", "b")
        with pytest.warns(UserWarning):
            table.to_dict(key)
Ejemplo n.º 3
0
# it behaves like a dict:
table["new_col"] = [5, 5]
assert table.pop("new_col") == [5, 5]
# keys and items have both regular (column) and "row" modes
col_item_view = table.items()  # iterate col_header/column
row_item_view = table.items("row")  # iterate row_header/row

# we can just call dict() to get back our dict of lists
assert dict(table) == dict_of_lists
# or use one of many other exports in `to_dict`
assert table.to_dict("records") == list_of_records

# change headers
table.row_headers = ("row1", "row2")
table.column_headers = ("a", "b", "c")

# setting value clears and resets the table:
table.value = np.arange(18).reshape(6, 3)
# we can get/set/delete the 2D data table using numpy-style indexing:
# get every other row
assert table.data[::2] == [[0, 1, 2], [6, 7, 8], [12, 13, 14]]
# set every other column in the 3rd row
table.data[2, ::2] = [99, 99]

# export to numpy or pandas
# table.data.to_numpy()
# table.to_dataframe()

table.show(run=True)