Python and Kepler
Details about the Python Actor
Kepler includes the Ptolemy II Python actor that uses Jython, a Java implementation of Python.
As of November 2011, the Kepler development tree was using Jython 2.5.2, see How to Determine Which Version of Jython is being used.
Kepler has the Python Script actor, which has no ports; to view or edit its Python script, select Configure (or double click on the icon).
See the Ptolemy II PythonScript documentation for details.
Contents
- External Resources
- Mailing List Questions about Python and Kepler
- How Jython find imports
- The Two Types of Python Actors in Ptolemy II
- How to Determine Which Version of Jython is being used
External Resources
- The Introduction to Kepler - Python tutorial.
- Kepler UAL Python Actor User guide - "Kepler's ualpyactor actor inserts Python commands and scripts into workflows, making it easy to use the data manipulation and plotting functions of Python." This probably does not use Jython, but uses the full Python?
- Synchronizing Two Model Using Ptolemy II - Uses the Python actor
- There are Kepler Python demonstrations in the outreach directory, to run one, use:
ant run-only -Dworkflow=../outreach/workflows/demos/Python/PythonDialogExample.xml
- Bug 138 - Editing Python Scripts is difficult
Mailing List Questions about Python and Kepler
The Kepler-users mailing list has posts about Python and Kepler. Unfortunately, there is no direct way to search the mailing list archives, we suggest searching for the terms "kepler python". Below are some post about Python and Kepler:
python and kepler - ideas about interfacing to an external Python interpreter (October, 2011)
Using swing from the Python Actor; skeleton in syspath? - includes information about how Jython searches for packages (March, 2011)
Ideas about creating a new type for the Python tuple type (July, 2009)
How Jython finds imports
See Chapter 8: Modules and Packages for Code Reuse for details, but basically, the Python sys.path variable stores the search path.
See Re: Using swing from the Python Actor; skeleton in syspath? for a Ptolemy model that prints the contents of the sys.path variable and discussion about a bug. In November, 2011, that model was checked in to the Ptolemy II tree as ptolemy/actor/lib/python/test/PythonSysPath.xml.
In my default Ptolemy setup, sys.path was:
/Users/cxh/ptII/vendors/jython/Lib,/Users/cxh/ptII/vendors/jython/jython.jar/Lib,__classpath__,/Users/cxh/ptII/vendors/jython/Lib
In November, 2011, the Kepler devel tree was updated with outreach/workflows/demos/Python/PythonImport.xml. That model reads a Jython module in ptolemy/src/lib/Lib/PtPythonSquare.py
- Start a development version of Kepler, and open outreach/workflows/demos/Python/PythonImport.xml
Right click on the PythonScript actor, select Configure and note that jythonClassName is set to "PtPythonSquare" - Double click on the PythonScript actor and be sure that the script looks like:
import PtPythonSquare PtPythonSquare = reload(PtPythonSquare)
- The Jython model to be run is at ptolemy/src/lib/Lib/PtPythonSquare.py. Note that you may need to create the Lib/ directory. Note that the first lib directory starts with a lower case el and the second starts with an uppercase el. Jython uses the Jython sys.path variable to find modules. To get the value of sys.path, either:
- Right click on the PythonScript actor, select "Listen to Actor", run the model and look at the value printed on stdout
OR
- Run the model at $PTII/ptolemy/actor/lib/python/test/PythonSysPath.xml
The contents of PtPythonSquare.py should be:
class Main : def fire(self) : token = self.input.get(0) self.output.broadcast(token.multiply(token)) return
- Right click on the PythonScript actor, select "Listen to Actor", run the model and look at the value printed on stdout
- Run the model, note that the output is the square of the input.
- Edit PtPythonSquare.py and change
multiply(token)
to
add(token) - Run the model again, note that the output is the twice the input.
Note that the python.home Java property controls what the location of the Python home directory. In Ptolemy, try:
java -classpath $PTII:${PTII}/lib/jython.jar -Dpython.home=/tmp/bar ptolemy.vergil.VergilApplication PythonSysPath.xml
The two types of Python Actors in Ptolemy II
Kepler uses Ptolemy II as its execution engine. Ptolemy II includes two different types of Python Actors. In the Ptolemy II vergil configuration, under More Libraries -> Python, there are two actors:
- PythonActor - Right click and select Open Actor and an editor appears that contains the Python contents of the actor
- PythonScript - Double click and an editor appears that contains the Python contents of the actor
By default, Kepler is shipped with the 2nd actor, PythonScript.
Both actors actually use the Java class ptolemy.actor.lib.python.PythonScript. The PythonActor merely has a _tableauFactory set in
the configuration. ptolemy/actor/lib/python/python.xml contains xml that sets the _tableauFactory.
When the user drags a PythonActor in, the _tableauFactory is set.
How to determine what version of Jython is being used
As of November 5, 2011, the Kepler development tree uses Jython-2.5.2. The easiest way to determine this is to look at the size of jython.jar. If jython.jar is 10mb, then Jython-2.5.2 is being used. If jython.jar is around 1Mb, then Jython-2.2 or 2.2.1 is being used.
Kepler-2.2 uses Jython-2.2 or 2.1
The Kepler ptolemy/src/lib/jython.jar file comes from the Ptolemy II tree file $PTII/lib/jython.jar
In October, 2011, the changelog for the jython.jar file in the Ptolemy II tree at $PTII/lib/jython.jar is below:
------------------------------------------------------------------------
r62357 | cxh | 2011-11-05 11:05:34 -0700 (Sat, 05 Nov 2011) | 1 line
Upgraded to Jython-2.5.2.
------------------------------------------------------------------------
r60529 | cxh | 2011-03-10 13:14:39 -0800 (Thu, 10 Mar 2011) | 1 line
Revert to jython (presumably) 2.2 because jython2.5.8 does not have org.python.core.PyJavaInstance
------------------------------------------------------------------------
r60525 | cxh | 2011-03-10 07:22:55 -0800 (Thu, 10 Mar 2011) | 1 line
Updated to jython 2.5.2
------------------------------------------------------------------------
r48715 | cxh | 2008-03-12 16:18:19 -0700 (Wed, 12 Mar 2008) | 2 lines
Updated jython.jar to a standalone version so that the javaos Jython package could be found. See ptolemy/actor/lib/python/test/auto/PythonReadFile.xml
------------------------------------------------------------------------
r48497 | cxh | 2008-02-18 16:16:18 -0800 (Mon, 18 Feb 2008) | 2 lines
Updated to Jython 2.2.1
------------------------------------------------------------------------
r46882 | cxh | 2007-09-17 08:08:28 -0700 (Mon, 17 Sep 2007) | 2 lines
Updated to Jython 2.2
------------------------------------------------------------------------
r32050 | liuxj | 2004-03-25 17:58:35 -0800 (Thu, 25 Mar 2004) | 2 lines
recompiled to work with j2sdk 1.5.0
------------------------------------------------------------------------
r27667 | cxh | 2003-04-30 20:41:06 -0700 (Wed, 30 Apr 2003) | 2 lines
jython.jar
------------------------------------------------------------------------