1

I have a piece of code written in Iron Python that reads data from table present in SpotFire and serialize in JSON object. It is taking too long to get executed. Please provide alternates to it.

import clr
import sys
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import DataValueCursor

rowCount = MyTable.RowCount
rows = IndexSet(rowCount,True)
cols = MyTable.Columns
MyTableData=[]

for r in rows:
 list={}
 item={}
 for c in cols:
  item[c.Name] = c.RowValues.GetFormattedValue(r)
  list['MyData']=item
 MyTableData.append(list)

json=JavaScriptSerializer(MaxJsonLength=sys.maxint).Serialize(MyTableData)
user948401
  • 31
  • 6

1 Answers1

0

Your code will be faster if you don't call list['MyData']=item for every column. You only need to call it once.

You could also use list and dictionary comprehensions, instead of appending, or looking up keys for every value.

MyTableData = [{'MyData': {column.Name: column.RowValues.GetFormattedValue(row)
                           for column in cols}}
               for row in rows]

If column.RowValues is an expensive operation you may be better looping over columns, which isn't as neat.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99