Skip to main content

2010 | Buch

The Definitive Guide To Jython

Python For The Java™ Platform

verfasst von: Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki

herausgegeben von: Steve Anglin, Duncan Parkes, Clay Andres, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh, Mary Tobin, Tracy Brown Collins

Verlag: Apress

insite
SUCHEN

Über dieses Buch

Jython is an open source implementation of the high-level, dynamic, object-oriented scripting language Python seamlessly integrated with the Java platform. The predecessor to Jython, JPython, is certified as 100% Pure Java. Jython is freely available for both commercial and noncommercial use and is distributed with source code. Jython is complementary to Java.

The Definitive Guide to Jython, written by the official Jython team leads, covers Jython 2.5 (or 2.5.x)—from the basics to more advanced features. This book begins with a brief introduction to the language and then journeys through Jython’s different features and uses.

The Definitive Guide to Jython is organized for beginners as well as advanced users of the language. The book provides a general overview of the Jython language itself, but it also includes intermediate and advanced topics regarding database, web, and graphical user interface (GUI) applications; Web services/SOA; and integration, concurrency, and parallelism, to name a few.

Inhaltsverzeichnis

Frontmatter

Jython Basics: Learning the Language

Frontmatter
Chapter 1. Language and Syntax
Abstract
Elegant is an adjective that is often used to describe the Python language. The word elegant is defined as “pleasingly graceful and stylish in appearance or manner.” Uncomplicated and powerful could also be great words to assist in the description of this language. It is a fact that Python is an elegant language that lets one create powerful applications in an uncomplicated manner. The ability to make reading and writing complex software easier is the objective of all programming languages, and Python does just that.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 2. Data Types and Referencing
Abstract
Programming languages and applications need data. We define applications to work with data, and we need to have containers that can be used to hold it. This chapter is all about defining containers and using them to work with application data. Whether the data we are using is coming from a keyboard entry or if we are working with a database, there needs to be a way to temporarily store it in our programs so that it can be manipulated and used. Once we’re done working with the data then these temporary containers can be destroyed in order to make room for new constructs.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 3. Operators, Expressions, and Program Flow
Abstract
The focus of this chapter is an in-depth look at each of the ways that we can evaluate code, and write meaningful blocks of conditional logic. We’ll cover the details of many operators that can be used in Python expressions. This chapter will also cover some topics that have already been discussed in more meaningful detail such as the looping constructs, and some basic program flow.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 4. Defining Functions and Using Built-ins
Abstract
Functions are the fundamental unit of work in Python. A function in Python performs a task and returns a result. In this chapter, we will start with the basics of functions. Then we look at using the built-in functions. These are the core functions that are always available, meaning they don’t require an explicit import into your namespace. Next we will look at some alternative ways of defining functions, such as lambdas and classes. We will also look at more advanced types of functions, namely closures and generator functions.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 5. Input and Output
Abstract
A program means very little if it does not take input of some kind from the program user. Likewise, if there is no form of output from a program then one may ask why we have a program at all. Input and output operations can define the user experience and usability of any program. This chapter is all about how to put information or data into a program, and then how to display it or save it to a file. This chapter does not discuss working with databases, but rather, working at a more rudimentary level with files. Throughout this chapter you will learn such techniques as how to input data for a program via a terminal or command line, likewise, you will learn how to read input from a file and write to a file. After reading this chapter, you should know how to persist Python objects to disk using the pickle module and also how to retrieve objects from disk and use them.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 6. Object-Oriented Jython
Abstract
This chapter is going to cover the basics of object-oriented programming. We’ll start with covering the basic reasons why you would want to write object-oriented code in the first place, and then cover all the basic syntax, and finally we’ll show you a non-trivial example.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 7. Exception Handling and Debugging
Abstract
Any good program makes use of a language’s exception handling mechanisms. There is no better way to frustrate an end-user then by having them run into an issue with your software and displaying a big ugly error message on the screen, followed by a program crash. Exception handling is all about ensuring that when your program encounters an issue, it will continue to run and provide informative feedback to the end-user or program administrator. Any Java programmer becomes familiar with exception handling on day one, as some Java code won’t even compile unless there is some form of exception handling put into place via the try-catch-finally syntax. Python has similar constructs to that of Java, and we’ll discuss them in this chapter.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 8. Modules and Packages for Code Reuse
Abstract
Up until this chapter, we have been looking at code at the level of the interactive console and simple scripts. This works well for small examples, but when your program gets larger, it becomes necessary to break programs up into smaller units. In Jython, the basic building block for these units in larger programs is the module.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki

Using the Language

Frontmatter
Chapter 9. Scripting With Jython
Abstract
In this chapter, we will look at scripting with Jython. For our purposes, we will define “scripting” as the writing of small programs to help out with daily tasks. These tasks are things like deleting and creating directories, managing files and programs, and anything else that feels repetitive that you might be able to express as a small program. In practice, however, scripts can become so large that the line between a script and a full sized program can blur.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 10. Jython and Java Integration
Abstract
Java integration is the heart of Jython application development. Most Jython developers are either Python developers that are looking to make use of the vast library of tools that the JVM has to offer, or Java developers that would like to utilize the Python language semantics without migrating to a completely different platform. The fact is that most Jython developers are using it so that they can take advantage of the vast libraries available to the Java world, and in order to do so there needs to be a certain amount of Java integration in the application. Whether you plan to use some of the existing Java libraries in your application, or you’re interested in mixing some great Python code into your Java application, this chapter is geared to help with the integration.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 11. Using Jython in an IDE
Abstract
In this chapter, we will discuss developing Jython applications using two of the most popular integrated development environments, Eclipse and Netbeans. There are many other development environments available for Python and Jython today; however, these two are perhaps the most popular and contain the most Jython-specific tools. Eclipse has had a plug-in known as PyDev for a number of years, and this plug-in provides rich support for developing and maintaining Python and Jython applications alike. Netbeans began to include Python and Jython support with version 6.5 and later. The Netbeans IDE also provides rich support for development and maintenance of Python and Jython applications.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 12. Databases and Jython: Object Relational Mapping and Using JDBC
Abstract
In this chapter, we will look at zxJDBC package, which is a standard part of Jython since version 2.1 and complies with the Python 2.0 DBI standard. zxJDBC can be an appropriate choice for simple one-off scripts where database portability is not a concern. In addition, it’s (generally) necessary to use zxJDBC when writing a new dialect for SQLAlchemy or Django. (But that’s not strictly true: you can use pg8000, a pure Python DBI driver, and of course write your own DBI drivers. But please don’t do that.) So knowing how zxJDBC works can be useful when working with these packages. However, it’s too low level for us to recommend for more general usage. Use SQLAlchemy or Django if at all possible. Finally, JDBC itself is also directly accessible, like any other Java package from Jython. Simply use the java.sql package. In practice this should be rarely necessary.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki

Developing Applications with Jython

Frontmatter
Chapter 13. Simple Web Applications
Abstract
One of the major benefits of using Jython is the ability to make use of Java platform capabilities programming in the Python programming language instead of Java. In the Java world today, the most widely used web development technique is the Java servlet. Now in JavaEE, there are techniques and frameworks used so that we can essentially code HTML or other markup languages as opposed to writing pure Java servlets. However, sometimes writing a pure Java servlet still has its advantages. We can use Jython to write servlets and this adds many more advantages above and beyond what Java has to offer because now we can make use of Python language features as well. Similarly, we can code web start applications using Jython instead of pure Java to make our lives easier. Coding these applications in pure Java has proven sometimes to be a difficult and sometimes grueling task. We can use some of the techniques available in Jython to make our lives easier. We can even code WSGI applications with Jython making use of the modjy integration in the Jython project.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 14. Web Applications With Django
Abstract
Django is a modern Python web framework that redefined web development in the Python world. A fullstack approach, pragmatic design, and superb documentation are some of the reasons for its success.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 15. Introduction to Pylons
Abstract
Although Django is currently the most popular web framework for Python, it is by no means your only choice. Where Django grew out of the needs of newsrooms to implement content management solutions rapidly, Pylons grew out of a need to build web applications in environments that may have existing databases to integrate with, and the applications don’t fit neatly into the class of applications that are loosely defined in the “content management” space.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 16. GUI Applications
Abstract
The C implementation of Python comes with Tkinter for writing Graphical User Interfaces (GUIs). The GUI toolkit that you get automatically with Jython is Swing, which is included with the Java Platform by default. Similar to CPython, there are other toolkits available for writing GUIs in Jython. Because Swing is available on any modern Java installation, we will focus on the use of Swing GUIs in this chapter.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 17. Deployment Targets
Abstract
Deployment of Jython applications varies from container to container. However, they are all very similar and usually allow deployment of WAR file or exploded directory web applications. Deploying to “the cloud” is a different scenario all together. Some cloud environments have typical Java application servers available for hosting, while others such as the Google App Engine run a bit differently. In this chapter, we’ll discuss how to deploy web-based Jython applications to a few of the more widely used Java application servers. We will also cover deployment of Jython web applications to the Google App Engine and mobile devices. Although many of the deployment scenarios are quite similar, this chapter will walk through some of the differences from container to container.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki

Strategy and Technique

Frontmatter
Chapter 18. Testing and Continuous Integration
Abstract
Nowadays, automated testing is a fundamental activity in software development. In this chapter, you will see a survey of the tools available for Jython in this field. These tools range from common tools used in the Python world to aid with unit testing, to more complex tools available in the Java world that can be extended or driven using Jython.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Chapter 19. Concurrency
Abstract
Supporting concurrency is increasingly important. In the past, mainstream concurrent programming generally meant ensuring that the code interacting with relatively slow network, disk, database, and other I/O resources did not unduly slow things down. Exploiting parallelism was typically only seen in such domains as scientific computing with the apps running on supercomputers.
Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki
Backmatter
Metadaten
Titel
The Definitive Guide To Jython
verfasst von
Josh Juneau
Jim Baker
Victor Ng
Leo Soto
Frank Wierzbicki
herausgegeben von
Steve Anglin
Duncan Parkes
Clay Andres
Mark Beckner
Ewan Buckingham
Gary Cornell
Jonathan Gennick
Jonathan Hassell
Michelle Lowman
Matthew Moodie
Duncan Parkes
Jeffrey Pepper
Frank Pohlmann
Douglas Pundick
Ben Renow-Clarke
Dominic Shakeshaft
Matt Wade
Tom Welsh
Mary Tobin
Tracy Brown Collins
Copyright-Jahr
2010
Verlag
Apress
Electronic ISBN
978-1-4302-2528-7
Print ISBN
978-1-4302-2527-0
DOI
https://doi.org/10.1007/978-1-4302-2528-7