pyqtgraph/tests/widgets/test_tablewidget.py
Kyle Sunden a472f8c5de
Remove all usage of python2_3.py (#1939)
* Remove all usage of python2_3.py

Technically these functions were exported at the top level of the library, this removes them without warning... If we want to we can bring them back for there, but I honestly don't think its needed, as we are py3 only now and have been for multiple releases.

This may introduce a number of 'useless cast' or similar but those were always happening anyway

This PR brought to you by sed

* Update varname in hdf example to avoid collision with builtin

* Clean up some leftover comments surrounding imports of compat code

* Unnecessary string casts

* Additional unnecessary casts

* syntax error fix

* more unnecessary casts

* Yet more unnecessary casts
2021-08-01 21:43:32 -07:00

121 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
import pyqtgraph as pg
import numpy as np
from collections import OrderedDict
app = pg.mkQApp()
listOfTuples = [('text_%d' % i, i, i/9.) for i in range(12)]
listOfLists = [list(row) for row in listOfTuples]
plainArray = np.array(listOfLists, dtype=object)
recordArray = np.array(listOfTuples, dtype=[('string', object),
('integer', int),
('floating', float)])
dictOfLists = OrderedDict([(name, list(recordArray[name])) for name in recordArray.dtype.names])
listOfDicts = [OrderedDict([(name, rec[name]) for name in recordArray.dtype.names]) for rec in recordArray]
transposed = [[row[col] for row in listOfTuples] for col in range(len(listOfTuples[0]))]
def assertTableData(table, data):
assert len(data) == table.rowCount()
rows = list(range(table.rowCount()))
columns = list(range(table.columnCount()))
for r in rows:
assert len(data[r]) == table.columnCount()
row = []
for c in columns:
item = table.item(r, c)
if item is not None:
row.append(item.value)
else:
row.append(None)
assert row == list(data[r])
def test_TableWidget():
w = pg.TableWidget(sortable=False)
# Test all input data types
w.setData(listOfTuples)
assertTableData(w, listOfTuples)
w.setData(listOfLists)
assertTableData(w, listOfTuples)
w.setData(plainArray)
assertTableData(w, listOfTuples)
w.setData(recordArray)
assertTableData(w, listOfTuples)
w.setData(dictOfLists)
assertTableData(w, transposed)
w.appendData(dictOfLists)
assertTableData(w, transposed * 2)
w.setData(listOfDicts)
assertTableData(w, listOfTuples)
w.appendData(listOfDicts)
assertTableData(w, listOfTuples * 2)
# Test sorting
w.setData(listOfTuples)
w.sortByColumn(0, pg.QtCore.Qt.SortOrder.AscendingOrder)
assertTableData(w, sorted(listOfTuples, key=lambda a: a[0]))
w.sortByColumn(1, pg.QtCore.Qt.SortOrder.AscendingOrder)
assertTableData(w, sorted(listOfTuples, key=lambda a: a[1]))
w.sortByColumn(2, pg.QtCore.Qt.SortOrder.AscendingOrder)
assertTableData(w, sorted(listOfTuples, key=lambda a: a[2]))
w.setSortMode(1, 'text')
w.sortByColumn(1, pg.QtCore.Qt.SortOrder.AscendingOrder)
assertTableData(w, sorted(listOfTuples, key=lambda a: str(a[1])))
w.setSortMode(1, 'index')
w.sortByColumn(1, pg.QtCore.Qt.SortOrder.AscendingOrder)
assertTableData(w, listOfTuples)
# Test formatting
item = w.item(0, 2)
assert item.text() == ('%0.3g' % item.value)
w.setFormat('%0.6f')
assert item.text() == ('%0.6f' % item.value)
w.setFormat('X%0.7f', column=2)
assert isinstance(item.value, float)
assert item.text() == ('X%0.7f' % item.value)
# test setting items that do not exist yet
w.setFormat('X%0.7f', column=3)
# test append uses correct formatting
w.appendRow(('x', 10, 7.3))
item = w.item(w.rowCount()-1, 2)
assert isinstance(item.value, float)
assert item.text() == ('X%0.7f' % item.value)
# test reset back to defaults
w.setFormat(None, column=2)
assert isinstance(item.value, float)
assert item.text() == ('%0.6f' % item.value)
w.setFormat(None)
assert isinstance(item.value, float)
assert item.text() == ('%0.3g' % item.value)
# test function formatter
def fmt(item):
if isinstance(item.value, float):
return "%d %f" % (item.index, item.value)
else:
return str(item.value)
w.setFormat(fmt)
assert isinstance(item.value, float)
assert isinstance(item.index, int)
assert item.text() == ("%d %f" % (item.index, item.value))