public class JythonScript extends Object
JythonScript provides an easy to use wrapper for executing and/or evaluating Python expressions or scripts
for JVM-based languages. JythonScript packages the Jython standalone JAR as part of it's distribution, so no further
dependencies should be required.
JythonScript operates on a couple of select criteria:
String file path, File, or InputStream#evaluate(...) or #execute(...) function, such as evaluate(String, Object...) or execute(String, Object...) MUST be setup as a
standard Python main module. This means that the following code must exist:
if __name__ == '__main__':
...
...
#evaluate(...) functions, and expecting a valid result to be
returned, you MUST push the resulting value to a local Python variable named 'result', otherwise JythonScript
will not be able to locate this value within the Python interpreter.#compile(...) functions that compile the given scripts into PyCode objects. For
speed increases at runtime, it's better to pre-compile Jython scripts with these functions, maintain the PyCode
objects via an in-memory cache or local variable, and execute or evaluate against the compiled scripts. JythonScript
provides the necessary evaluate(PyCode, Object...) and execute(PyCode, Object...) methods to
foster these speed increases.
| Modifier and Type | Method and Description |
|---|---|
static org.python.core.PyCode |
compile(File file)
Compiles the given Jython script into a
PyCode object. |
static org.python.core.PyCode |
compile(String filePath)
Compiles the Jython script at the given
filePath into a PyCode object. |
static Object |
evaluate(File scriptFile,
Object... args)
Evaluates the given Jython script, returning the result as it's equivalent Java type.
|
static Object |
evaluate(InputStream inputStream,
Object... args)
Evaluates the given Jython script, returning the result as it's equivalent Java type.
|
static Object |
evaluate(org.python.core.PyCode pyCode,
Object... args)
Evaluates the given Jython script, returning the result as it's equivalent Java type.
|
static Object |
evaluate(String scriptPath,
Object... args)
Evaluates the Jython script at the given
scriptPath, returning the result as it's equivalent Java type. |
static void |
execute(File scriptFile,
Object... args)
Executes the given Jython script with optional arguments passed to the script at runtime.
|
static void |
execute(InputStream inputStream,
Object... args)
Executes the given Jython script with optional arguments passed to the script at runtime.
|
static void |
execute(org.python.core.PyCode pyCode,
Object... args)
Executes the given Jython script with optional arguments passed to the script at runtime.
|
static void |
execute(String scriptPath,
Object... args)
Executes the Jython script at the given
scriptPath with optional arguments passed to the script at
runtime. |
public static org.python.core.PyCode compile(String filePath) throws JythonScriptException
filePath into a PyCode object.filePath - the absolute path of the Jython file to compileJythonScriptException - when the given file path is empty or nullpublic static org.python.core.PyCode compile(File file) throws JythonScriptException
PyCode object.file - the Jython file to compileJythonScriptException - when the given file is null or is not a file (i.e. a directory)public static Object evaluate(String scriptPath, Object... args) throws JythonScriptException
scriptPath, returning the result as it's equivalent Java type.
Accepts optional arguments to be passed to the script at runtime.scriptPath - the fully qualified path of the Jython script to executeargs - arguments to be passed to the scriptJythonScriptException - when the given file path is null, a directory, or cannot be foundpublic static Object evaluate(File scriptFile, Object... args) throws JythonScriptException
scriptFile - the Jython script to executeargs - arguments to be passed to the scriptJythonScriptException - when the given file is null, a directory, or cannot be foundpublic static Object evaluate(InputStream inputStream, Object... args) throws JythonScriptException
inputStream - the InputStream that represents the Jython script to be executedargs - arguments to be passed to the scriptJythonScriptException - when a script execution error occurs or when a local Python variable named 'result' is not foundpublic static Object evaluate(org.python.core.PyCode pyCode, Object... args) throws JythonScriptException
pyCode - the compiled Jython script to evaluateargs - arguments to be passed to the scriptJythonScriptException - when a script execution error occurs or when a local Python variable named 'result' is not foundpublic static void execute(String scriptPath, Object... args) throws JythonScriptException
scriptPath with optional arguments passed to the script at
runtime.scriptPath - the fully qualified path of the Jython script to executeargs - arguments to be passed to the scriptJythonScriptException - when the given file path is null, a directory, or cannot be foundpublic static void execute(File scriptFile, Object... args) throws JythonScriptException
scriptFile - the Jython script to executeargs - arguments to be passed to the scriptJythonScriptException - when the given file is null, a directory, or cannot be foundpublic static void execute(InputStream inputStream, Object... args) throws JythonScriptException
inputStream - the InputStream that represents the Jython script to be executedargs - arguments to be passed to the scriptJythonScriptException - when the given inputstream is null or a script execution error occurspublic static void execute(org.python.core.PyCode pyCode,
Object... args)
throws JythonScriptException
pyCode - the compiled Jython script to evaluateargs - arguments to be passed to the scriptJythonScriptException - when the given PyCode is null or a script execution error occursCopyright © 2016. All rights reserved.