Ironpython - 我如何在额外的代码行中引用计算变量

Ironpython - how do I refer to to a calculated variable in additional lines of code

我正在 Spotfire 中使用 IronPython。

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

我有一直到 datatable.Select 语句的工作代码,我需要在其中进行匹配。如果我根据 "Date(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

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

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

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

你应该把 new 作为变量

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

但不确定它是否有效,因为 Date 接受整数而非字符串所以您可能需要重写:

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) 

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

如果您 post 这个问题带有示例 DXP to Tibco Answers 可能会有更多帮助,因为将有一个示例 dxp 可以使用。但希望这可以帮助你。