Jython – Decision Control

Jython – Decision Control ”; Previous Next Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed, if the condition is determined to be true, and optionally, other statements to be executed, if the condition is determined to be false. The following illustration shows the general form of a typical decision making structure found in most of the programming languages − Jython does not use curly brackets to indicate blocks of statements to be executed when the condition is true or false (as is the case in Java). Instead, uniform indent (white space from left margin) is used to form block of statements. Such a uniformly indented block makes the conditional code to be executed when a condition given in ‘if’ statement is true. A similar block may be present after an optional ‘else’ statement. Jython also provides the elif statement using which successive conditions can be tested. Here, the else clause will appear last and will be executed only when all the preceding conditions fail. The general syntax of using if..elif..else is as follows. if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s) In the following example, if ..elif ..else construct is used to calculate discount on different values of amount input by user. discount = 0 amount = input(“enter Amount”) if amount>1000: discount = amount*0.10 elif amount>500: discount = amount*0.05 else: discount = 0 print ”Discount = ”,discount print ”Net amount = ”,amount-discount The output of above code will be as shown below. enter Amount1500 Discount = 150.0 Net amount = 1350.0 enter Amount600 Discount = 30.0 Net amount = 570.0 enter Amount200 Discount = 0 Net amount = 200 Print Page Previous Next Advertisements ”;

Jython – Java Application

Jython – Java Application ”; Previous Next Download jython-standalone-2.7.0.jar – For embedding Jython in Java applications from their official downloads page: http://www.jython.org/downloads.html and include this jar file in Java CLASSPATH environment variable. This library contains the PythonInterpreter class. Using the object of this class, any Python script can be executed using the execfile() method. The PythonInterpreter enables you to make use of PyObjects directly. All objects known to the Jython runtime system are represented by an instance of the class PyObject or one of its subclasses. The PythonInterpreter class has some regularly used methods, which are explained in the table given below. Sr.No. Method & Description 1 setIn(PyObject) Set the Python object to use for the standard input stream 2 setIn(java.io.Reader) Set a java.io.Reader to use for the standard input stream 3 setIn(java.io.InputStream) Set a java.io.InputStream to use for the standard input stream 4 setOut(PyObject) Set the Python object to use for the standard output stream 5 setOut(java.io.Writer) Set the java.io.Writer to use for the standard output stream 6 setOut(java,io.OutputStream) Set the java.io.OutputStream to use for the standard output stream 7 setErr(PyObject) Set a Python error object to use for the standard error stream 8 setErr(java.io.Writer Set a java.io.Writer to use for the standard error stream 9 setErr(java.io.OutputStream) Set a java.io.OutputStream to use for the standard error stream 10 eval(String) Evaluate a string as Python source and return the result 11 eval(PyObject) Evaluate a Python code object and return the result 12 exec(String) Execute a Python source string in the local namespace 13 exec(PyObject) Execute a Python code object in the local namespace 14 execfile(String filename) Execute a file of Python source in the local namespace 15 execfile(java.io.InputStream) Execute an input stream of Python source in the local namespace 16 compile(String) Compile a Python source string as an expression or module 17 compile(script, filename) Compile a script of Python source as an expression or module 18 set(String name, Object value) Set a variable of Object type in the local namespace 19 set(String name, PyObject value) Set a variable of PyObject type in the local namespace 20 get(String) Get the value of a variable in the local namespace 21 get(String name, Classjavaclass Get the value of a variable in the local namespace. The value will be returned as an instance of the given Java class. The following code block is a Java program having an embedded Jython script “hello.py”.usingexecfile() method of the PythonInterpreter object. It also shows how a Python variable can be set or read using set() and get() methods. import org.python.util.PythonInterpreter; import org.python.core.*; public class SimpleEmbedded { public static void main(String []args) throws PyException { PythonInterpreter interp = new PythonInterpreter(); System.out.println(“Hello, world from Java”); interp.execfile(“hello.py”); interp.set(“a”, new PyInteger(42)); interp.exec(“print a”); interp.exec(“x = 2+2”); PyObject x = interp.get(“x”); System.out.println(“x: “+x); System.out.println(“Goodbye “); } } Compile and run the above Java program to obtain the following output. Hello, world from Java hello world from Python 42 x: 4 Goodbye Print Page Previous Next Advertisements ”;

Jython – Eclipse Plugin

