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 org.python.core.PyCode |
compile(URL fileUrl)
Compiles the Jython script at the given
fileUrl into a PyCode object. |
static org.python.core.PyCode |
compileString(String script)
Compiles the given Jython script into a
PyCode object. |
static Object |
evaluate(File scriptFile,
Object... args)
Evaluates the given Jython script, returning the result as its equivalent Java type.
|
static Object |
evaluate(InputStream inputStream,
Object... args)
Evaluates the given Jython script, returning the result as its 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 its equivalent Java type. |
static Object |
evaluate(URL scriptUrl,
Object... args)
Evaluates the Jython script at the given
scriptUrl, returning the result as its 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. |
static void |
execute(URL scriptUrl,
Object... args)
Executes the Jython script at the given
scriptUrl 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(URL fileUrl) throws JythonScriptException
fileUrl into a PyCode object.fileUrl - the URL to a Jython file to compileJythonScriptException - when the given URL is empty, null, or not a valid pathpublic 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 org.python.core.PyCode compileString(String script) throws JythonScriptException
PyCode object.script - the Jython script to compile (this should be actual Jython code represented as a String)JythonScriptException - when the given script is null or emptypublic static Object evaluate(String scriptPath, Object... args) throws JythonScriptException
scriptPath, returning the result as its equivalent Java type.
Accepts optional arguments to be passed to the script at runtime. args should be interpreted as 'sys.argv'
arguments in the given script. Note that the arguments passed in here will begin at the first index in a Jython
scripts sys.argv list.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(URL scriptUrl, Object... args) throws JythonScriptException
scriptUrl, returning the result as its equivalent Java type.
Accepts optional arguments to be passed to the script at runtime. args should be interpreted as 'sys.argv'
arguments in the given script. Note that the arguments passed in here will begin at the first index in a Jython
scripts sys.argv list.scriptUrl - the URL to a Jython script to executeargs - arguments to be passed to the scriptJythonScriptException - when the given script is null, a directory, or cannot be foundpublic static Object evaluate(File scriptFile, Object... args) throws JythonScriptException
args should be interpreted as 'sys.argv' arguments in the given
script. Note that the arguments passed in here will begin at the first index in a Jython scripts sys.argv list.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
args should be interpreted as 'sys.argv' arguments in the given
script. Note that the arguments passed in here will begin at the first index in a Jython scripts sys.argv list.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
args should be interpreted as 'sys.argv' arguments in the given
script. Note that the arguments passed in here will begin at the first index in a Jython scripts sys.argv list.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. args should be interpreted as 'sys.argv' arguments in the given script. Note that the arguments
passed in here will begin at the first index in a Jython scripts sys.argv list.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(URL scriptUrl, Object... args) throws JythonScriptException
scriptUrl with optional arguments passed to the script at
runtime. args should be interpreted as 'sys.argv' arguments in the given script. Note that the arguments
passed in here will begin at the first index in a Jython scripts sys.argv list.scriptUrl - the URL to a 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(File scriptFile, Object... args) throws JythonScriptException
args should be
interpreted as 'sys.argv' arguments in the given script. Note that the arguments passed in here will begin at
the first index in a Jython scripts sys.argv list.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
args should be
interpreted as 'sys.argv' arguments in the given script. Note that the arguments passed in here will begin at
the first index in a Jython scripts sys.argv list.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
args should be
interpreted as 'sys.argv' arguments in the given script. Note that the arguments passed in here will begin at
the first index in a Jython scripts sys.argv list.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 © 2017. All rights reserved.