This is an introductory tutorial, which covers the basics of Jython and explains how to handle its various modules and sub-modules.

Similar documents
Before you start with this tutorial, you need to know basic Java programming.

Parrot is a virtual machine designed to efficiently compile and execute bytecode for interpreted languages.

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Python - Variable Types. John R. Woodward

This tutorial explains how you can use Gradle as a build automation tool for Java as well as Groovy projects.

Memcached is an open source, high-performance, distributed memory object caching system.

This tutorial has been prepared for beginners to help them understand the basic functionalities of Gulp.

This tutorial is designed for all those software professionals who are keen on learning the basics of Clojure and how to put it into practice.

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer. PySpark

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

jmeter is an open source testing software. It is 100% pure Java application for load and performance testing.

This tutorial provides a basic level understanding of the LOLCODE programming language.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

The current topic: Python. Announcements. Python. Python

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

This is a brief tutorial that explains how to make use of Sqoop in Hadoop ecosystem.

Java Bytecode (binary file)

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

This tutorial will take you through simple and practical approaches while learning AOP framework provided by Spring.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Jenkins

You must have a basic understanding of GNU/Linux operating system and shell scripting.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer ASP.NET WP

This is an introductory tutorial designed for beginners to help them understand the basics of Radius.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Meteor

This tutorial also elaborates on other related methodologies like Agile, RAD and Prototyping.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Before proceeding with this tutorial, you should have a good understanding of the fundamental concepts of marketing.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. RichFaces

SAP Lumira is known as a visual intelligence tool that is used to visualize data and create stories to provide graphical details of the data.

About Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Euphoria

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

This tutorial will show you, how to use RSpec to test your code when building applications with Ruby.

CS 231 Data Structures and Algorithms, Fall 2016

Chapter 2 Getting Started with Python

Scalable Vector Graphics commonly known as SVG is a XML based format to draw vector images. It is used to draw twodimentional vector images.

Microsoft Excel is a spreadsheet tool capable of performing calculations, analyzing data and integrating information from different programs.

This tutorial is designed for all Java enthusiasts who want to learn document type detection and content extraction using Apache Tika.

And Parallelism. Parallelism in Prolog. OR Parallelism

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright DAX

In this tutorial, we are going to learn how to use the various features available in Flexbox.

Full file at

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Java Programming. Price $ (inc GST)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer DBMS

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer. Logstash

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

CS Advanced Unix Tools & Scripting

This tutorial provides a basic understanding of how to generate professional reports using Pentaho Report Designer.

This tutorial covers a foundational understanding of IPC. Each of the chapters contain related topics with simple and useful examples.

Welcome to Python 3. Some history

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

So, this tutorial is divided into various chapters and describes the 5G technology, its applications, challenges, etc., in detail.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. TurboGears

Variables, Constants, and Data Types

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

Adobe Flex Tutorial i

This tutorial is designed for software programmers who would like to learn the basics of ASP.NET Core from scratch.

This tutorial explains the key concepts of Web Dynpro with relevant screenshots for better understanding.

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

COLLEGE OF ENGINEERING, NASHIK-4

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Graph Theory

Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

This tutorial has been prepared for beginners to help them understand the simple but effective SEO characteristics.

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

In this tutorial, we will understand how to use the OpenNLP library to build an efficient text processing service.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Matrex Table of Contents

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Gerrit

Introduction to Python

Table of Contents EVALUATION COPY

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

Typescript on LLVM Language Reference Manual

You should have a basic understanding of Relational concepts and basic SQL. It will be good if you have worked with any other RDBMS product.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Programming. Syntax and Semantics

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

This tutorial provides a basic understanding of the infrastructure and fundamental concepts of managing an infrastructure using Chef.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

"Charting the Course... Java Programming Language. Course Summary

Jython. secondary. memory

4 Programming Fundamentals. Introduction to Programming 1 1

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Avro

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Dart is an open-source general-purpose programming language. It is originally developed by Google and later approved as a standard by ECMA.

