The Definitive Guide to Jython (eBook)
544 Seiten
Apress (Verlag)
978-1-4302-2528-7 (ISBN)
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.
Josh Juneau has been developing software since the mid-1990s. PL/SQL development and database programming was the focus of his career in the beginning, but as his skills developed, he began to use Java and later shifted to it as a primary base for his application development. Josh has worked with Java in the form of graphical user interface, web, and command-line programming for several years. During his tenure as a Java developer, he has worked with many frameworks such as JSF, EJB, and JBoss Seam. At the same time, Josh has extended his knowledge of the Java Virtual Machine (JVM) by learning and developing applications with other JVM languages such as Jython and Groovy. His interest in learning new languages that run on the JVM led to his interest in Jython. Since 2006, Josh has been the editor and publisher for the Jython Monthly newsletter. In late 2008, he began a podcast dedicated to the Jython programming language.
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.
Josh Juneau has been developing software since the mid-1990s. PL/SQL development and database programming was the focus of his career in the beginning, but as his skills developed, he began to use Java and later shifted to it as a primary base for his application development. Josh has worked with Java in the form of graphical user interface, web, and command-line programming for several years. During his tenure as a Java developer, he has worked with many frameworks such as JSF, EJB, and JBoss Seam. At the same time, Josh has extended his knowledge of the Java Virtual Machine (JVM) by learning and developing applications with other JVM languages such as Jython and Groovy. His interest in learning new languages that run on the JVM led to his interest in Jython. Since 2006, Josh has been the editor and publisher for the Jython Monthly newsletter. In late 2008, he began a podcast dedicated to the Jython programming language.
The Definitive Guide to Jython 1
Copyright Page 2
Contents at a Glance 3
Contents 5
Foreword 19
About the Authors 20
About the Technical Reviewers 22
Acknowledgments 23
Introduction 26
PART 1 Jython Basics: Learning the Language 28
CHAPTER 1 Language and Syntax 30
The Difference between Jython and Python 31
Installing and Configuring Jython 31
Identifiers and Declaring Variables 32
Reserved Words 33
Coding Structure 33
Operators 35
Expressions 35
Functions 36
Classes 37
Statements 38
if-elif-else Statement 39
print Statement 40
try-except-finally 42
raise Statement 43
import Statement 44
Iteration 44
While Loop 46
For Loop 47
Basic Keyboard Input 47
Other Python Statements 48
Documenting Code 49
Python Help 50
Summary 51
CHAPTER 2 Data Types and Referencing 52
Python Data Types 52
Strings and String Methods 54
Lists, Dictionaries, Sets, and Tuples 60
Jython-specific Collections 77
Files 79
Iterators 81
Referencing and Copies 82
Garbage Collection 84
Summary 85
CHAPTER 3 Operators, Expressions, and Program Flow 86
Types of Expressions 86
Mathematical Operations 86
Comparison Operators 90
Bitwise Operators 92
Augmented Assignment 93
Boolean Expressions 95
Conversions 97
Using Expressions to Control Program Flow 99
if-elif-else Statement 99
while Loop 100
continue Statement 101
break Statement 102
for Loop 103
Example Code 104
Summary 106
CHAPTER 4 Defining Functions and Using Built-ins 107
Function Syntax and Basics 107
The def Keyword 108
Naming the Function 108
Function Parameters and Calling Functions 110
Function Body 112
Miscellaneous Information for the Curious Reader 116
Built-in Functions 116
Alternative Ways to Define Functions 116
Lambda Functions 117
Generator Functions 117
Defining Generators 118
Generator Expressions 121
Namespaces, Nested Scopes, and Closures 121
Function Decorators 122
Coroutines 125
Decorators in Coroutines 127
Coroutine Example 128
Summary 128
CHAPTER 5 Input and Output 130
Input from the Keyboard 130
sys.stdin and raw_input 130
Obtaining Variables from Jython Environment 131
File I/0 132
Pickle 135
Output Techniques 136
Summary 137
CHAPTER 6 Object-Oriented Jython 138
Basic Syntax 138
Object Attribute Lookups 142
Inheritance and Overloading 144
Underscore Methods 146
Protocols 148
Default Arguments 152
Runtime Binding of Methods 153
Caching Attribute Access 153
Summary 156
CHAPTER 7 Exception Handling and Debugging 158
Exception Handling Syntax and Differences with Java 158
Catching Exceptions 159
Raising Exceptions 167
Defining Your Own Exceptions 168
Issuing Warnings 168
Assertions and Debugging 173
Context Managers 173
Summary 175
CHAPTER 8 Modules and Packages for Code Reuse 176
Imports for Reuse 176
Import Basics 176
The Import Statement 178
An Example Program 178
Trying Out the Example Code 179
Types of Import Statements 180
From Import Statements 180
Relative Import Statements 181
Aliasing Import Statements 181
Hiding Module Names 182
Module Search Path, Compilation, and Loading 182
Java Import Example 182
Module Search Path and Loading 183
Java Package Scanning 183
How Jython Finds the Jars and Classes to Scan 184
Compilation 185
Python Modules and Packages versus Java Packages 185
sys.path 185
Naming Python Modules and Packages 185
Proper Python Naming 186
Advanced Import Manipulation 186
Import Hooks 186
sys.path_hooks 186
sys.meta_path 187
Summary 187
PART II Using the Language 188
CHAPTER 9 Scripting With Jython 190
Searching for a File 191
Manipulating Files 192
Making a Script a Module 193
Parsing Commandline Options 194
Compiling Java Source 195
Example Script: Builder.py 195
HelloWorld.java 197
Summary 198
CHAPTER 10 Jython and Java Integration 199
Using Java Within Jython Applications 199
Using Jython Within Java Applications 202
Object Factories 203
JSR-223 216
Utilizing PythonInterpreter 217
Summary 219
CHAPTER 11 Using Jython in an IDE 220
Eclipse 220
Installing PyDev 220
Minimal Configuration 221
Hello PyDev!: Creating Projects and Executing Modules 223
Passing Command-line Arguments and Customizing Execution 224
Playing with the Editor 225
A Bit of Structure: Packages, Modules, and Navigation 227
Testing 230
Adding Java Libraries to the Project 233
Debugging 234
Conclusion about Eclipse 236
Netbeans 236
IDE Installation and Configuration 236
Advanced Python Options 238
General Python Usage 239
Standalone Jython Apps 239
Jython and Java Integrated Apps 244
Using a JAR or Java Project in Your Jython App 244
Using Jython in Java 245
The Netbeans Python Debugger 246
Other Netbeans Python Features 251
Summary 252
CHAPTER 12 Databases and Jython: Object Relational Mappingand Using JDBC 253
ZxJDBC—Using Python’s DB API via JDBC 253
Getting Started 254
Connections 255
ZxJDBC.lookup 259
Prepared Statements 265
Resource Management 265
Metadata 266
Data Manipulation Language and Data Definition Language 267
History 271
Object Relational Mapping 271
SqlAlchemy 271
Installation 271
Using SqlAlchemy 272
Hibernate 276
Entity Classes and Hibernate Configuration 276
Jython Implementation Using the Java Entity Classes 278
Summary 283
PART III Developing Applications with Jython 284
CHAPTER 13 Simple Web Applications 286
Servlets 286
Configuring Your Web Application for Jython Servlets 287
Writing a Simple Servlet 287
Using JSP with Jython 289
Applets and Java Web Start 293
Coding a Simple GUI-Based Web Application 293
Distributing via Standalone JAR 297
WSGI and Modjy 297
Running a Modjy Application in Glassfish 298
Summary 301
CHAPTER 14 Web Applications With Django 302
Getting Django 302
A Quick Tour of Django 303
Starting a Project (and an “App”) 304
Models 305
Bonus: The Admin 308
Views and Templates 313
Reusing Templates Without “include”: Template Inheritance 318
Forms 321
Feeds 323
Comments 325
And More... 327
J2EE Deployment and Integration 328
Deploying Your First Application 329
Disabling PostgreSQL Logins 329
A Note About WAR Files 330
Extended Installation 332
Connection Pooling With JavaEE 333
Dealing With Long-running Tasks 336
Thread Pools 336
Passing Messages Across Process Boundaries 339
Summary 346
CHAPTER 15 Introduction to Pylons 347
A Guide for the Impatient 347
A Note about Paste 349
Pylons MVC 349
An Interlude into Java’s Memory Model 350
Invoking the Pylons Shell 351
request.GET 352
request.POST 352
request.params 352
request.headers 353
Context Variables and Application Globals 353
Routes 353
Controllers and Templates 354
Adding a JSON API 360
Unit Testing, Functional Testing, and Logging 361
Deployment into a Servlet Container 366
Summary 366
CHAPTER 16 GUI Applications 367
Summary 377
CHAPTER 17 Deployment Targets 378
Application Servers 378
Tomcat 379
Glassfish 380
Other Java Application Servers 381
Google App Engine 381
Starting With an SDK Demo 382
Deploying to the Cloud 382
Working With a Project 383
Object Factories with App Engine 384
Using PyServlet Mapping 384
Example Jython Servlet Application for App Engine 385
Using Eclipse 388
Deploy Modjy to GAE 389
Java Store 389
Deploying a Single JAR 390
Mobile 394
Summary 394
PART IV Strategy and Technique 396
CHAPTER 18 Testing and Continuous Integration 398
Python Testing Tools 398
UnitTest 398
Doctests 403
A Complete Example 407
Nose 414
Integration with Java? 419
Continuous Integration 420
Hudson 420
Getting Hudson 420
Installing the Jython Plug-in 421
Creating a Hudson Job for a Jython Project 422
Using Nose on Hudson 426
Summary 429
CHAPTER 19 Concurrency 431
Java or Python APIs? 432
Working With Threads 432
Thread Locals 434
No Global Interpreter Lock 435
Module Import Lock 435
Working With Tasks 436
Thread Safety 440
Synchronization 441
Deadlocks 444
Other Synchronization Objects 445
Atomic Operations 449
Thread Confinement 450
Python Memory Model 451
Interruption 451
Summary 454
APPENDIX A Using Other Tools with Jython 455
The Jython Registry 455
Registry Properties 455
Finding the Registry File 456
Setuptools 456
Virtualenv 460
APPENDIX B Jython Cookbook 462
Logging 462
Using log4j with Jython, Josh Juneau 462
Working with Spreadsheets 464
Creating and Reading Spreadsheets Using Apache Poi 464
Jython and XML 467
Writing and Parsing RSS with ROME, Josh Juneau 467
Working with CLASSPATH 471
Using the CLASSPATH, Steve Langer 471
Ant 473
Writing Ant Tasks with Jython, Ed Takema 473
Developing Django Web Apps 478
Using Django in Netbeans, Josh Juneau 478
APPENDIX C Built-in Functions 480
Constructor Functions 480
bool([x]) 480
chr(i) 480
complex([real[, imag]]) 481
dict([arg]) 481
file(filename[, mode[, bufsize]]) 481
float([x]) 481
frozenset([iterable]) 481
int([x[, radix]]) 481
iter(o[, sentinel]) 482
list([iterable]) 482
object() 482
open(filename[, mode[, bufsize]]) 482
range([start,] stop[, step]) 483
set([iterable]) 483
slice([start,] stop[, step]) 483
str([object]) 484
tuple([iterable]) 484
type(name, bases, dict) 484
unichr(i) 484
unicode([object[, encoding [, errors]]]) 484
xrange([start,] stop[, step]) 485
Math Built-in Functions 485
abs(x) 485
cmp(x, y) 485
divmod(a, b) 485
pow(x, y[, z]) 485
round(x[, n]) 486
Functions on Iterables 486
all(iterable) 486
any(iterable) 486
enumerate(sequence[, start=0]) 486
filter(function, iterable) 486
map(function, iterable, ...) 487
max(iterable[, key])or max([, arg, ...][, key]) 487
min(iterable[, key]) or min([, arg, ...][, key]) 487
reduce(function, iterable[, initializer]) 487
reversed(seq) 487
sorted(iterable[, cmp[, key[, reverse]]]) 487
sum(iterable[, start=0]) 488
zip([iterable, ...]) 488
Conversion Functions 489
hex(x) 489
long([x[, radix]]) 489
oct(x) 489
ord(c) 489
Functions for Working with Code 490
classmethod(function) 490
compile(source, filename, mode[, flags[, dont_inherit]]) 490
eval(expression[, globals[, locals]]) 491
execfile(filename[, globals[, locals]]) 491
property([fget[, fset[, fdel[, doc]]]]) 492
staticmethod(function) 493
super(type[, object-or-type]) 493
Input Functions 494
input([prompt]) 494
raw_input([prompt]) 495
Functions for Working with Modules and Objects 495
callable(object) 495
delattr(object, name) 495
dir([object]) 495
getattr(object, name[, default]) 496
globals() 496
hasattr(object, name) 496
hash(object) 497
help([object]) 497
id(object) 497
isinstance(object, classinfo) 497
issubclass(class, classinfo) 498
len(s) 498
locals() 498
reload(module) 498
repr(object) 499
setattr(object, name, value) 499
type(object) 499
vars([object]) 499
__import__(name[, globals[, locals[, fromlist[, level]]]]) 500
Index 502
Erscheint lt. Verlag | 28.12.2010 |
---|---|
Zusatzinfo | 544 p. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Mathematik / Informatik ► Informatik ► Theorie / Studium | |
Mathematik / Informatik ► Informatik ► Web / Internet | |
Schlagworte | code reuse • Database • Debugging • Deployment • Django • Java • Jython • Python • Web Services |
ISBN-10 | 1-4302-2528-9 / 1430225289 |
ISBN-13 | 978-1-4302-2528-7 / 9781430225287 |
Haben Sie eine Frage zum Produkt? |
Größe: 5,1 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich