The Definitive Guide to Jython doc

545 5.7K 0
The Definitive Guide to Jython doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Juneau Baker Ng Soto Wierzbicki The Definitive Guide to Jython Companion eBook Available this print for content only—size & color not accurate CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® The Definitive Guide to Jython: Python for the Java ™ Platform Dear Reader, With Jython you enjoy the increased productivity and clarity of Python, and the ability to integrate with existing Java™ libraries. You get the best of both worlds: the elegance of Python, and the enterprise-readiness and maturity of the JVM. The Definitive Guide to Jython explores the Python programming lan- guage from the ground up, pointing out along the way details that are specific to Jython. If you are unfamiliar with the Python language, then you can get started here, but you will also learn about the differences between Jython and standard CPython as you go. Once you have the basics of the language, you’ll want to put it into use. This book covers a variety of topics which will help you harness the power of Jython. You will learn how to develop dynamic applications and scripts, and access data- bases from Jython to build powerful database applications. Fundamental to get- ting the most out of Jython is Java integration, and this book encourages you to use Java within Jython code as well as Jython within Java code. Jython has matured a lot between the last couple of versions and it is now compliant with modern versions of the Python language. This means that you can now take advantage of Python frameworks such as Django and Pylons, which are both covered in this book. We will also provide you with a foundation to begin using Java Servlet and Java Swing technologies with Jython. We hope you enjoy this book and that it helps Jython to become as useful to you as it is to us! Josh Juneau, Jim Baker, Victor Ng, Leo Soto, and Frank Wierzbicki US $54.99 Shelve in: Java, Python User level: Beginner – Intermediate www.apress.com Available online at www.jythonbook.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2527-0 9 781430 225270 5 54 9 9 RELATED TITLES spine = 1.03125" 544 page count Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki Foreword by Ted Leung Enjoy the power and flexibility of Python on the JVM Covers Jython 2.5 The Definitive Guide to Jython Python for the Java ™ Platform THE EXPERT’S VOICE ® IN SOFTWARE DEVELOPMENT www.it-ebooks.info www.it-ebooks.info The Definitive Guide to Jython Python for the Java™ Platform ■ ■ ■ Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki www.it-ebooks.info ■ CONTENTS AT A GLANCE ii The Definitive Guide to Jython: Python for the Java™ Platform Copyright © 2010 by Josh Juneau, Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2527-0 ISBN-13 (electronic): 978-1-4302-2528-7 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. President and Publisher: Paul Manning Lead Editors: Steve Anglin, Duncan Parkes Technical Reviewers: Mark Ramm, Tobias Ivarsson Editorial Board: Clay Andres, Steve Anglin, 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 Coordinating Editor: Mary Tobin Copy Editor: Tracy Brown Collins Indexer: BIM Indexers and e-Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please e-mail info@apress.com, or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. This book is available online under the Creative Commons Attribution-Share Alike license (http://creativecommons.org/licenses/by-sa/3.0/). You can read the book at http://jythonbook.com. www.it-ebooks.info ■ iii Contents at a Glance Contents at a Glance iii Contents v Foreword xix About the Authors xx About the Technical Reviewers xxii Acknowledgments xxiii Introduction xxvi Part I: Jython Basics: Learning the Language 1 ■ Chapter 1: Language and Syntax 3 ■ Chapter 2: Data Types and Referencing 25 ■ Chapter 3: Operators, Expressions, and Program Flow 59 ■ Chapter 4: Defining Functions and Using Built-ins 81 ■ Chapter 5: Input and Output 105 ■ Chapter 6: Object-Oriented Jython 113 ■ Chapter 7: Exception Handling and Debugging 133 ■ Chapter 8: Modules and Packages for Code Reuse 151 Part II: Using the Language 163 ■ Chapter 9: Scripting With Jython 165 ■ Chapter 10: Jython and Java Integration 175 ■ Chapter 11: Using Jython in an IDE 197 ■ Chapter 12: Databases and Jython: Object Relational Mapping and Using JDBC 231 Part III: Developing Applications with Jython 263 ■ Chapter 13: Simple Web Applications 265 ■ Chapter 14: Web Applications With Django 281 ■ Chapter 15: Introduction to Pylons 327 ■ Chapter 16: GUI Applications 347 www.it-ebooks.info ■ CONTENTS AT A GLANCE iv ■ Chapter 17: Deployment Targets 359 Part IV: Strategy and Technique 377 ■ Chapter 18: Testing and Continuous Integration 379 ■ Chapter 19: Concurrency 413 ■ Appendix A: Using Other Tools with Jython 437 ■ Appendix B: Jython Cookbook 445 ■ Appendix C: Built-in Functions 463 Index 485 www.it-ebooks.info ■ v Contents Contents at a Glance iii Contents v Foreword xix About the Authors xx About the Technical Reviewers xxii Acknowledgments xxiii Introduction xxvi Part I: Jython Basics: Learning the Language 1 ■ Chapter 1: Language and Syntax 3 The Difference between Jython and Python 4 Installing and Configuring Jython 4! Identifiers and Declaring Variables 5! Reserved Words 6! Coding Structure 6! Operators 8! Expressions 8! Functions 9! Classes 10! Statements 11! if-elif-else Statement 12! print Statement 13! try-except-finally 15! raise Statement 16! import Statement 17! Iteration 17! While Loop 19! For Loop 20! Basic Keyboard Input 20! www.it-ebooks.info ■ CONTENTS vi Other Python Statements 21! Documenting Code 22! Python Help 23! Summary 24! ■ Chapter 2: Data Types and Referencing 25 Python Data Types 25! Strings and String Methods 27! String Formatting 31! Lists, Dictionaries, Sets, and Tuples 33! Lists 33! List Comprehensions 40! Tuples 41! Dictionaries 42! Sets 45! Ranges 48! Range Format 49! Jython-specific Collections 50! Files 52! Iterators 54! Referencing and Copies 55! Garbage Collection 57! Summary 58! ■ Chapter 3: Operators, Expressions, and Program Flow 59 Types of Expressions 59! Mathematical Operations 59! Comparison Operators 63! Bitwise Operators 65! Augmented Assignment 66! Boolean Expressions 68! Conversions 70! Using Expressions to Control Program Flow 72! if-elif-else Statement 72! while Loop 73! continue Statement 74! break Statement 75! www.it-ebooks.info ■ CONTENTS vii for Loop 76! Example Code 77! Summary 79! ■ Chapter 4: Defining Functions and Using Built-ins 81 Function Syntax and Basics 81! The def Keyword 82! Naming the Function 82! Function Parameters and Calling Functions 84! Recursive Function Calls 86! Function Body 86! Documenting Functions 86! Returning Values 87! Introducing Variables 88! Other Statements 89! Empty Functions 89! Miscellaneous Information for the Curious Reader 90! Built-in Functions 90! Alternative Ways to Define Functions 90! Lambda Functions 91! Generator Functions 91! Defining Generators 92! Generator Expressions 95! Namespaces, Nested Scopes, and Closures 95! Function Decorators 96! Coroutines 99! Decorators in Coroutines 101! Coroutine Example 102! Summary 102! ■ Chapter 5: Input and Output 105 Input from the Keyboard 105! sys.stdin and raw_input 105! Obtaining Variables from Jython Environment 106! File I/O 107! Pickle 110! Output Techniques 111! www.it-ebooks.info ■ CONTENTS viii Summary 112! ■ Chapter 6: Object-Oriented Jython 113 Basic Syntax 113! Object Attribute Lookups 117! Inheritance and Overloading 119! Underscore Methods 121! Protocols 123! Default Arguments 127! Runtime Binding of Methods 128! Caching Attribute Access 128! Summary 131! ■ Chapter 7: Exception Handling and Debugging 133 Exception Handling Syntax and Differences with Java 133! Catching Exceptions 134! Raising Exceptions 142! Defining Your Own Exceptions 143! Issuing Warnings 143! Assertions and Debugging 148! Context Managers 148! Summary 150! ■ Chapter 8: Modules and Packages for Code Reuse 151 Imports for Reuse 151! Import Basics 151! breakfast.py 151! The Import Statement 153! An Example Program 153! greetings.py 154! greet/__init__.py 154! greet/hello.py 154! greet/people.py 154! Trying Out the Example Code 154! Types of Import Statements 155! From Import Statements 155! Relative Import Statements 156! www.it-ebooks.info [...]... will be in the code font The examples are available on the Apress website at http://www.apress.com as well as at the Open Source site http://jythonbook.com This book will continue to evolve and we will continually update both the online version and the printed copy We’d like to thank members of the Jython community for contributing to the book, especially Andrea Aime and others who wrote to the mailing... order to run Jython, you will need to invoke the jython. bat executable file on Windows or the jython. sh file on *NIX machines and Mac OS X That being said, you’ll have to traverse into the directory 4 www.it-ebooks.info CHAPTER 1 ■ LANGUAGE AND SYNTAX that you’ve installed Jython where you will find the file It would be best to place this directory within your PATH environment variable on either Windows,... and other times inconvenient to my family My daughter, Lily, who is five at the time of writing, has also needed to show patience when her dad was working on Jython and on this book I want to thank my parents, who brought a Commodore 64 into the house when I was still impressionable enough to get sucked into a life of programming I also want to thank all of the contributors and users of Jython They... entity and can be read separately from the others This provides the ability to jump around the book if you’d like, or read it from start to finish Some chapters contain references to other parts of the book and this book builds upon itself to guide a novice or a seasoned developer into becoming an expert Jython programmer Since this is a multi-author book, each of the chapters was written by an individual... find that the chapters each contain a unique touch, but they are orchestrated in such a way that they work very well together Part I of this book will take a look at the Python language and provide a tutorial to guide you through learning the language from the ground up It contains Python language basics, as well as Jython- specific portions for those who already know Python Until now, using Jython in... very well documented Part II addresses this topic, teaches you how to use Python and Java techniques for working with databases, and even shows how to develop Jython using both the Eclipse and Netbeans IDEs The second part of the book is all about making use of Jython Part III delves into developing full applications with Jython, deploying them in different environments, and also testing them to ensure... collaborated to find the other great authors that helped us write this book In the end, I believe that the team of authors that was chosen provides the perfect blend of knowledge and skills that went into authoring this book I thank each of the authors for devoting their time and effort towards this book; I think that it will be a great asset to the community! Thanks for everything, I look forward to writing the. .. on Jython, the language implementation that takes the elegance, power, and ease of Python and runs it on the JVM The Java platform is an asset to the Jython language much like the C libraries are for Python Jython is able to run just about everywhere, which gives lots of flexibility when deciding how to implement an application Not only does the Java platform allow for flexibility with regards to application... use by Jython Add in the maturity of the Java platform and it becomes easy to see why Jython is such an attractive programming language The goal, if you will, of any programming language is to grant its developers the same experience that Jython does Simply put, learning Jython will be an asset to any developer As I’ve mentioned, the Jython language implementation takes Python and runs it on the JVM,... experienced the power of programming on the Java platform, it will be difficult to move away from it Learning Jython not only allows you to run on the JVM, but it also allows you to learn a new way to harness the power of the platform The language increases productivity as it has an easily understood syntax that reads almost as if it were pseudocode It also adds dynamic abilities that are not available in the . Python, and the enterprise-readiness and maturity of the JVM. The Definitive Guide to Jython explores the Python programming lan- guage from the ground. BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® The Definitive Guide to Jython: Python for the Java ™ Platform Dear Reader, With Jython you

Ngày đăng: 06/03/2014, 17:20

Từ khóa liên quan

Mục lục

  • Prelim

  • Contents at a Glance

  • Contents

  • Foreword

  • About the Authors

  • About the Technical Reviewers

  • Acknowledgments

  • Introduction

  • Jython Basics: Learning the Language

    • Language and Syntax

      • The Difference between Jython and Python

      • Installing and Configuring Jython

      • Identifiers and Declaring Variables

      • Reserved Words

      • Coding Structure

      • Operators

      • Expressions

      • Functions

      • Classes

      • Statements

        • if-elif-else Statement

        • print Statement

        • try-except-finally

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan