I’m trying to make a dynamic table, based on the script below, this is just for one product, but depending on what location I pick, only want to run queries for available products. I keep getting this:
java.lang.ClassCastException: java.lang.ClassCastException: class org.python.core.PyFloat cannot be cast to class org.python.core.PySequence (org.python.core.PyFloat and org.python.core.PySequence are in unnamed module of loader java.net.URLClassLoader @37827677)
Here is the script I’m using. Right now it’s only for one location, but I’ll loop thru it as I add locations, or do I need to scrap this and try a different approach.
if (event.propertyName == 'yearly' or 'monthly' or 'daily') and (event.newValue == 1):
start=event.source.parent.getComponent('LOADS').start
end=event.source.parent.getComponent('LOADS').end
loc=event.source.parent.Loc
prods=system.db.runPrepQuery("select * from Location where location="+"'"+loc+"'")
qry="SELECT coalesce(sum(weight/2000.0),0.0) as "+'"'+"Total Tons" + '"' + " from truck_counts where location="+ "'" + loc + "'" +" and t_stamp between " + "'" + start + "'" + "and " + "'" + end + "'"
master=system.db.runPrepQuery(qry)
total=master[0][0]
toth='"'+"<html><br> <html> <br><Center>Total "+'"'
data=[]
data.append(total)
header=[]
header.append(toth)
if prods[0][4]==1:
prod1='SELECT coalesce(sum(weight/2000.0),0.0) as '+'"'+"Product I/II"+'"'+" from truck_counts where location="+"'"+loc+"'"+" and product_type='Product_I_II' and t_stamp between "+"'"+start+"'"+ "and "+"'"+end+"'"
p1=system.db.runPrepQuery(prod1)
p1d=p1[0][0]
p1h='"'+"<html><br><Center>Product <br><Center>I/II "+'"'
data.extend([p1d])
header.extend(p1h)
d1=system.dataset.toDataSet(header ,data)
event.source.data=d1
Haven’t looked at anything else yet, but if (event.propertyName == 'yearly' or 'monthly' or 'daily') isn’t doing what you think it is - it’s checking:
does event.propertyName equal ‘yearly’
or is “monthly” true
or is “daily” true
In Python, any non-zero length string is true, so your check isn’t doing what it’s supposed to. It should be if event.propertyName in {"yearly", "monthly", "daily"} (the curly braces initialize a set; you could also use square brackets to initialize a list, or parentheses for a tuple)
That aside, is there a line number on the exception you’re getting?
The top part is working, just let’s me know when to run the script. I’m getting the error when I go to create the dataset, so line 27.
This is what each list returns:
[20.0, 0.0] # data
[’
Total ’, ‘ Product I/II ’] #header I have this header static in another script for something else and works great for increasing the column header height on the table so I can multi-line it.
Another aside, this is not the proper use of prepared SQL statements and leaves you open for an injection attack if loc, start, or end are human inputs
query="""
SELECT COALESCE(SUM(weight/2000.0),0.0) as 'Product I/II'
FROM truck_counts
WHERE
location = ?
AND product_type = 'Product_I_II'
AND t_stamp BETWEEN ? AND ?
"""
args = [location, start_time, end_time]
p1=system.db.runPrepQuery(query, args)
There’s no line 27 in what you posted, but I’m pretty sure the issue is this: header.extend(p1h)
If you only have a single item, you should be using append - extend is used to add all the elements from one sequence to the end of another.
Figured out what the problem was…gotta thank Nick Mudge in another post for jogging a few brain cells. I forgot the data part of that fuction is a list of lists. Just created another list, and after appending everything in my original list, appended that list to the new one list. Works great now.
testsHeaders = [ 'Sample '+str(i) for i,j in valuesIn]
testsData = [[ float(j) for i,j in valuesIn]]
print testsHeaders
print testsData
print len(testsHeaders),len(testsData)
datasetTests = system.dataset.toDataSet(testsHeaders, testsData)
When I added an additional set of brackets for testsData it worked.
Without them, I got the error about the floats.
There are 240 i, and 240 j.
i is an integer. j is a float.
Why did I need the second brackets to make this work?
Snip of results. I don't understand why it works with 240, 1 for lengths instead of 240, 240.
Because, system.dataset.toDataSet() expects a List of Lists as the second argument.
Each list is a row, each item in each list is a value in that column.
You get the error stating that a float can not be converted to a PySequence because the function treats it like a list of lists, but then the first "list" is actually a float.