首页 > 解决方案 > Ironpython - 如何在其他代码行中引用计算变量

问题描述

我正在 Spotfire 中使用 IronPython。

我需要从范围过滤器中提取最大日期值,然后使用该值过滤汇率表。

我的工作代码一直到 datatable.Select 语句,我需要在其中进行匹配。如果我根据“日期(2020,3,1)”(即注释掉的行)执行此操作,则匹配有效并返回正确的结果,但是对于使用计算变量“newdate”,我无法获得正确的语法代替 Date(xxx) 语句。我还在学习python,以前没有遇到过。

代码如下 - 任何帮助将不胜感激。

from Spotfire.Dxp.Application.Filters import RangeFilter, ValueRange
from Spotfire.Dxp.Data.DataType import  Date
from System.Globalization import CultureInfo
parser = Date.CreateCultureSpecificFormatter(CultureInfo("en-AU"))


#get a reference to a filter as checkbox from the myDataTable script parameter
filt=Document.FilteringSchemes[Document.ActiveFilteringSelectionReference].Item[dt].Item[dt.Columns.Item["Calendar Date"]].As[RangeFilter]()

print filt.ValueRange.High

if str(filt.ValueRange.High) == "High":
    maxdate = Document.Properties["loaddate"]
else: 
    maxdate = filt.ValueRange.High

maxdate = Date.Formatter.Parse(maxdate)
print maxdate
new = str(maxdate.Year) + "," + str(maxdate.Month) + "," + str("1")
print new
Document.Properties["maxdate"] = new


from Spotfire.Dxp.Data import *
from System.Collections.Generic import List
table=Document.ActiveDataTableReference
# Expression to limit the data in a table 
rowSelection=table.Select("CALENDAR_DATE = Date('new')")
#rowSelection=table.Select("CALENDAR_DATE = Date(2020,3,1)")

# Create a cursor to the Column we wish to get the values from
cursor = DataValueCursor.CreateFormatted(table.Columns["FY_AVERAGE_EXCHANGE"])

# Create List object that will hold values
listofValues=List[str]()

# Loop through all rows, retrieve value for specific column,
# and add value into list
for  row in  table.GetRows(rowSelection.AsIndexSet(),cursor):
   rowIndex = row.Index
   value1 = cursor.CurrentValue
   listofValues.Add(value1)


for val in listofValues:
    print val

标签: pythonironpythonspotfire

解决方案


我认为您的变量 new 将打印为 2020,01,01

这一行new是一个字符串,因此 Date() 无法提取日期。

rowSelection=table.Select("CALENDAR_DATE = Date('new')")

你应该把 new 作为变量

rowSelection=table.Select("CALENDAR_DATE = Date(" +new +")")

但不确定它是否会起作用,因为 Date 接受 Integer 而不是 Strings 所以你可能需要重新写入:

y = maxdate.Year
m= maxdate.Month 
d = 1 

rowSelection=table.Select("CALENDAR_DATE = Date("+ y + ',' + m +',' + d + ")")

或事先构建您的字符串,这是我将使用的方法:

y = maxdate.Year
m= maxdate.Month 
d = 1 
mystring =  "CALENDAR_DATE = Date("+ str(y) + ',' + str(m) +',' + str(d) + ")"

rowSelection=table.Select(mystring) 

上述方法之一应该有效,我将从最后一个设置字符串开始,因为不处理整数和字符串的许多转换是最有意义的。

如果您将此问题与示例 DXP 一起发布到 Tibco Answers 可能会有所帮助,因为将有一个示例 dxp 可以使用。但希望这可以帮助你。


推荐阅读