Python in 10 (50) minutes

Python memento TI-Smart Grids

Extending Jython. with SIM, SPARQL and SQL

Getting started with Java

Testing is the process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Django

This is a small tutorial where we will cover all the basic steps needed to start with Balsamiq Mockups.

Key Differences Between Python and Java

Pace University. Fundamental Concepts of CS121 1

Transcription:

About the Tutorial Jython is the JVM implementation of the Python programming language. It is designed to run on the Java platform. Jython was created in 1997 by Jim Hugunin. It closely follows the standard Python implementation called CPython. Jython 2.7.0 was released in May 2015, which corresponds to CPython 2.7. This is an introductory tutorial, which covers the basics of Jython and explains how to handle its various modules and sub-modules. Audience This tutorial will be helpful for Java programmers who want to utilize important features of Python i.e. Simple Syntax, Rich Data Types and Rapid Application Development in Java code. This will also be useful for Pythonistas to import feature Java class library into the Python Environment. This tutorial is made to make the programmers comfortable in getting started with Jython and its various functions. Prerequisites Since Jython helps in integrating two very popular programming technologies namely Java and Python, a reasonable knowledge of both languages is required. Copyright and Disclaimer Copyright 2017 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@tutorialspoint.com i

Table of Contents About the Tutorial... i Audience... i Prerequisites... i Copyright and Disclaimer... i Table of Contents... ii 1. Jython Overview... 1 2. Jython Installation... 2 3. Jython Importing Java Libraries... 6 4. Jython Variables and Data Types... 8 Jython Numbers... 8 Jython Strings... 9 Jython Lists... 9 Jython Tuples... 10 Jython Dictionary... 10 5. Jython Using Java Collection Types... 12 Jarray Class... 12 6. Jython Decision Control... 14 7. Jython Loops... 16 The WHILE Loop... 16 The FOR Loop... 17 8. Jython Functions... 19 9. Jython Modules... 21 10. Jython Package... 22 11. Jython Java Application... 24 12. Jython Eclipse Plugin... 26 13. Jython A Project in Eclipse... 29 14. Jython NetBeans Plugin & Project... 31 Jython Project in NetBeans... 34 15. Jython Servlets... 38 16. Jython JDBC... 41 17. Jython Using the Swing GUI library... 43 ii

18. Jython Layout Management... 46 Absolute Layout... 46 Jython FlowLayout... 48 Jython GridLayout... 50 Jython BorderLayout... 52 Jython BoxLayout... 52 Jython GroupLayout... 55 19. Jython Event Handling... 58 Jython JRadioButton Event... 62 Jython JCheckBox Event... 64 Jython JList Event... 65 20. Jython Menus... 68 21. Jython Dialogs... 71 iii

1. JYTHON OVERVIEW Jython 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. 4

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. 5

2. JYTHON INSTALLATION Jython Before installation of Jython 2.7, ensure that the system has JDK 7 or more installed. Jython is available in the form of an executable jar file. Download it from http://www.jython.org/downloads.html and either double click on its icon or run the following command: java -jar jython_installer-2.7.0.jar An installation wizard will commence with which installation options have to be given. Here is the systematic installation procedure. The first step in the wizard asks you to select the language. 6

The second step prompts you to accept the licence agreement. In the next step, choose the installation type. It is recommended to choose the Standard installation. 7

The next screen asks your confirmation about your options and proceeds to complete the installation. 8

9

The installation procedure might take some time to complete. After the installation is complete, invoke jython.exe from the bin directory inside the destination directory. Assuming that Jython is installed in C:\jython27, execute the following from the command line. C:\jython27\bin\jython A Python prompt (>>>) will appear, in front of which any Python statement or Python script can be executed. 10

