fable.python.jep
Class FableJep

java.lang.Object
  extended by fable.python.jep.FableJep

public class FableJep
extends java.lang.Object

Wrapper for Java embedded Python (Jep) to create and return a Jep interpreter.

Author:
andy

Constructor Summary
FableJep()
           
FableJep(boolean requireErrorMessage)
           
 
Method Summary
 void close()
          Close Jep.
 boolean eval(java.lang.String str)
           
 void flushStdout()
          Flush sys.stdout
 java.lang.Boolean getBooleanValue(java.lang.String str)
          Return the value as a Boolean using Jep.
static FableJep getFableJep()
          Call this if you want to have this instance only
 java.lang.Float getFloatValue(java.lang.String str)
          Cast a variable in Python to float and return the value as a Float using Jep.
 java.lang.Integer getIntegerValue(java.lang.String str)
          Cast a variable in python to int and return the value as an Integer using Jep.
 jep.Jep getJep()
           
static java.lang.String getPythonPath()
           
 java.lang.String getStringValue(java.lang.String str)
          Return the value as a String using Jep.
 float[] getValue_floatarray(java.lang.String str)
          Return a float array from Python using Jep.
 java.lang.Object getValue(java.lang.String str)
          Return a python variable as a Java Object type using Jep.
 boolean isRequireErrorMessage()
           
static void jepImportModules(jep.Jep _jep, java.lang.String _modules)
           
static void jepImportModules(jep.Jep _jep, java.lang.String _modules, boolean requireErrorMessage)
          Import a module for the jep instance.
static void jepImportSpecificDefinition(jep.Jep _jep, java.lang.String _from, java.lang.String _import)
           
static void jepImportSpecificDefinition(jep.Jep _jep, java.lang.String _from, java.lang.String _import, boolean requireMessage)
          Import a specific definition into the current namespace, ie "from ImageD11 import peaksearcher" and add the module path to sys.path.
static void jepSetPythonPath(jep.Jep jep)
          Prepend the pythonPath to sys.path.
 void outputConsole(FableMessageConsole console)
           
static void record(boolean b)
           
 void redirectStderr(java.lang.String filename)
          Redirect stdout in a python program to a file.
 void redirectStdout(java.lang.String filename)
          Redirect stdout in a python program to a file.
static java.lang.String replaceEscapeSequences(java.lang.String input)
          Replaces escape sequences in the given string with a \ plus the escape sequence character.
 void set(java.lang.String str1, double dbl)
           
 void set(java.lang.String str, float f1)
           
 void set(java.lang.String str1, int str2)
           
 void set(java.lang.String str1, java.lang.String str2)
           
static void setPythonPath(java.lang.String pythonPath)
           
 void setRequireErrorMessage(boolean requireErrorMessage)
           
static void setScriptFileName(java.lang.String name)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FableJep

public FableJep()
         throws java.lang.Throwable
Throws:
jep.JepException
java.lang.Throwable

FableJep

public FableJep(boolean requireErrorMessage)
         throws java.lang.Throwable
Throws:
jep.JepException
java.lang.Throwable
Method Detail

getJep

public jep.Jep getJep()

getFableJep

public static FableJep getFableJep()
                            throws java.lang.Throwable
Call this if you want to have this instance only

Throws:
jep.JepException
java.lang.Throwable

jepSetPythonPath

public static void jepSetPythonPath(jep.Jep jep)
                             throws jep.JepException
Prepend the pythonPath to sys.path.

Throws:
jep.JepException

jepImportModules

public static void jepImportModules(jep.Jep _jep,
                                    java.lang.String _modules)
                             throws jep.JepException
Throws:
jep.JepException

jepImportModules

public static void jepImportModules(jep.Jep _jep,
                                    java.lang.String _modules,
                                    boolean requireErrorMessage)
                             throws jep.JepException
Import a module for the jep instance.

Parameters:
_jep -
_modules -
Throws:
jep.JepException
jep.JepException

jepImportSpecificDefinition

public static void jepImportSpecificDefinition(jep.Jep _jep,
                                               java.lang.String _from,
                                               java.lang.String _import)
                                        throws jep.JepException
Throws:
jep.JepException

jepImportSpecificDefinition

