The Definitive Guide to Jython: Python for the Java™ Platform

Book description

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 GUI applications; Web services/SOA; and integration, concurrency, and parallelism, to name a few.

Table of contents

  1. Copyright
  2. Foreword
  3. About the Authors
  4. About the Technical Reviewers
  5. Acknowledgments
  6. Introduction
  7. I. Jython Basics: Learning the Language
    1. 1. Language and Syntax
      1. 1.1. The Difference between Jython and Python
      2. 1.2. Installing and Configuring Jython
      3. 1.3. Identifiers and Declaring Variables
      4. 1.4. Reserved Words
      5. 1.5. Coding Structure
      6. 1.6. Operators
      7. 1.7. Expressions
      8. 1.8. Functions
      9. 1.9. Classes
      10. 1.10. Statements
        1. 1.10.1. if-elif-else Statement
        2. 1.10.2. print Statement
        3. 1.10.3. try-except-finally
        4. 1.10.4. raise Statement
        5. 1.10.5. import Statement
      11. 1.11. Iteration
        1. 1.11.1. While Loop
        2. 1.11.2. For Loop
      12. 1.12. Basic Keyboard Input
      13. 1.13. Other Python Statements
      14. 1.14. Documenting Code
      15. 1.15. Python Help
      16. 1.16. Summary
    2. 2. Data Types and Referencing
      1. 2.1. Python Data Types
        1. 2.1.1. Strings and String Methods
          1. 2.1.1.1. String Formatting
        2. 2.1.2. Lists, Dictionaries, Sets, and Tuples
          1. 2.1.2.1. Lists
            1. 2.1.2.1.1. Traversing and Searching Lists
          2. 2.1.2.2. List Comprehensions
          3. 2.1.2.3. Tuples
          4. 2.1.2.4. Dictionaries
          5. 2.1.2.5. Sets
          6. 2.1.2.6. Ranges
          7. 2.1.2.7. Range Format
        3. 2.1.3. Jython-specific Collections
        4. 2.1.4. Files
        5. 2.1.5. Iterators
        6. 2.1.6. Referencing and Copies
        7. 2.1.7. Garbage Collection
      2. 2.2. Summary
    3. 3. Operators, Expressions, and Program Flow
      1. 3.1. Types of Expressions
      2. 3.2. Mathematical Operations
        1. 3.2.1. Comparison Operators
        2. 3.2.2. Bitwise Operators
        3. 3.2.3. Augmented Assignment
        4. 3.2.4. Boolean Expressions
        5. 3.2.5. Conversions
      3. 3.3. Using Expressions to Control Program Flow
        1. 3.3.1. if-elif-else Statement
        2. 3.3.2. while Loop
        3. 3.3.3. continue Statement
        4. 3.3.4. break Statement
        5. 3.3.5. for Loop
        6. 3.3.6. Example Code
      4. 3.4. Summary
    4. 4. Defining Functions and Using Built-ins
      1. 4.1. Function Syntax and Basics
        1. 4.1.1. The def Keyword
        2. 4.1.2. Naming the Function
        3. 4.1.3. Function Parameters and Calling Functions
          1. 4.1.3.1. Recursive Function Calls
        4. 4.1.4. Function Body
          1. 4.1.4.1. Documenting Functions
          2. 4.1.4.2. Returning Values
          3. 4.1.4.3. Introducing Variables
          4. 4.1.4.4. Other Statements
          5. 4.1.4.5. Empty Functions
      2. 4.2. Miscellaneous Information for the Curious Reader
      3. 4.3. Built-in Functions
      4. 4.4. Alternative Ways to Define Functions
        1. 4.4.1. Lambda Functions
      5. 4.5. Generator Functions
        1. 4.5.1. Defining Generators
        2. 4.5.2. Generator Expressions
      6. 4.6. Namespaces, Nested Scopes, and Closures
      7. 4.7. Function Decorators
      8. 4.8. Coroutines
        1. 4.8.1. Decorators in Coroutines
        2. 4.8.2. Coroutine Example
      9. 4.9. Summary
    5. 5. Input and Output
      1. 5.1. Input from the Keyboard
        1. 5.1.1. sys.stdin and raw_input
        2. 5.1.2. Obtaining Variables from Jython Environment
      2. 5.2. File I/O
      3. 5.3. Pickle
        1. 5.3.1. Output Techniques
      4. 5.4. Summary
    6. 6. Object-Oriented Jython
      1. 6.1. Basic Syntax
        1. 6.1.1.
          1. 6.1.1.1.
            1. 6.1.1.1.1. Import pickle
      2. 6.2. Object Attribute Lookups
      3. 6.3. Inheritance and Overloading
      4. 6.4. Underscore Methods
      5. 6.5. Protocols
      6. 6.6. Default Arguments
      7. 6.7. Runtime Binding of Methods
      8. 6.8. Caching Attribute Access
      9. 6.9. Summary
    7. 7. Exception Handling and Debugging
      1. 7.1. Exception Handling Syntax and Differences with Java
        1. 7.1.1. Catching Exceptions
        2. 7.1.2. Raising Exceptions
      2. 7.2. Defining Your Own Exceptions
      3. 7.3. Issuing Warnings
      4. 7.4. Assertions and Debugging
      5. 7.5. Context Managers
      6. 7.6. Summary
    8. 8. Modules and Packages for Code Reuse
      1. 8.1. Imports for Reuse
        1. 8.1.1. Import Basics
          1. 8.1.1.1. breakfast.py
        2. 8.1.2. The Import Statement
      2. 8.2. An Example Program
        1. 8.2.1.
          1. 8.2.1.1. greetings.py
          2. 8.2.1.2. greet/__init__.py
          3. 8.2.1.3. greet/hello.py
          4. 8.2.1.4. greet/people.py
        2. 8.2.2. Trying Out the Example Code
      3. 8.3. Types of Import Statements
        1. 8.3.1. From Import Statements
        2. 8.3.2. Relative Import Statements
        3. 8.3.3. Aliasing Import Statements
        4. 8.3.4. Hiding Module Names
      4. 8.4. Module Search Path, Compilation, and Loading
        1. 8.4.1. Java Import Example
        2. 8.4.2. Module Search Path and Loading
      5. 8.5. Java Package Scanning
        1. 8.5.1. How Jython Finds the Jars and Classes to Scan
        2. 8.5.2. Compilation
      6. 8.6. Python Modules and Packages versus Java Packages
        1. 8.6.1. sys.path
        2. 8.6.2. Naming Python Modules and Packages
        3. 8.6.3. Proper Python Naming
      7. 8.7. Advanced Import Manipulation
        1. 8.7.1. Import Hooks
        2. 8.7.2. sys.path_hooks
        3. 8.7.3. sys.meta_path
      8. 8.8. Summary
  8. II. Using the Language
    1. 9. Scripting With Jython
      1. 9.1. Getting the Arguments Passed to a Script
      2. 9.2. Searching for a File
      3. 9.3. Manipulating Files
      4. 9.4. Making a Script a Module
      5. 9.5. Parsing Commandline Options
      6. 9.6. Compiling Java Source
      7. 9.7. Example Script: Builder.py
      8. 9.8. HelloWorld.java
      9. 9.9. Summary
    2. 10. Jython and Java Integration
      1. 10.1. Using Java Within Jython Applications
      2. 10.2. Using Jython Within Java Applications
        1. 10.2.1. Object Factories
          1. 10.2.1.1. One-to-One Jython Object Factories
          2. 10.2.1.2. Summary of One-to-One Object Factory
          3. 10.2.1.3. Making Use of a Loosely Coupled Object Factory
          4. 10.2.1.4. More Efficient Version of Loosely Coupled Object Factory
          5. 10.2.1.5. Returning __doc__ Strings
          6. 10.2.1.6. Applying the Design to Different Object Types
        2. 10.2.2. JSR-223
        3. 10.2.3. Utilizing PythonInterpreter
      3. 10.3. Summary
    3. 11. Using Jython in an IDE
      1. 11.1. Eclipse
        1. 11.1.1. Installing PyDev
        2. 11.1.2. Minimal Configuration
        3. 11.1.3. Hello PyDev!: Creating Projects and Executing Modules
        4. 11.1.4. Passing Command-line Arguments and Customizing Execution
        5. 11.1.5. Playing with the Editor
        6. 11.1.6. A Bit of Structure: Packages, Modules, and Navigation
        7. 11.1.7. Testing
        8. 11.1.8. Adding Java Libraries to the Project
      2. 11.2. Debugging
        1. 11.2.1. Conclusion about Eclipse
      3. 11.3. Netbeans
      4. 11.4. IDE Installation and Configuration
      5. 11.5. Advanced Python Options
      6. 11.6. General Python Usage
      7. 11.7. Standalone Jython Apps
      8. 11.8. Jython and Java Integrated Apps
        1. 11.8.1. Using a JAR or Java Project in Your Jython App
        2. 11.8.2. Using Jython in Java
      9. 11.9. The Netbeans Python Debugger
      10. 11.10. Other Netbeans Python Features
      11. 11.11. Summary
    4. 12. Databases and Jython: Object Relational Mapping and Using JDBC
      1. 12.1. ZxJDBC—Using Python's DB API via JDBC
        1. 12.1.1. Getting Started
        2. 12.1.2. Connections
        3. 12.1.3. ZxJDBC.lookup
          1. 12.1.3.1. Cursors
          2. 12.1.3.2. Creating and Executing Queries
        4. 12.1.4. Prepared Statements
        5. 12.1.5. Resource Management
        6. 12.1.6. Metadata
        7. 12.1.7. Data Manipulation Language and Data Definition Language
          1. 12.1.7.1. Calling Procedures
          2. 12.1.7.2. Customizing zxJDBC Calls
            1. 12.1.7.2.1. Life Cycle
            2. 12.1.7.2.2. Developer Support
            3. 12.1.7.2.3. Binding Prepared Statements
            4. 12.1.7.2.4. Building Results
        8. 12.1.8. History
      2. 12.2. Object Relational Mapping
        1. 12.2.1. SqlAlchemy
        2. 12.2.2. Installation
        3. 12.2.3. Using SqlAlchemy
        4. 12.2.4. Hibernate
        5. 12.2.5. Entity Classes and Hibernate Configuration
        6. 12.2.6. Jython Implementation Using the Java Entity Classes
      3. 12.3. Summary
  9. III. Developing Applications with Jython
    1. 13. Simple Web Applications
      1. 13.1. Servlets
        1. 13.1.1. Configuring Your Web Application for Jython Servlets
        2. 13.1.2. Writing a Simple Servlet
        3. 13.1.3. Using JSP with Jython
          1. 13.1.3.1. Configuring for JSP
          2. 13.1.3.2. Coding the Controller/View
      2. 13.2. Applets and Java Web Start
        1. 13.2.1. Coding a Simple GUI-Based Web Application
          1. 13.2.1.1. Object Factory Application Design
        2. 13.2.2. Distributing via Standalone JAR
      3. 13.3. WSGI and Modjy
        1. 13.3.1. Running a Modjy Application in Glassfish
      4. 13.4. Summary
    2. 14. Web Applications With Django
      1. 14.1. Getting Django
      2. 14.2. A Quick Tour of Django
        1. 14.2.1. Starting a Project (and an "App")
        2. 14.2.2. Models
        3. 14.2.3. Bonus: The Admin
        4. 14.2.4. Views and Templates
        5. 14.2.5. Reusing Templates Without "include": Template Inheritance
        6. 14.2.6. Forms
        7. 14.2.7. Feeds
        8. 14.2.8. Comments
        9. 14.2.9. And More...
      3. 14.3. J2EE Deployment and Integration
        1. 14.3.1. Deploying Your First Application
        2. 14.3.2. Disabling PostgreSQL Logins
        3. 14.3.3. A Note About WAR Files
        4. 14.3.4. Extended Installation
        5. 14.3.5. Connection Pooling With JavaEE
        6. 14.3.6. Dealing With Long-running Tasks
        7. 14.3.7. Thread Pools
        8. 14.3.8. Passing Messages Across Process Boundaries
      4. 14.4. Summary
    3. 15. Introduction to Pylons
      1. 15.1. A Guide for the Impatient
      2. 15.2. A Note about Paste
      3. 15.3. Pylons MVC
      4. 15.4. An Interlude into Java's Memory Model
      5. 15.5. Invoking the Pylons Shell
        1. 15.5.1. request.GET
        2. 15.5.2. request.POST
        3. 15.5.3. request.params
        4. 15.5.4. request.headers
      6. 15.6. Context Variables and Application Globals
      7. 15.7. Routes
      8. 15.8. Controllers and Templates
      9. 15.9. Adding a JSON API
      10. 15.10. Unit Testing, Functional Testing, and Logging
      11. 15.11. Deployment into a Servlet Container
      12. 15.12. Summary
    4. 16. GUI Applications
      1. 16.1. Summary
    5. 17. Deployment Targets
      1. 17.1. Application Servers
        1. 17.1.1. Tomcat
          1. 17.1.1.1. Deploying Web Start
          2. 17.1.1.2. Deploying a WAR or Exploded Directory Application
        2. 17.1.2. Glassfish
          1. 17.1.2.1. Deploying Web Start
          2. 17.1.2.2. WAR File and Exploded Directory Deployment
          3. 17.1.2.3. Glassfish v3 Django Deployment
        3. 17.1.3. Other Java Application Servers
      2. 17.2. Google App Engine
        1. 17.2.1. Starting With an SDK Demo
        2. 17.2.2. Deploying to the Cloud
        3. 17.2.3. Working With a Project
        4. 17.2.4. Object Factories with App Engine
        5. 17.2.5. Using PyServlet Mapping
        6. 17.2.6. Example Jython Servlet Application for App Engine
        7. 17.2.7. Using Eclipse
        8. 17.2.8. Deploy Modjy to GAE
      3. 17.3. Java Store
        1. 17.3.1. Deploying a Single JAR
      4. 17.4. Mobile
      5. 17.5. Summary
  10. IV. Strategy and Technique
    1. 18. Testing and Continuous Integration
      1. 18.1. Python Testing Tools
        1. 18.1.1. UnitTest
        2. 18.1.2. Doctests
        3. 18.1.3. A Complete Example
        4. 18.1.4. Nose
        5. 18.1.5. Integration with Java?
      2. 18.2. Continuous Integration
        1. 18.2.1. Hudson
        2. 18.2.2. Getting Hudson
        3. 18.2.3. Installing the Jython Plug-in
        4. 18.2.4. Creating a Hudson Job for a Jython Project
        5. 18.2.5. Using Nose on Hudson
      3. 18.3. Summary
    2. 19. Concurrency
      1. 19.1. Java or Python APIs?
      2. 19.2. Working With Threads
      3. 19.3. Thread Locals
      4. 19.4. No Global Interpreter Lock
      5. 19.5. Module Import Lock
      6. 19.6. Working With Tasks
      7. 19.7. Thread Safety
        1. 19.7.1. Synchronization
        2. 19.7.2. Deadlocks
        3. 19.7.3. Other Synchronization Objects
        4. 19.7.4. Atomic Operations
        5. 19.7.5. Thread Confinement
      8. 19.8. Python Memory Model
      9. 19.9. Interruption
      10. 19.10. Summary
    3. A. Using Other Tools with Jython
      1. A.1. The Jython Registry
        1. A.1.1. Registry Properties
          1. A.1.1.1. python.cachedir
          2. A.1.1.2. python.verbose
          3. A.1.1.3. python.security.respectJavaAccessibility
          4. A.1.1.4. python.jythonc.compiler
          5. A.1.1.5. python.jythonc.classpath
          6. A.1.1.6. python.jythonc.compileropts
          7. A.1.1.7. python.console
          8. A.1.1.8. python.console.readlinelib
        2. A.1.2. Finding the Registry File
      2. A.2. Setuptools
      3. A.3. Virtualenv
    4. B. Jython Cookbook
      1. B.1. Logging
        1. B.1.1. Using log4j with Jython, Josh Juneau
          1. B.1.1.1. Setting Up Your Environment
          2. B.1.1.2. Using log4j in a Jython Application
      2. B.2. Working with Spreadsheets
        1. B.2.1. Creating and Reading Spreadsheets Using Apache Poi
          1. B.2.1.1. Create Spreadsheet
          2. B.2.1.2. Read an Excel File
      3. B.3. Jython and XML
        1. B.3.1. Writing and Parsing RSS with ROME, Josh Juneau
          1. B.3.1.1. Setting up the CLASSPATH
          2. B.3.1.2. Parsing Feeds
          3. B.3.1.3. Creating Feeds
          4. B.3.1.4. Summary
      4. B.4. Working with CLASSPATH
        1. B.4.1. Using the CLASSPATH, Steve Langer
          1. B.4.1.1. What to Do?
          2. B.4.1.2. Method
          3. B.4.1.3. Summary
      5. B.5. Ant
        1. B.5.1. Writing Ant Tasks with Jython, Ed Takema
          1. B.5.1.1. Writing Custom Ant Tasks
          2. B.5.1.2. Setup Development Environment
          3. B.5.1.3. SimpleTask Jython Class
          4. B.5.1.4. Compiling Jython Code to a Jar
          5. B.5.1.5. Build.XML File to Use the Task
          6. B.5.1.6. A Task Container Task
          7. B.5.1.7. Build.XML File to Use the TaskContainer
          8. B.5.1.8. Things to Look Out For
          9. B.5.1.9. Summary
      6. B.6. Developing Django Web Apps
        1. B.6.1. Using Django in Netbeans, Josh Juneau
    5. C. Built-in Functions
      1. C.1. Constructor Functions
        1. C.1.1. bool([x])
        2. C.1.2. chr(i)
        3. C.1.3. complex([real[, imag]])
        4. C.1.4. dict([arg])
        5. C.1.5. file(filename[, mode[, bufsize]])
        6. C.1.6. float([x])
        7. C.1.7. frozenset([iterable])
        8. C.1.8. int([x[, radix]])
        9. C.1.9. iter(o[, sentinel])
        10. C.1.10. list([iterable])
        11. C.1.11. object()
        12. C.1.12. open(filename[, mode[, bufsize]])
        13. C.1.13. range([start,] stop[, step])
        14. C.1.14. set([iterable])
        15. C.1.15. slice([start,] stop[, step])
        16. C.1.16. str([object])
        17. C.1.17. tuple([iterable])
        18. C.1.18. type(name, bases, dict)
        19. C.1.19. unichr(i)
        20. C.1.20. unicode([object[, encoding [, errors]]])
        21. C.1.21. xrange([start,] stop[, step])
      2. C.2. Math Built-in Functions
        1. C.2.1. abs(x)
        2. C.2.2. cmp(x, y)
        3. C.2.3. divmod(a, b)
        4. C.2.4. pow(x, y[, z])
        5. C.2.5. round(x[, n])
      3. C.3. Functions on Iterables
        1. C.3.1. all(iterable)
        2. C.3.2. any(iterable)
        3. C.3.3. enumerate(sequence[, start=0])
        4. C.3.4. filter(function, iterable)
        5. C.3.5. map(function, iterable, ...)
        6. C.3.6. max(iterable[, key])or max([, arg, ...][, key])
        7. C.3.7. min(iterable[, key]) or min([, arg, ...][, key])
        8. C.3.8. reduce(function, iterable[, initializer])
        9. C.3.9. reversed(seq)
        10. C.3.10. sorted(iterable[, cmp[, key[, reverse]]])
        11. C.3.11. sum(iterable[, start=0])
        12. C.3.12. zip([iterable, ...])
      4. C.4. Conversion Functions
        1. C.4.1. hex(x)
        2. C.4.2. long([x[, radix]])
        3. C.4.3. oct(x)
        4. C.4.4. ord(c)
      5. C.5. Functions for Working with Code
        1. C.5.1. classmethod(function)
        2. C.5.2. compile(source, filename, mode[, flags[, dont_inherit]])
        3. C.5.3. eval(expression[, globals[, locals]])
        4. C.5.4. execfile(filename[, globals[, locals]])
        5. C.5.5. property([fget[, fset[, fdel[, doc]]]])
        6. C.5.6. staticmethod(function)
        7. C.5.7. super(type[, object-or-type])
      6. C.6. Input Functions
        1. C.6.1. input([prompt])
        2. C.6.2. raw_input([prompt])
      7. C.7. Functions for Working with Modules and Objects
        1. C.7.1. callable(object)
        2. C.7.2. delattr(object, name)
        3. C.7.3. dir([object])
        4. C.7.4. getattr(object, name[, default])
        5. C.7.5. globals()
        6. C.7.6. hasattr(object, name)
        7. C.7.7. hash(object)
        8. C.7.8. help([object])
        9. C.7.9. id(object)
        10. C.7.10. isinstance(object, classinfo)
        11. C.7.11. issubclass(class, classinfo)
        12. C.7.12. len(s)
        13. C.7.13. locals()
        14. C.7.14. reload(module)
        15. C.7.15. repr(object)
        16. C.7.16. setattr(object, name, value)
        17. C.7.17. type(object)
        18. C.7.18. vars([object])
        19. C.7.19. __import__(name[, globals[, locals[, fromlist[, level]]]])

Product information

  • Title: The Definitive Guide to Jython: Python for the Java™ Platform
  • Author(s):
  • Release date: March 2010
  • Publisher(s): Apress
  • ISBN: 9781430225270