Ok, big thanks to PGriffith and pturmel for their help. My current code is as follows. The formatting works but I still need to digest and incorporate the additional tips from PGriffith. I'm pretty new to Python so the suggestions for improving the efficiency and quality of my code are very much appreciated!
def sortTags(self):
"""
Arguments:
self: A reference to the component instance this method is invoked on. This argument
is automatic and should not be specified when invoking this method.
"""
from java.text import DecimalFormat
# Set up the empty lists we need
names = []
colors = []
paths = []
valuePaths = []
tooltipPaths = []
engUnitPaths = []
formatStringPaths = []
displayNames = []
# Scan through the rows of pens from the chart
# and put the path for the tooltips, values etc. into seperate datasets
rawNames = self.inputTags.getColumnAsList(0)
rawDisplayNames = self.inputTags.getColumnAsList(1)
rawColors = self.inputTags.getColumnAsList(2)
for index, item in enumerate(rawNames):
if not rawNames[index]:
break
names.append(rawNames[index])
if rawDisplayNames[index]:
displayNames.append(rawDisplayNames[index])
else:
displayNames.append(rawNames[index])
colors.append(str(rawColors[index]))
paths.append("[default]GlobalVars/" + rawNames[index])
valuePaths.append("[default]GlobalVars/" + rawNames[index] + ".Value")
tooltipPaths.append("[default]GlobalVars/" + rawNames[index] + ".Tooltip")
engUnitPaths.append("[default]GlobalVars/" + rawNames[index] + ".EngUnit")
formatStringPaths.append("[default]GlobalVars/" + rawNames[index] + ".FormatString")
# Create a dataset with the unsorted tag names
unsortedTags = system.dataset.toDataSet(["Name"], [[name] for name in names])
# If the list of names is not empty then look up the actual tool tip, value and eng units from the paths on each row
if len(names) > 0:
toolTips = [toolTipTag.value for toolTipTag in system.tag.readBlocking(tooltipPaths)]
values = [valueTag.value for valueTag in system.tag.readBlocking(valuePaths)]
engUnits = [engUnitTag.value for engUnitTag in system.tag.readBlocking(engUnitPaths)]
formatString = [formatStringTag.value for formatStringTag in system.tag.readBlocking(formatStringPaths)]
# Add the various lists to the unsortedTags dataset as individual columns
unsortedTags = system.dataset.addColumn(unsortedTags, 1, paths, "Path", str)
unsortedTags = system.dataset.addColumn(unsortedTags, 2, values, "Value", float)
unsortedTags = system.dataset.addColumn(unsortedTags, 3, engUnits, "Eng Unit", str)
unsortedTags = system.dataset.addColumn(unsortedTags, 4, toolTips, "Tooltip", str)
unsortedTags = system.dataset.addColumn(unsortedTags, 5, colors, "Color", str)
unsortedTags = system.dataset.addColumn(unsortedTags, 6, displayNames, "Display Name", str)
unsortedTags = system.dataset.addColumn(unsortedTags, 7, formatString, "Format String", str)
# Sort the data set by Value in decending order
sortedTags = system.dataset.sort(unsortedTags, "Value", False)
#For each row in the sorted data set update the value, eng units, tooltip and colour
for row in range(len(rawNames)):
if row >= unsortedTags.getRowCount():
self.getComponent('Label '+ str(row)).visible = False
else:
if sortedTags.getValueAt(row,2) == None:
self.getComponent('Label '+ str(row)).text = "- : " + sortedTags.getValueAt(row,6)
else:
formatter = DecimalFormat(sortedTags.getValueAt(row,7))
self.getComponent('Label '+ str(row)).text = str(formatter.format(sortedTags.getValueAt(row,2))) + " : " + sortedTags.getValueAt(row,6)
self.getComponent('Label '+ str(row)).toolTipText = sortedTags.getValueAt(row,0) + " - " + sortedTags.getValueAt(row,4) + " (" + sortedTags.getValueAt(row,3) + ")"
self.getComponent('Label '+ str(row)).foreground = system.gui.color(sortedTags.getValueAt(row,5))
self.getComponent('Label '+ str(row)).visible = True