通过使用IronPython从现有dataTable计算新行来创建新的Spotfire DataTable

时间:2016-08-30 15:03:32

标签: ironpython tibco spotfire

我是Iron Python的新手,我需要用它来创建一个新的Spotfire DataTable。我已经能够创建我的硬编码行。请参阅下面的代码。 但我需要能够将现有Spotfire dataTable的计算添加到每一行:

Rows    NoOfSessions    Calculations
row1    133 sum(OpenLastNos)
row2    0   sum(OpenLastNos)
row3    15  sum(OpenLastNos) + sum(OpenSince)
row4    133 sum(OpenLastNos)
row5    0   sum(OpenLastNos)
row6    148 sum(ImplementedSinceNos)
row7    44  sumClosedSinceNos)
row8    104 sum(OpenLastNos)
row9    104 sum(OpenLastNos)
row10   0   sum(OpenLastNos)

我有什么想法可以做到吗?非常感谢所有帮助

    import clr
import System
from System import DateTime
from System.Data import DataSet, DataTable
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
from Spotfire.Dxp.Data import AddRowsSettings
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin


# Load Spotfire Source Data into memory and calculate “Number of Sessions” column. Add the 10 different calculations of This new column to each of the rows below and add these rows to a new Spotfire data table 

#Create data with columns

row1 =  "Open Last,Open Last,Pos" 
row2 =  "Open Last,Change,Pos"
row3 =  "Open Since,Open Since,Pas"
row4 =  "Open Since,Change,invisible"
row5 =  "Implemented Since,Implemented since,Pas"
row6 =  "Implemented Since,Change,invisible"
row7 = "Closed Since,Closed Since,Pis"
row8 =  "Closed Since,Change,invisible"
row9 =  "Open Current,Open Current,Yus"
row10 =  "Open Current,Change,Pis"

textData = "Movement,Category,Label\r\n" + row1 + "\r\n" + row2 + "\r\n" + row3 + "\r\n" + row3 + "\r\n" + row4 + "\r\n" + row5 + "\r\n" + row6 + "\r\n" + row7 + "\r\n" + row8 + "\r\n" + row9 + "\r\n" + row10 + "\r\n" 

#Memory Stream stuff. Simply just writing our variable 
#into a place we can access to make a data source

stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

#you need settings to tell the system what stuff you're importing.
#here we define it is comma separated and what data types our columns are.

readerSettings = TextDataReaderSettings()
readerSettings.Separator = ","
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.String)
readerSettings.SetDataType(2, DataType.String)
textDataSource = TextFileDataSource(stream,readerSettings)

# add the data into a Data Table in Spotfire
if Document.Data.Tables.Contains("Stock Data"):
     Document.Data.Tables["Stock Data"].ReplaceData(textDataSource)
else:
     nuTab = Document.Data.Tables.Add("Stock Data", textDataSource)
     tableSettings = DataTableSaveSettings (nuTab, False, False)
     Document.Data.SaveSettings.DataTableSettings.Add(tableSettings)

#We create some settings here automatically having the system match
#column names for us between the data table and our data source.
#settings = AddRowsSettings(nuTab,textDataSource)

#And finally we add the rows from our datasource with our settings.
#nuTab.AddRows(textDataSource,settings)

完成的dataTable看起来像这样(CSV):

 Movement   Category    Label   NoOfSessions
Open Last   Open Last   Positive    133
Open Last   Change  Positive    0
Open Since  Open Since  Moderate    15
Open Since  Change  Negative    133
Implemented Since   Implemented Since   Moderate    0
Implemented Since   Change  Negative    148
Closed Since    Closed Since    Strongly Positive   44
Closed Since    Change  Negative    104
Open Current    Open Current    Indiffirent 104
Open Current    Change  Strongly Positive   0

0 个答案:

没有答案