IgnitionModuleDevelopmentCommunity/ignition-extensions

system.dataset.toDict

Closed this issue · 2 comments

Converts a dataset into a dictionary. I use it quite often when taking a dataset and need to pull a column of data. Used heavily with templates.

def dataSetToDict(dataSet, headers = None):
	finalDict = {}
	if headers is None:
		headers = dataSet.getColumnNames().toArray()	
	
	for col in range(dataSet.getColumnCount()):	
		tempArray = []
		for row in range(dataSet.getRowCount()):
			value = dataSet.getValueAt(row, col)
			if value != 'None' and value is not None:
				#print(value)
				tempArray.append(value)
		finalDict[headers[col]] = tempArray
	
	return finalDict

I am not sure how I should go about getting values into the Python dict. I assume you could use PyDictionary similar to TagConfigurationModel.toPyDictionary()

Yes, you probably want to collect the entries into a PyStringMap (a specialization of PyDictionary with string keys).

The most idiomatic Kotlin code would probably look something like this (spoilers, in case you want to do it yourself :) )

Kotlin
        return PyStringMap(
            dataset.columnIndices.associate { 
                dataset.getColumnName(it) to PyList(dataset.getColumnAsList(it))
            },
        )