3. JYTHON IMPORTING JAVA LIBRARIES Jython 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. 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:\jython27\bin>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); } 11

} 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:\jython27\bin>jython importex.py Hello World! Hello TutorialsPoint! 12

4. JYTHON VARIABLES AND DATA TYPES Jython Variables are named locations in computer s memory. Each variable can hold one piece of data in it. Unlike Java, Python is a dynamically typed language. Hence while using Jython also; prior declaration of data type of variable is not done. Rather than the type of variable deciding which data can be stored in it, the data decides the type of variable. In the following example, a variable is assigned an integer value. Using the type() built-in function, we can verify that the type of variable is an integer. But, if the same variable is assigned a string, the type() function will string as the type of same variable. > x=10 >>> type(x) <class 'int'> >>> x="hello" >>> type(x) <class 'str'> This explains why Python is called a dynamically typed language. The following Python built-in data types can also be used in Jython: Number String List Tuple Dictionary Python recognizes numeric data as a number, which may be an integer, a real number with floating point or a complex number. String, List and Tuple data types are called sequences. Jython Numbers In Python, any signed integer is said to be of type int. To express a long integer, letter L is attached to it. A number with a decimal point separating the integer part from a fractional component is called float. The fractional part may contain an exponent expressed in the scientific notation using E or e. A Complex number is also defined as numeric data type in Python. A complex number contains a real part (a floating-point number) and an imaginary part having j attached to it. 13

In order to express a number in the Octal or the Hexadecimal representation, 0O or 0X is prefixed to it. The following code block gives examples of different representations of numbers in Python. int -> 10, 100, -786, 80 long -> 51924361L, -0112L, 47329487234L float -> 15.2, -21.9, 32.3+e18, -3.25E+101 complex -> 3.14j, 45.j, 3e+26J, 9.322e-36j Jython Strings A string is any sequence of characters enclosed in single (e.g. hello ), double (e.g. hello ) or triple (e.g. hello o hello ) quotation marks. Triple quotes are especially useful if content of the string spans over multiple lines. The Escape sequence characters can be included verbatim in triple quoted string. The following examples show different ways to declare a string in Python. str= hello how are you? str= Hello how are you? str="""this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up. """ The third string when printed, will give the following output. this is a long string that is made up of several lines and non-printable characters such as TAB ( ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up. 14

Jython Lists A List is a sequence data type. It is a collection of comma-separated items, not necessarily of the same type, stored in square brackets. Individual item from the List can be accessed using the zero based index. The following code block summarizes the usage of a List in Python. list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5] The following table describes some of the most common Jython Expressions related to Jython Lists. Jython Expression len(list) Description Length List[2]=10 Updation Del List[1] Deletion List.append(20) Append List.insert(1,15) Insertion List.sort() Sorting Jython Tuples A tuple is an immutable collection of comma-separated data items stored in parentheses. It is not possible to delete or modify an element in tuple, nor is it possible to add an element to the tuple collection. The following code block shows Tuple operations. tup1=('physics','chemistry,1997,2000); tup2=(1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5] 15

Jython Dictionary The Jython Dictionary is similar to Map class in Java Collection framework. It is a collection of key-value pairs. Pairs separated by comma are enclosed in curly brackets. A Dictionary object does not follow zero based index to retrieve element inside it as they are stored by hashing technique. The same key cannot appear more than once in a dictionary object. However, more than one key can have same associated values. Different functions available with Dictionary object are explained below: dict={'011':'new Delhi','022':'Mumbai','033':'Kolkata'} print "dict[ 011 ]: ",dict['011'] print "dict['age']: ", dict['age'] The following table describes some of the most common Jython Expressions related to Dictionary. Jython Expression Description dict.get( 011 ) Search len(dict) Length dict[ 044 ]= Chennai Append del dict[ 022 ] Delete dict.keys() list of keys dict.values() List of values dict.clear() Removes all elements 16

End of ebook preview If you liked what you saw Buy it from our store @ https://store.tutorialspoint.com 17