You are previewing Jython for Java Programmers.
O'Reilly logo
Jython for Java Programmers

Book Description

This book will help Java developers to increase application development and deployment, thus optimizing their overall efficiency.

A brief introduction is provided that shows the differences between Java and Jython, important to include so that the reader will have a better understanding of why their union is beneficial.

The remainder of the book teaches, through examples, how to use Jython. Intended for an experienced Java developer, this book assumes you understand the fundamentals to programming (i.e. loop statements, if.then, etc.). So, page space is focused on utilizing Jython with Java by covering the following: extending Python with java classes, modules, compiling, interpreters, design integration, etc.

www.newriders.com contains all code and applications developed in the book.

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Author
  3. About the Technical Reviewers
  4. Acknowledgments
  5. Tell Us What You Think
  6. Introduction
    1. What Is Jython?
    2. Why Jython?
      1. Seamless Access to Java Classes
      2. Efficiency
      3. Dynamic Types
      4. Introspection and Dynamic Execution
      5. First-Class Functions and Functional Programming
      6. Short Learning Curve
      7. Write Once, Run Anywhere
      8. Java Security
      9. Code Clarity
      10. Unicode and Internationalization
      11. Hotspot and Development Efficiency
    3. What You Can Expect from This Book
      1. Who This Book Is For
      2. Who This Book Isn’t For
    4. Additional Resources
  7. I. Jython
    1. 1. Jython Syntax, Statements, and Comments
      1. The Interactive Console
      2. Line Separators and Block Indentation Syntax
        1. Semicolon
        2. Backslash
        3. Open Groupings
        4. Code Blocks
      3. Comments
      4. Documentation Strings
      5. Statements
        1. Simple Statements
          1. assert
          2. assignment
          3. break
          4. continue
          5. del
          6. exec
          7. global
          8. import
          9. pass
          10. print
          11. raise
          12. return
        2. Compound Statements
          1. class
          2. def
          3. for
          4. if
          5. try
          6. while
      6. Comparing Jython and Java
    2. 2. Operators, Types, and Built-In Functions
      1. Identifiers
      2. Jython Data Objects
        1. Numeric Objects
        2. Sequence Objects
          1. PyString—The String Object
          2. PyTuple
          3. PyList
        3. Mapping Objects
          1. PyDictionary
          2. PyStringMap
        4. PyNone
      3. Operators
        1. Arithmetic Operators
        2. Shift Operators
        3. Comparison Operators
        4. Boolean Operators
        5. Sequence Operators
      4. Built-Ins
        1. Introspection Functions
        2. Numeric Functions
        3. Type Conversion Functions
        4. The Built-in File Function
        5. Sequence Functions
        6. Dynamic Language Functions
        7. Attribute Tools
        8. Functional Tools
        9. Miscellaneous Functions
      5. Comparison of Jython Data Types and Java Data Types
      6. Java Types
    3. 3. Errors and Exceptions
      1. Jython Exceptions
      2. Exception Handling
      3. The raise Statement
      4. Tracebacks
      5. The assert Statement and the __debug__ Variable
      6. The Warnings Framework
      7. Comparing Jython and Java
    4. 4. User-Defined Functions and Variable Scoping
      1. Defining Functions
        1. Indentation
        2. Return Values
        3. Documentation Strings
        4. Function Attributes
        5. Parameters
          1. Positional Parameters
          2. Default Values
          3. *params and Handling an Unknown Number of Positional Parameters
          4. Keyword Pairs as Parameters
          5. **kw params and Handling Unknown Key-Value Pairs
      2. Namespaces
        1. Two Static Scopes
        2. Statically Nested Scopes
        3. Special Variables in User-Defined Functions
      3. Recursion
      4. Built-In Functional Programming Tools
        1. lambda
        2. map()
        3. filter()
        4. reduce()
        5. zip()
        6. List Comprehension
        7. The Functional Programming Idiom
      5. Synchronization
    5. 5. Modules and Packages
      1. The import Statement
        1. import X
        2. import X as Y
        3. from X import Y
        4. from X import Y as Z
      2. A Comparison of Jython and Java
      3. Python’s package.module Hierarchy
        1. The sys.path Variable
        2. What Is a Module?
        3. Special Module Variables
        4. What Are Packages?
      4. Java’s package.class Hierarchy
      5. Reloading
    6. 6. Classes, Instances, and Inheritance
      1. Encapsulation, Abstraction, and Information Hiding
      2. Defining Jython Classes
      3. Jython Class and Instance Attributes
      4. Constructors
      5. Finalizer or Destructor
      6. Inheritance
        1. Subclassing Jython Classes
        2. Subclassing Java Interfaces
        3. Subclassing a Java Class
      7. Method Overloading
      8. Sample Classes
        1. Singletons
        2. File grep Utility
        3. HTTP Headers
        4. Trees
    7. 7. Advanced Classes
      1. Pre-Existing Class Attributes
        1. __name__
        2. __doc__
        3. __module__
        4. __dict__
        5. __bases__
      2. Pre-Existing Instance Attributes
        1. __class__
        2. __dict__
      3. Special Methods for General Customization
        1. __init__
        2. __del__
        3. __repr__
        4. __str__
      4. Dynamic Attribute Access
        1. __getattr__
        2. __setattr__
        3. __delattr__
      5. The Callable Hook—__call__
      6. Special Comparison Methods
        1. Three-Way, or Poor, Comparisons
        2. Rich Comparisons
        3. __hash__
      7. Object “Truth”
      8. Emulating Built-In Data Objects
        1. Emulating Sequences
          1. __len__
          2. __getitem__
          3. __setitem__
          4. __delitem__
          5. Sequence Concatenation and Multiplication
          6. Slices
          7. __contains__
          8. UserList
        2. Emulating Mappings
        3. Emulating Numeric Types
  8. II. Jython Internals and Integrating Jython With Java
    1. 8. Compiling Jython with jythonc
      1. What Is jythonc?
      2. Compiling a Module with jythonc
      3. Paths and Compiled Jython
        1. Set the python.home Property in the JVM
        2. Explicitly Add Directories to sys.path Within Modules
        3. Add to the python.path or python.prepath Property
        4. Freeze an Application
        5. Write a Custom __import__() Hook
      4. jythonc Options
      5. Java-Compatible Classes
        1. An Example Java-Compatible Jython Class
        2. Module-Global Objects and Java-Compatible Classes
    2. 9. Embedding and Extending Jython in Java
      1. Embedding Jython
        1. PythonInterpreter
          1. Initialization
            1. Properties and Initialization
            2. Instantiating the Interpreter
            3. Setting Output and Error Streams
            4. PySystemState
          2. Using the Interpreter
            1. exec
            2. execfile
            3. eval
          3. Compiling Code Object for Later Use
          4. Handling Exceptions in the Interpreter
          5. The set and get Methods
            1. set
            2. get
            3. __tojava__
          6. The getLocals and setLocals Methods
          7. imp and the Top-Level Script
      2. Embedding the InteractiveInterpreter
      3. Embedding an InteractiveConsole
      4. Extending Jython
        1. ClassDictInit
        2. __doc__ Strings
        3. Exceptions
        4. Parameters
        5. Importing Jython Modules in Java
        6. Working with PyObjects
        7. Writing Jython Classes in Java
        8. Adding a Java Class as a Built-In Jython Module
  9. III. Applications with Jython
    1. 10. GUI Development
      1. Comparing a Java and Jython GUI
      2. Bean Properties and Events
        1. Bean Properties
        2. Bean Properties and Tuples
        3. Bean Events
        4. Name Priorities
      3. The pawt Package
        1. GridBag
        2. colors
        3. test
        4. pawt.swing
      4. Examples
        1. Simple AWT Graphics
        2. Adding Events
        3. Images
        4. Menus and Menu Events
        5. Drag and Drop
        6. Swing
    2. 11. Database Programming
      1. DBM Files
      2. Serialization
        1. The marshal Module
        2. The pickle and cPickle Modules
        3. Shelves
        4. PythonObjectInputStream
      3. Database Management Systems
        1. MySQL
        2. PostgreSQL
      4. JDBC
        1. Connecting to the Database
          1. JDBC URLs
          2. JDBC Connection
            1. Connection Scripts
            2. Connection Dialog
        2. DatabaseMetaData
        3. Statements
        4. The ResultSet
        5. Prepared Statements
        6. Transactions
      5. zxJDBC
        1. Connecting to the Database
        2. Cursor
        3. zxJDBC and MetaData
        4. Prepared Statements
        5. Errors and Warnings
        6. dbexts
    3. 12. Server-Side Web Programming
      1. Jython Servlet Containers
      2. Defining Simple Servlet Classes
        1. A Simple Java Servlet
        2. A Simple Jython Servlet
        3. Testing the Java and Jython Servlets
          1. Installing Tomcat
          2. Installing the Java Servlet
          3. Installing the Jython Servlet
            1. Compiling a Jython Servlet with jythonc
            2. Adding jython.jar to the classpath
            3. Making Jython’s lib Directory Available to Servlets
            4. Testing the Jython Servlet
      3. More About GenericServlet
        1. init(ServletConfig) Method
        2. service(ServletRequest, ServletResponse) Method
        3. destroy() Method
      4. HttpServlet
        1. HttpServlet Methods
        2. HttpServlet Example
        3. HttpServletRequest and HttpServletResponse
      5. PyServlet
        1. Installing PyServlet
        2. Testing PyServlet
      6. Cookies
      7. Sessions
      8. Databases and Servlets
      9. JSP
        1. jythonc-Compiled Classes and JSP
        2. Embedding a PythonInterpreter in JSP
        3. A Jython Taglib
        4. BSF
  10. IV. Appendix
    1. A. Jython Statements and Built-In Functions Quick Reference