public static void jepImportSpecificDefinition(jep.Jep _jep,
                                               java.lang.String _from,
                                               java.lang.String _import,
                                               boolean requireMessage)
                                        throws jep.JepException
Import a specific definition into the current namespace, ie "from ImageD11 import peaksearcher" and add the module path to sys.path.

Parameters:
_jep - The jep instance.
_from - The module to import.
_import - The specific definition to import.
Throws:
jep.JepException

eval

public boolean eval(java.lang.String str)
             throws jep.JepException
Throws:
jep.JepException

set

public void set(java.lang.String str1,
                java.lang.String str2)
         throws jep.JepException
Throws:
jep.JepException

set

public void set(java.lang.String str1,
                double dbl)
         throws jep.JepException
Throws:
jep.JepException

set

public void set(java.lang.String str1,
                int str2)
         throws jep.JepException
Throws:
jep.JepException

set

public void set(java.lang.String str,
                float f1)
         throws jep.JepException
Throws:
jep.JepException

getValue

public java.lang.Object getValue(java.lang.String str)
                          throws jep.JepException
Return a python variable as a Java Object type using Jep.

Be careful this methods returns any of the following Java types e.g. Float, Integer or String, depending on the Python variable. This can cause problems at run time.

Parameters:
str - The name of python variable e.g. "a".
Returns:
The Java object (could be Float or Int or String).
Throws:
jep.JepException

getBooleanValue

public java.lang.Boolean getBooleanValue(java.lang.String str)
Return the value as a Boolean using Jep. Returns false if the value is not defined in Python.

Parameters:
str - The name of the Python variable e.g. "a".
Returns:
The value cast to Boolean.
Throws:
jep.JepException

getIntegerValue

public java.lang.Integer getIntegerValue(java.lang.String str)
Cast a variable in python to int and return the value as an Integer using Jep. Returns null if the value is not defined in Python.

Parameters:
str - The name of the Python variable e.g. "a".
Returns:
The value cast to Integer.

getFloatValue

public java.lang.Float getFloatValue(java.lang.String str)
Cast a variable in Python to float and return the value as a Float using Jep. Returns null if the value is not defined in Python.

Parameters:
str - The name of the Python variable e.g. "a".
Returns:
The value cast to Float.
Throws:
jep.JepException

getStringValue

public java.lang.String getStringValue(java.lang.String str)
Return the value as a String using Jep. Returns null if the value is not defined in Python.

Parameters:
str - The name of the Python variable e.g. "a".
Returns:
The value cast to Float.
Throws:
jep.JepException

getValue_floatarray

public float[] getValue_floatarray(java.lang.String str)
                            throws jep.JepException
Return a float array from Python using Jep.

Parameters:
str - The name of the Python float array.
Returns:
The float[].
Throws:
jep.JepException

redirectStdout

public void redirectStdout(java.lang.String filename)
                    throws jep.JepException
Redirect stdout in a python program to a file.

Throws:
jep.JepException

redirectStderr

public void redirectStderr(java.lang.String filename)
                    throws jep.JepException
Redirect stdout in a python program to a file.

Throws:
jep.JepException

flushStdout

public void flushStdout()
                 throws jep.JepException
Flush sys.stdout

Throws:
jep.JepException

outputConsole

public void outputConsole(FableMessageConsole console)

record

public static void record(boolean b)

setScriptFileName

public static void setScriptFileName(java.lang.String name)

close

public void close()
Close Jep. This releases the Python subinterpreter and Python will garbage collect any object created during the script's execution. This must be called when needed or you may see large memory usage.


getPythonPath

public static java.lang.String getPythonPath()
Returns:
the pythonpath

setPythonPath

public static void setPythonPath(java.lang.String pythonPath)
Parameters:
pythonpath - the pythonpath to set

replaceEscapeSequences

public static java.lang.String replaceEscapeSequences(java.lang.String input)
Replaces escape sequences in the given string with a \ plus the escape sequence character. For example: "\t" -> "\\t" (printed as TAB -> \t). This is needed when returning filenames from Python using Jep.

Parameters:
input -
Returns:

isRequireErrorMessage

public boolean isRequireErrorMessage()

setRequireErrorMessage

public void setRequireErrorMessage(boolean requireErrorMessage)