简体   繁体   中英

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

I am working with IronPython inside Spotfire.

I need to extract the maximum date value from a range filter, then use that value to filter a table of exchange rates.

I have working code right up to the datatable.Select statement in which I need to do the match. If I do it based on "Date(2020,3,1)" - which is the row commented out - then the match works and the correct result is returned, however I cannot get the syntax correct for using a calculated variable "newdate" in place of the Date(xxx) statement. I am still learning python and have not come across this before.

Code as below - any help would be greatly appreciated.

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

I think your variable new would print out as 2020,01,01

In this line new is a string so Date() cannot extract a date.

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

You should put new as variable

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

but not sure it'll work as Date takes in Integer not Strings So you might have to re-write to:

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

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

or build your String before hand which is the method I would use:

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

rowSelection=table.Select(mystring) 

One of the above ways should work, I'd start with the last one setting your string before as it makes the most sense to not deal with to many conversions of integers and strings.

If you post this question with an example DXP to Tibco Answers could possible help more since will have an example dxp to work with. but hopefully this helps you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM