Python Programming with the Java Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython

 
   
  Table of Contents
Python Programming with the Java Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython
By Richard Hightower
   
Publisher : Addison Wesley
Pub Date : June 28, 2002
ISBN : 0-201-61616-5
Pages : 640

Characterized by ease of use, richness of expression, and concise syntax, Python has remained a premier programming language for more than a decade, and is used by novices and professionals alike. In particular, its close relationship to Java(TM) makes the two languages, when used in combination, ideal for Web and distributed enterprise application development.

This tutorial begins with coverage of some of the basics of Python programming. Using plenty of skill-building exercises and interactive programming sessions, this book will help those new to programming develop an understanding of concepts and practical techniques. For experienced programmers, the book demonstrates Python's breadth of capabilities and shows the ways that Python interfaces with Java APIs for professional application development.

Python Programming with the Java(TM) Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython covers important topics such as:

  • Fundamental programming concepts, including statements, expressions, interpreters, and compilers

  • Python basics, including operators, string formatting, namespaces, classes, errors, and exceptions

  • Object-oriented programming concepts

  • File input/output

  • Python's intrinsic functions

  • Formatting, parsing, and manipulating strings

  • Interfacing with the Java APIs and working with Java Streams

  • Using Python and Java Swing to create GUIs

  • Working with SQL and JDBC(TM)

  • Python and Java applets

In addition, the book contains instructions for downloading and installing the Python language and the Java Development Kit (JDK). Terminology, definitions, explanations, and numerous code samples make this book a useful learning experience.

Whether you are a sophisticated computer user new to programming or a serious application developer, Python Programming with the Java(TM) Class Libraries will give you insight into the power of Python and the know-how to put it to work.

777

Copyright
    Preface
      A Very Short History
      What You Will Learn
      The Audience
      Acknowledgments
   
    Chapter 1.  Jython Overview
      Programming, Briefly
      Learning Python First
      Python the Language
      Starting with Python
      Basic Functions with Python
      Python as a Main Program
      The Fast Track
      The Power of Python
      Where Do You Go from Here?
      Summary
   
    Chapter 2.  Statements and Expressions
      Comments and Document Strings
      Statements
      Expressions
      Variables
      Data Types
      Python Collection Types
      Advanced Topic: Determining Types at Runtime
      Literals
      Summary
   
    Chapter 3.  Operators and String Formatting
      Operators
      Formatting StringsModulus
      Summary
   
    Chapter 4.  Control Flow
      The if Statement
      The while Statement
      The for Statement
      Putting It All Together
      Summary
   
    Chapter 5.  Organizing Your Code
      Evolution of a Programmer
      Code Blocks and Namespaces
      Modules
      Functions and Methods
      Putting Things Together
      Classes
      Packages
      Globals and the Global Statement
      Summary
   
    Chapter 6.  Object-Oriented Programming
      What Is OOP?
      Objects and Classes
      Special Class Methods
      Inheritance
      Polymorphism
      Summary
   
    Chapter 7.  Errors and Exceptions
      Syntax and Sequence Errors
      The Danger of Exceptions
      The try Statement
      The raise Statement
      Classes and Instances as Exceptions
      Getting the Most Out of Your Exceptions
      Summary
   
    Chapter 8.  Working with Files
      Simple File Operations
      Common File Methods
      Putting It All Together: The Address Book Example
      The Full address3.py Code
      Persisting Objects with pickle
      pickle and the Address Book Application
      Summary
   
    Chapter 9.  Built-In Functions
      Conversion
      Namespace: dir(), globals(), locals(), vars()
      Type Checking: callable(), type()
      Operations
      Advanced Topic: Functional Programming
      Advanced exec and eval
      Summary
   
    Chapter 10.  Working with Strings
      Conversion: atoi(), atof(), atol()
      Case Change: capitalize(), capwords(), swapcases(), lower(), upper()
      Finding: find(), rfind(), index(), rindex(), count(), replace()
      Splitting and Joining: split(), splitfields(), join(), joinfields()
      Stripping and Parsing: lstrip(), rstrip(), strip()
      Adjusting Text: ljust(), rjust(), center(), zfill(), expandtabs()
      Summary
   
    Chapter 11.  Interfacing with Java
      Using the Java APIs
      Java Types
      Java Constructors
      Java Arrays and jarray
      Java Arrays and Methods
      Bean Properties
      Properties
      Java Event Handling
      Subclassing Java Classes
      Advanced Topics
      Summary
   
    Chapter 12.  Working with Java Streams
      The Java Way of File Operations
      Text Streams
      Binary Streams: InputStream and OutputStream
      DataInput and DataOutput
      The File Class
      The RandomAccessFile Class
      The StreamTokenizer Class
      Persisting Objects with Java Streams
      Using Java Streams to Work with Memory
      Summary
   
    Chapter 13.  JFC Fundamentals
      Components and Containers
      JFrame
      Handling Events with JFrame
      The Python Way of Handling Events
      The Class Hierarchy for JFrame and Frame
      JPanel
      JLabel
      JComponent
      JButton
      JTextField
      JCheckBox
      JRadioButton
      List and JList
      Summary
   
    Chapter 14.  First Swing Application, Layout, and Menus
      Putting Things Together with Basic Java GUIs
      Adding an Input Form for an Address Entry: The Prototype
      Adding an Input Form for an Address Entry: First Cut
      Adding a Main Window for the Address Book Application: Prototype
      Adding a Main Window: First Cut
      Adding a Toolbar and a Dialog for the Address Book Application
      Menus
      Layout Managers
      A GridBagLayout Example
      Putting Things Together: Adding GridBagLayout to the Address Application
      Summary
   
    Chapter 15.  Graphics and Events
      A Quick Graphics Tour
      A Quick Tour of Common Events
      Putting Things Together: A Drawing Program
      The Complete Shapes and DrawShapes Modules
      Summary
   
    Chapter 16.  Advanced Swing
      JTable
      Working with Table Models
      Putting Things TogetherAdding a Table Model to the Address Book Application
      JTree
      JToolBar and Actions
      Summary
   
    Chapter 17.  SQL and JDBC
      A Quick and Dirty JDBC Session
      Programming with JDBC and SQL
      SQL Data Definition Language
      SQL Data Manipulation Language
      Putting It All TogetherAdding Database Support to the Address Book Application
      Summary
   
    Chapter 18.  Applets
      What Are Applets?
      Working with Applets
      Using an Applet as a Container
      Transforming the Address Book Application into an Applet
      Advanced Topic: AppletContext and AppletStub
      Summary
   
    Appendix A.  Installing Jython on Windows
      Installing Java Runtime
      Installing the J2SEStep by Step
      Installing Jython
   
    Appendix B.  Installing Jython on Linux
      Unpacking the Blackdown Package
      Unpacking the Sun Package
      Installing the Sun and Blackdown Distributions
      Installing Older Distributions
      Running Jython
   
    Appendix C.  The Power of Scripting
      Scripting Versus Programming Languages
      Java and Scripting
      Integrating Scripting with Jython
      Which Scripting Language to Choose
      Hello WorldThe Programming Rosetta Stone
      What Does It All Mean?
   
    Appendix D.  Java and Python: A Comparison
      Python 101
      A GUI Application
      A Statistics Application
      A String Parsing Example
      Embedding Jython in Java
   
    Appendix E.  Regular Expressions
      A Simple Example
      Pattern Characteristics
      Regular Expression Functions and Error and Flags Properties
      re Object Methods and Properties
      Match Object Methods and Properties
      Metacharacters
      Putting Things Together

Copyright

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.

The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.

The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact:

U.S. Corporate and Government Sales

(800) 382-3419

corpsales@pearsontechgroup.com

For sales outside of the U.S., please contact:

International Sales

(317) 581-3793

international@pearsontechgroup.com

Visit Addison-Wesley on the Web: www.awprofessional.com

Library of Congress Cataloging-in-Publication Data

Hightower, Richard.

Python programming with the Java class libraries : a tutorial for building Web and Enterprise applications with Jython / Richard Hightower.

p. cm.

ISBN 0-201-61616-5 (alk. paper)

1. Python (Computer program language) 2. Java (Computer program language) 3. Application softwareDevelopment. I. Title.

QA76.73.P98 H54 2002

005.2'762dc21

2002066565

Copyright 2003 by Pearson Education, Inc.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada.

For information on obtaining permission for use of material from this work, please submit a written request to:

Pearson Education, Inc.

Rights and Contracts Department

75 Arlington Street, Suite 300

Boston, MA 02116

Fax: (617) 848-7047

Text printed on recycled paper

1 2 3 4 5 6 7 8 9 10CRS0605040302

First printing, July 2002

Preface

Python is a dynamic programming language with the power of well-known languages such as Java, C++, and Smalltalk. In fact, Python is leaner and meaner than any of these languages and yet very expressive; it doesn't talk much, but it has a lot to say.

Python has another plus: the simplicity of languages such as VB Script and JavaScript, which makes it easy for beginning programmers to learn. Novices who know their way around a computer can pick it up quickly, particularly if they have ever created a spreadsheet that graphs and organizes data, written spreadsheet formulas, or created a Web page. For those who have, say, written macros and batch files or programmed in any language, Python will be a breeze.

A Very Short History

Python was derived from a language called ABC created by Guido van Rossum and others in the early 1980s. The hope was that ABC's designed-in ease of use would become popular with novices as a way to get up to programming speed quickly and painlessly. This hope didn't pan out, so van Rossum began a new project, Python, which was released in 1990. He didn't forget ABC; in fact, there's a lot of ABC in Python's concise syntax and elegant expression, as well as in its ease of use. The formula worked this time; Python succeeded where ABC failed, and it has stood the test of time.

What You Will Learn

Put simply, my goal with this book is to teach programming using Python. You'll learn the workings of Python and how to apply them, particularly for the following:

  • Abstract Windows Toolkit (AWT) and Swing application development

  • Java applet development

  • Internet programming

  • Regular expressions and pattern matching

You may not understand these concepts now, but you will by the end of the book.

If you're looking for a full language reference, you won't get it here. What you will get is a deep enough understanding of Python to get started on your own programming. I'm going to remind you often of my belief that the best way to learn programming is by doing it. That's why I've provided lots of hands-on exercises in the form of interactive programming sessions, which I hope you'll follow along with at the keyboard as you read.

The Audience

As a reader of this book, you may be one of the following:

  • A nonprogrammer who wants to learn a programming language

  • A novice programmer who wants to learn Python

For simplicity, I'll call you both novices. You know your computer and how to use it to get the job done.

If you're not a novice and you're reading this book, then you're an experienced programmer who wants to learn a higher-level language that's quicker and more powerful than the one you're using now. For simplicity, I'll call you programmers.

For Novices

You'll be introduced to Python in conversational English, with many step-by-step examples. Chapters 2 through 9 build progressively on the chapters preceding them, so you should be able to learn enough to begin writing useful Python programs. The goal is to give you as much of a headstart as possible. From Chapter 10 on you'll learn more about Python and about supporting Java and Python libraries on your own, as well as about Internet programming with Python.

For Programmers

Most likely you're an experienced Java programmer who's aware of the close relationship between Java and Python and how it can lead to better application development (we'll get into this more in Chapter 1). You can skip over or skim the basic material and concentrate on the Advanced Topic sections and the various sidebars that are geared to your level of understanding.

Now let's get into our tour of Pythonthe destination is worth the trip.

Acknowledgments

The author and publisher are grateful for the efforts of this book's developmental editor, Dianne Cannon Wood, and reviewer, Barry Busler. Countless hours and suggestions were extremely helpful during the book's production stages. Thanks.