How to use JPython 1:1 module

Why does this not work? Knime says that it cannot add cellY and cellY, so how to do it?

dts = inData0.getDataTableSpec()
xCol = dts.findColumnIndex(“X”)
yCol = dts.findColumnIndex(“Y”)

iterator = inData0.iterator()
while iterator.hasNext():
row = iterator.next()

cellX = row.getCell(xCol)
cellY = row.getCell(yCol)

newVal = cellX + cellY

newDoubleCell = DoubleCell(newVal)

newRow = AppendedColumnRow(row,[newDoubleCell])
outContainer.addRowToTable(newRow)

Though I don't know Python, the following lines seem to be the problem (at least they would be in Java):

cellX = row.getCell(xCol) cellY = row.getCell(yCol)

A DataCell does not automatically contain a double, thus, in Java it must be stated explicitely that you are interested in the double value:

double cellX = ((DoubleCell)row.getCell(xCol)).getDoubleValue(); double cellY = ((DoubleCell)row.getCell(yCol)).getDoubleValue();

Does calling the method getDoubleValue() solve the problem?

Type casts don’t work in Python but Fabian was at least partially right - you
need to get the Double Value out of the cell before adding the values.
So the correct lines are:

...
while iterator.hasNext():
   row = iterator.next()

   cellX = row.getCell(xCol).getDoubleValue()
   cellY = row.getCell(yCol).getDoubleValue()

   newVal = cellX + cellY
...

Cheers, Michael

Thank You, this works perfectly.

Nevertheless, this brings me back to the question: Where can I find such a description?
I expected that I have to do something like this, since I do als develop with Java. In the
Python module classes I coulnd’t find the basic existing methods. In the documentation
also not. So I played a little bit with Python-like mehtods but failed.

So, how can I help myself?

Furthermore: Can I import and use modules? E.g. matplotlib or numpy?

Hi Michael, Specifically the documentation for the jython extensions is poor. These nodes were contributed to KNIME; we didn’t author them. In fact, no one of us is really a python programmer. But back to your question: As far as I know the available methods are very much in line with the java-methods that we have in KNIME core (they are exposed through jython). The documentation for the java API is available on the web: www.knime.org/api As an example, see the DataRow class, on which you have called getCell(int). There are two more methods that you can call: getKey() and getNumCells(). However, you can’t use all classes available in KNIME, instead it is limited to the classes available in the following packages:

As for the integration of external libs: That is is not possible for what I know because the nodes use jython and not python (there is a FAQ on jython.org where it states that one of the limitations of jython compared to python is that “Jython programs cannot use CPython extension modules written in C”). Hope that helps Bernd