Jython – Eclipse Plugin ”; Previous Next PyDev is an open source plugin for Eclipse IDE to enable development of projects in Python, Jython as well as IronPython. It is hosted at https://pydev.org. A step-by-step procedure to install PyDev plugin in Eclipse IDE is given below. Step 1 − Open Eclipse IDE and choose the Install New Software option from the Help menu. Step 2 − Enter http://pydev.org/updates in the textbox in front of work with label and click add. Choose all available entries in the list and click on Next. The Wizard will take a few minutes to complete the installation and it will prompt the IDE to be restarted. Step 3 − Now choose the preferences option from the Window menu. The Preferences dialog will open as shown below. Step 4 − Expand the Interpreters node and select Jython Interpreter in the left pane. On the right pane, click on new to give path to the jython.jar file. We are now ready to start a Jython project using Eclipse. Print Page Previous Next Advertisements ”;

Jython – Importing Java Libraries

Jython – Importing Java Libraries ”; Previous Next One of the most important features of Jython is its ability to import Java classes in a Python program. We can import any java package or class in Jython, just as we do in a Java program. The following example shows how the java.util packages are imported in Python (Jython) script to declare an object of the Date class. Live Demo from java.util import Date d = Date() print d Save and run the above code as UtilDate.py from the command line. Instance of the current date and time will be displayed. C:jython27bin>jython UtilDate.py Sun Jul 09 00:05:43 IST 2017 The following packages from the Java library are more often imported in a Jython program mainly because standard Python library either does not have their equivalents or are not as good. Servlets JMS J2EE Javadoc Swing is considered superior to other GUI toolkits Any Java package for that matter can be imported in a Jython script. Here, the following java program is stored and compiled in a package called foo. package foo; public class HelloWorld { public void hello() { System.out.println(“Hello World!”); } public void hello(String name) { System.out.printf(“Hello %s!”, name); } } This HelloWorld.class is imported in the following Jython Script. Methods in this class can be called from the Jython script importex.py. from foo import HelloWorld h = HelloWorld() h.hello() h.hello(“TutorialsPoint”) Save and execute the above script from the command line to get following output. C:jython27bin>jython importex.py Hello World! Hello TutorialsPoint! Print Page Previous Next Advertisements ”;

Jython – Overview

Jython – Overview ”; Previous Next Jython is the JVM implementation of the Python programming language. It is designed to run on the Java platform. A Jython program can import and use any Java class. Just as Java, Jython program compiles to bytecode. One of the main advantages is that a user interface designed in Python can use GUI elements of AWT, Swing or SWT Package. Jython, which started as JPython and was later renamed, follows closely the standard Python implementation called CPython as created by Guido Van Rossum. Jython was created in 1997 by Jim Hugunin. Jython 2.0 was released in 1999. Since then, Jython 2.x releases correspond to equivalent CPython releases. Jython 2.7.0 released in May 2015, corresponds to CPython 2.7. Development of Jython 3.x is under progress. Difference between Python and Java Following are the differences between Python and Java − Python is a dynamically typed language. Hence, the type declaration of variable is not needed. Java on the other hand is a statically typed language, which means that the type declaration of variable is mandatory and cannot be changed. Python has only unchecked exceptions, whereas Java has both checked and unchecked exceptions. Python uses indents for scoping, while Java uses matching curly brackets. Since Python is an interpreter-based language, it has no separate compilation steps. A Java program however needs to be compiled to bytecode and is in turn executed by a JVM. Python supports multiple inheritance, but in Java, multiple inheritance is not possible. It however has implementation of an interface. Compared to Java, Python has a richer built-in data structures (lists, dicts, tuples, everything is an object). Difference between Python and Jython Following are the differences between Python and Jython − Reference implementation of Python, called CPython, is written in C language. Jython on the other hand is completely written in Java and is a JVM implementation. Standard Python is available on multiple platforms. Jython is available for any platform with a JVM installed on it. Standard Python code compiles to a .pyc file, while Jython program compiles to a .class file. Python extensions can be written in C language. Extensions for Jython are written in Java. Jython is truly multi-threaded in nature. Python however uses the Global Interpreter Lock (GIL) mechanism for the purpose. Both implementations have different garbage collection mechanisms. In the next chapter, we will learn how to import the Java libraries in Jython. Print Page Previous Next Advertisements ”;