Using Jython as a scripting engine in a project

Finally I have a proper usecase for using Jython as an integrated scripting engine in a Java-project. The opportunity came in a current project, where we do collaboration with third party who developes a certain calculation model which will be shared intellectual property between us and also released to the general public. Usage on Jython allows us to model the calculation model as python code, create it easily readable and runnable code on almost any and every desktop computer – and run the same code on our backend. Which is nice.

Based on the documentation and quick tests, integrating Jython is a snap and using JSR 233 or native interface are almost similar, though I’d prefer JSR 233 scripting api.

ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

TestObject foo = new TestObject();
foo.setName("Name of the object");
engine.put("foo", foo);
engine.eval("foo.name = Changed name");
TestObject grab = (TestObject) engine.get("foo");

Works like a charm.

http://wiki.python.org/jython/UserGuide#embedding-jython

This entry was posted in java, programming. Bookmark the permalink.

Vastaa

Täytä tietosi alle tai klikkaa kuvaketta kirjautuaksesi sisään:

WordPress.com-logo

Olet kommentoimassa WordPress.com -tilin nimissä. Log Out / Muuta )

Twitter-kuva

Olet kommentoimassa Twitter -tilin nimissä. Log Out / Muuta )

Facebook-kuva

Olet kommentoimassa Facebook -tilin nimissä. Log Out / Muuta )

Google+ photo

Olet kommentoimassa Google+ -tilin nimissä. Log Out / Muuta )

Muodostetaan yhteyttä palveluun %s