Jython Programming
Jython Programming
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. About this tutorial....................................................... 2. Object-oriented programming in Jython ............................ 3. Advanced object-oriented programming ........................... 4. Debugging Jython ...................................................... 5. Java support in Jython ................................................ 6. Java thread support in Jython........................................ 7. Interfacing with Java services ........................................ 8. Jython string processing .............................................. 9. Processing regular expressions ..................................... 10. File I/O in Jython ...................................................... 11. A simple Swing GUI .................................................. 12. Wrap-up and resourcesFeedback ................................. 13. Appendices ............................................................
2 5 13 25 29 34 40 46 54 58 67 73 76
Page 1 of 100
ibm.com/developerWorks
To benefit from the discussion, you should be familiar with at least one procedural programming language and the basic concepts of computer programming, including command-line processing. To fully utilize Jython's features you should also be familiar
Page 2 of 100 Introduction to Jython, Part 2: Programming essentials
with the basic concepts of object-oriented programming. To fully understand the GUI application example at the end of the tutorial you should have prior experience with Swing GUI programming, although you will be able to glean a lot from the preceding discussion and examples. It will also be helpful to have a working knowledge of the Java platform, because Jython runs on a JVM; although this is not a requirement of the tutorial. Note that this tutorial is oriented towards Windows systems. All command examples will employ Windows syntax. In most cases similar commands perform the same functions on UNIX systems, although these commands will not be demonstrated.
ibm.com/developerWorks
Objects in Jython
Jython is an object-oriented language that completely supports object-oriented programming. Objects defined by Jython have the following features: Identity: Each object must be distinct and this must be testable. Jython supports the is and is not tests for this purpose. State: Each object must be able to store state. Jython provides attributes (a.k.a. fields or instance variables) for this purpose. Behavior: Each object must be able to manipulate its state. Jython provides methods for this purpose. Note that the id(object) built-in function returns a unique integer identity value. So, the expression x is y is equivalent to id(x) == id(y).
Page 5 of 100
ibm.com/developerWorks
Inheritance with polymorphism: Jython supports single- and multiple-inheritance . All Jython instance methods are polymorphic (or virtual) and may be overridden by subclasses. Encapsulation with data hiding: Jython allows (but does not require) attributes to be hidden, thus permitting access outside the class itself only through methods of the class. Classes implement functions (called methods) to modify the data.
Defining a class
Defining a class is a lot like defining a module in that both variables and functions can be defined. Unlike the Java language, Jython allows the definition of any number of public classes per source file (or module). Thus, a module in Jython is much like a package in the Java language. We use the class statement to define classes in Jython. The class statement has the following form:
statement
When you define a class, you have the option to provide zero or more assignment statements. These create class attributes that are shared by all instances of the class. You can also provide zero or more function definitions. These create methods. The superclasses list is optional. We'll discuss superclasses a little later in the tutorial. The class name should be unique in the same scope (module, function, or class). The class name is really a variable bound to the class body (similar to any other assignment). In fact, you can define multiple variables to reference the same class.
Page 6 of 100
ibm.com/developerWorks
create an instance of a class you call the class as if it were a function. There is no need to use a new operator like in C++ or the Java language. For example, with the class
x = MyClass()
# class attributes
def method2(self, p1, p2): print MyClass.attr2 # reference the class attribute def method3(self, text): self.text = text # instance attribute print text, self.text # print my argument and my attribute
Page 7 of 100
ibm.com/developerWorks
method4 = method3
Note that inside a class, you should qualify all references to class attributes with the class name (for example, MyClass.attr1) and all references to instance attributes with the self variable (for example, self.text). Outside the class, you should qualify all references to class attributes with the class name (for example, MyClass.attr1) or an instance (for example, x.attr1) and all references to instance attributes with an instance (for example, x.text, where x is an instance of the class).
Hidden variables
To achieve data hiding, it is often desirable to create "private" variables, which can be accessed only by the class itself. Jython provides a naming convention that makes accessing attributes and methods outside the class difficult. If you declare names of the form: __xxx or __xxx_yyy (that's two leading underscores), the Jython parser will automatically mangle (that is, add the class name to) the declared name, in effect creating hidden variables. For example:
class MyClass: __attr = 10 def method1(self): pass def method2(self, p1, p2): pass
Note that unlike C++ and the Java language, all references to instance variables must be qualified with self; there is no implied use of this.
Page 8 of 100
ibm.com/developerWorks
class Class1: def __init__ (self): self.data = [] class Class2: def __init__ (self, v1, v2): self.v1 = v1 self.v2 = v2
# no arguments # set implicit data # 2 required arguments # set data from parameters
class Class3: def __init__ (self, values=None): # 1 optional argument if values is None: values = [] self.values = values # set data from parameter
class Class: def __init__ (self, db): self.connection = db.getConnection() self.connection.open() def __del__ (self): self.close()
def close(self): # cleanup if not self.connection is None and self.connection.isOpen(): self.connection.close() # release connection self.connection = None
Page 9 of 100
ibm.com/developerWorks
Inheritance
The ability to inherit from classes is a fundamental to object-oriented programming. Jython supports both single and multiple-inheritance. Single inheritance means there can be only one superclass; multiple inheritance means there can be more than one superclass. Inheritance is implemented by subclassing other classes. These classes can be either other Jython classes or Java classes. Any number of pure-Jython classes or Java interfaces can be superclasses but only one Java class can be (directly or indirectly) inherited from. You are not required to supply a superclass. Any attribute or method in a superclass is also in any subclass and may be used by the class itself or any client (assuming it is publicly visible). Any instance of a subclass can be used wherever an instance of the superclass can be used -- this is an example of polymorphism. These features enable reuse, rapid development, and ease of extension. Below are some examples of inheritance:
# no inheritance
# single inheritance
class Class4(Class3,Class2): pass # multiple inheritance from java import awt from java import io # inherit a Java class and interface and a Jython class class MyPanel(awt.Panel, io.Serializable, Class2): :
Page 10 of 100
ibm.com/developerWorks
class Class1(SuperClass): def __init__ (self): SuperClass.__init__(self) self.data = [] class Class2(SuperClass): def __init__ (self, v1, v2): SuperClass.__init__(self, v1) self.v2 = v2
# no arguments # init my super-class # set implicit data # 2 required arguments # init my super-class with v1
class Class2(Super1, Super2): def __init__ (self, v1, v2, v3): # 3 required arguments # note you may do work before calling the super __init__ methods self.v3 = v3 # set data from parameter Super1.__init__(self, v1) # init each super-class Super2.__init__(self, v2)
class Class1: def method1 (self): : class Class2(Class1): def method1 (self): : Class1.method1(self) : def method2 (self): : class Class3(Class2): def method1 (self): : Class2.method1(self) # override method1 # call my super-class method # override method1 # call my super-class method
Page 11 of 100
ibm.com/developerWorks
Note that the secondary method definitions (in Class2 and Class3) override the superclass definitions. There is no requirement that the subclass method call its superclass method; however, if it doesn't, then it must completely replace the function of the superclass method.
Calling methods
There are two syntaxes for calling methods (assuming you have an instance of MyClass referenced by variable mci): mci.someMethod(...) MyClass.someMethod(mci, ...) The first form typically is used in class client coding while the second one is used more often in subclasses to call superclass methods.
Page 12 of 100
ibm.com/developerWorks
Special attributes
Jython classes provide support for several special attributes. The most significant are shown below: Name __dict__ __class__ __bases__ Role The object's writeable attributes The class of an object Comment(s) Can be used to introspect the attributes of an object Access the class of the object (similar to x.getClass() in Java coding)
A tuple of the Can be used to introspect the immediate superclasses superclasses of the object of the object
x = SomeClass() print isinstance(x, SomeClass) # print isinstance(x, SomeOtherClass) # : # change the class (that is, the type) x.__class__ = SomeOtherClass print isinstance(x, SomeClass) #
Page 13 of 100
ibm.com/developerWorks
After this change, the x instance will support the methods of SomeOtherClass, not SomeClass as it did previously. Take care when changing the class of an object that the instance has the right attributes for the new class.
__any__ = ['getMembers', 'printObject'] def addMember (list, item): if not item in list: list.append(item) def getMembers (obj, memtype="attrs"): """ Get all the members (of memtype) of the object. """ members = [] for name, value in vars(obj).items(): try: item = obj.__name__, name, value except: item = "<instance>", name, value if memtype.lower().startswith("attr"): if not callable(value): addMember(members, item) elif memtype.lower().startswith("meth"): if callable(value): addMember(members, item) elif memtype.lower() == "all": addMember(members, item) try: for base in obj.__bases__: members.extend(getMembers(base, memtype)) except: pass return members import sys
Page 14 of 100
ibm.com/developerWorks
def printObject (obj, stream=sys.stdout): """ Print all the members of the object. """ members = getMembers(obj, "attrs") members.sort() print >>stream, "Attributes:" for objname, memname, value in members: print >>stream, " %s.%s" % (objname, memname) members = getMembers(obj, "methods") members.sort() print >>stream, "Methods:" for objname, memname, value in members: print >>stream, " %s.%s" % (objname, memname)
if __name__ == "__main__": from UserList import UserList class MyClass(UserList): def __init__ (self, x, y): UserList.__init__(self) self.__x = x self.__y = y def method1 (self): return self.x + self.y def method2 (self, x, y): return self.x + self.y + x + y print "For class:", `MyClass` printObject(MyClass) print aMyClass = MyClass(1, 2) aMyClass.extend([1,2,3,4]) print "For instance:", `aMyClass` printObject(aMyClass)
Page 15 of 100
ibm.com/developerWorks
running the main code from the above module. Notice that the private fields and methods (see Hidden variables on page 8 ) have mangled names.
For class: <class __main__.MyClass at 28921555> Attributes: Methods: MyClass.__doc__ MyClass.__init__ MyClass.__module__ MyClass.method1 UserList.__doc__ MyClass.method2 UserList.__module__ UserList._UserList__cast UserList.__add__ UserList.__cmp__ UserList.__contains__ UserList.__delitem__ UserList.__delslice__ UserList.__eq__ UserList.__ge__ UserList.__getitem__ UserList.__getslice__ UserList.__gt__ UserList.__iadd__ UserList.__imul__ UserList.__init__ UserList.__le__ For instance: [1, 2, 3, 4] Attributes: <instance>._MyClass__x <instance>._MyClass__y <instance>.data Methods:
UserList.__len__ UserList.__lt__ UserList.__mul__ UserList.__ne__ UserList.__radd__ UserList.__repr__ UserList.__rmul__ UserList.__setitem__ UserList.__setslice__ UserList.append UserList.count UserList.extend UserList.index UserList.insert UserList.pop UserList.remove UserList.reverse UserList.sort
Note that methods and class attributes reside with classes and instance attributes reside with instances. Yet all the class's methods can be applied to each instance.
Introspection
You will often need to determine, at runtime, the characteristics of an object. We call this introspecting the object. The Java platform offers introspection services via the java.lang.Class class and classes in the java.lang.reflect package. While powerful, these APIs are somewhat difficult to use. As you probably already suspected, Jython offers a simpler approach to introspection. In Jython, we can use the dir and vars functions to examine the bindings for any object, such as modules, functions, classes, sequences, maps, and more. To better understand how this works, consider the following example. The output has been inserted (and reformatted) after the print statements prefixed with "..." for easier reading. The dir function returns only the binding names, while the vars function returns the names and values; thus, when the same names are returned by both
Page 16 of 100
ibm.com/developerWorks
#-- empty start -print "vars:", vars() ...vars: {'__doc__': None, '__name__': '__main__'} x y z l d = = = = = 1 2 3 [x, y, z] {x:"xxxx", y:"yyyy", z:"zzzz"}
#-- locals variables -print x, y, z, l, d ...1 2 3 [1, 2, 3] {3: 'zzzz', 2: 'yyyy', 1: 'xxxx'} #-- plus locals variables -print "vars:", vars() ...vars: {'__name__': '__main__', 'x': 1, \ ... 'd': {3: 'zzzz', 2: 'yyyy', 1: 'xxxx'}, '__doc__': None, \ ... 'y': 2, 'l': [1, 2, 3], 'z': 3} import sys #-- plus import -print "vars:", vars() ...vars: {'__name__': '__main__', 'z': 3, 'l': [1, 2, 3], \ ... '__doc__': None, 'y': 2, 'x': 1, 'sys': sys module, \ ... 'd': {3: 'zzzz', 2: 'yyyy', 1: 'xxxx'}} #-- sys import -print "vars sys:", vars(sys) ...vars sys: {'classLoader': \ ... <beanProperty classLoader type: java.lang.ClassLoader at 31845755>, ... ... many values removed ..., ... 'warnoptions': <reflected field public static \ ... org.python.core.PyList \ ... org.python.core.PySystemState.warnoptions at 1024901>} del x, y, z #-- post delete -print "vars:", vars() ...vars: {'__name__': '__main__', 'l': [1, 2, 3], '__doc__': None, \ ... 'sys': sys module, 'd': {3: 'zzzz', 2: 'yyyy', 1: 'xxxx'}} def func (x, y): return x, y class MyClass (): def __init__ (self, x, y): self.__x = x self.__y = y def method1 (self): return self.x + self.y
Page 17 of 100
ibm.com/developerWorks
def method2 (self, x, y): return self.x + self.y + x + y #-- plus function and class -print "vars:", vars() ....vars: {'func': <function func at 21569784>, '__name__': '__main__', \ ... 'l': [1, 2, 3], '__doc__': None, \ .... 'MyClass': <class __main__.MyClass at 1279942>, \ ... 'sys': sys module, 'd': {3: 'zzzz', 2: 'yyyy', 1: 'xxxx'}} #-- function -print "dir: ", dir(func) # **** dir and vars different here **** print "vars:", vars(func) ...dir: ['__dict__', '__doc__', '__name__', 'func_closure', \ ... 'func_code', 'func_defaults', 'func_doc', 'func_globals', 'func_name'] ...vars: None #-- class -print "vars:", vars(MyClass) ...vars: {'__doc__': None, '__init__': <function __init__ at 17404503>, \ ... 'method2': <function method2 at 23511968>, '__module__': '__main__', \ ... 'method1': <function method1 at 28670096>} myclass = MyClass(1, 2) #-- instance -print "myclass:", myclass print "vars:", vars(myclass) ...myclass: <__main__.MyClass instance at 19014134> ...vars: {'_MyClass__y': 2, '_MyClass__x': 1}
Note that dir(x) is generally equivalent to x.__dict__.keys() and vars(x) is generally equivalent to x.__dict__.
Comment(s) Tests to see if the named attribute exists Gets the named attribute if it exists; else default is returned (or an exception is raised if no default is provided) Sets the named attribute's value
Introduction to Jython, Part 2: Programming essentials
ibm.com/developerWorks
See Appendix K: Built-in functions on page 95 to learn more about these functions.
Abstract classes
Abstract classes are classes in which some or all of the methods are missing or have incomplete definitions. A subclass must be created to provide or complete these method definitions. Concrete classes are not abstract (that is, all the methods are complete). So far we have been working only with concrete classes. Abstract classes are created to facilitate reuse. They provide a partial implementation of a design that you can complete or extend by subclassing them. To get a better understanding of how this works, we will create a simple abstract command framework that supports command do, undo, and redo actions. Commands are defined in (sub)classes and can be added easily by creating new do_... and undo_... methods. We access these methods via introspection, as discussed in the previous panels.
class CommandProcessor: # an abstract class """ Process Commands. """ def __init__ (self): self.__history = [] self.__redo = [] def execute (self, cmdName, *args): """ Do some command """ self.__history.append( (cmdName, args) ) processor = getattr(self, "do_%s" % cmdName, None) if processor: return processor(*args) else: raise NameError, "cannot find do_%s" % cmdName def undo (self, count=1): """ Undo some (or all) commands in LIFO order """ self.__redo = []
Page 19 of 100
ibm.com/developerWorks
while count > 0 and len(self.__history) > 0: cmdName, args = self.__history.pop() count -= 1 processor = getattr(self, "undo_%s" % cmdName, None) if processor: self.__redo.append( (cmdName, args) ) processor(*args) else: raise NameError, "cannot find undo_%s" % cmdName def redo (self, count=1): """ Redo some (or all) undone commands """ while count > 0 and len(self.__redo) > 0: cmdName, args = self.__redo.pop() count -= 1 processor = getattr(self, "do_%s" % cmdName, None) if processor: processor(*args) else: raise NameError, "cannot find do_%s" % cmdName
Note:This example is based on code from Jython Essentials by Samuele Pedroni and Noel Rappin (see Resources on page 73 for more information).
class MyProcessor (CommandProcessor): # a concrete subclass def __init__ (self): CommandProcessor.__init__(self) def do_Cmd1 (self, args): print "Do Command 1:", args def do_Cmd2 (self, args): print "Do Command 2:", args def do_Cmd3 (self, args): print "Do Command 3:", args def undo_Cmd1 (self, args): print "Undo Command 1:", args def undo_Cmd2 (self, args): print "Undo Command 2:", args def undo_Cmd3 (self, args): print "Undo Command 3:", args mp = MyProcessor()
Page 20 of 100
ibm.com/developerWorks
; ; ; ; ;
The framework with the given test case produces the following output:
execute: Do Command 1: None execute: Do Command 2: (1, 2, 3) execute: Do Command 3: Hello undo: Undo Command 3: Hello Undo Command 2: (1, 2, 3) redo: Do Command 2: (1, 2, 3) Do Command 3: Hello execute: Traceback (innermost last): File "cmdproc.py", line 63, in ? File "cmdproc.py", line 15, in execute NameError: cannot find do_BadCmd
Operator overloading
Like C++, but unlike the Java language, Jython allows many of the standard language operators to be overloaded by classes. This means classes can define a specific meaning for the language operators. Jython also allows classes to emulate built-in types like numbers, sequences, and maps. To learn more about emulation see Appendix B: Common overloaded operators and methods on page 76 . In the example that follows, we'll use the standard Jython UserList class definition to show an example of operator overloading in practice. UserList is a class that wraps a list and behaves as a list does. Most of its function is delegated (passed on to) its contained list, called data. In a more realistic example, these overloaded functions would be implemented to access some other store, such as a disk file or a database.
class UserList: def __init__(self, initlist=None): self.data = [] if initlist is not None: if type(initlist) == type(self.data): self.data[:] = initlist elif isinstance(initlist, UserList):
Page 21 of 100
ibm.com/developerWorks
self.data[:] = initlist.data[:] else: self.data = list(initlist) def __cast(self, other): if isinstance(other, UserList): return other.data else: return other # `self`, repr(self) def __repr__(self): return repr(self.data) # self < other def __lt__(self, other): return self.data < self.__cast(other)
# self <= other def __le__(self, other): return self.data <= self.__cast(other) # self == other def __eq__(self, other): return self.data == self.__cast(other) # self != other, self <> other def __ne__(self, other): return self.data != self.__cast(other) # self > other def __gt__(self, other): return self.data > self.__cast(other)
# self >= other def __ge__(self, other): return self.data >= self.__cast(other) # cmp(self, other) def __cmp__(self, other): raise RuntimeError, "UserList.__cmp__() is obsolete" # item in self def __contains__(self, item): return item in self.data # len(self) def __len__(self): return len(self.data) # self[i] def __getitem__(self, i): return self.data[i] # self[i] = item def __setitem__(self, i, item): self.data[i] = item # del self[i] def __delitem__(self, i): del self.data[i] # self[i:j] def __getslice__(self, i, j): i = max(i, 0); j = max(j, 0) return self.__class__(self.data[i:j]) # self[i:j] = other def __setslice__(self, i, j, other): i = max(i, 0); j = max(j, 0) if isinstance(other, UserList): self.data[i:j] = other.data
Page 22 of 100
ibm.com/developerWorks
elif isinstance(other, type(self.data)): self.data[i:j] = other else: self.data[i:j] = list(other) # del self[i:j] def __delslice__(self, i, j): i = max(i, 0); j = max(j, 0) del self.data[i:j] # self + other (join) def __add__(self, other): if isinstance(other, UserList): return self.__class__(self.data + other.data) elif isinstance(other, type(self.data)): return self.__class__(self.data + other) else: return self.__class__(self.data + list(other)) # other + self (join) def __radd__(self, other): if isinstance(other, UserList): return self.__class__(other.data + self.data) elif isinstance(other, type(self.data)): return self.__class__(other + self.data) else: return self.__class__(list(other) + self.data) # self += other (join) def __iadd__(self, other): if isinstance(other, UserList): self.data += other.data elif isinstance(other, type(self.data)): self.data += other else: self.data += list(other) return self # self * other (repeat) def __mul__(self, n): return self.__class__(self.data*n) __rmul__ = __mul__ # self *= other (repeat) def __imul__(self, n): self.data *= n return self # implement "List" functions below: def append(self, item): self.data.append(item) def insert(self, i, item): self.data.insert(i, item) def pop(self, i=-1): return self.data.pop(i) def remove(self, item): self.data.remove(item)
Page 23 of 100
ibm.com/developerWorks
def count(self, item): return self.data.count(item) def index(self, item): return self.data.index(item) def reverse(self): self.data.reverse() def sort(self, *args): apply(self.data.sort, args) def extend(self, other): if isinstance(other, UserList): self.data.extend(other.data) else: self.data.extend(other)
Nested classes
Like functions, classes can be nested. Nested classes in Jython work similarly to static inner classes in the Java language. Here's an example:
def __init__ (self): self.data = Data() def set (self, name, value): setattr(self.data, name, value) def get (self, name, default=None): return getattr(self.data, name, default)
Page 24 of 100
ibm.com/developerWorks
: def myFunc(x): print "x at entry:", x : print "x at exit:", x return x : z = myFunc(20)
Page 25 of 100
ibm.com/developerWorks
C:\Articles>jython \jython-2.1\lib\pdb.py factor.py > C:\Articles\<string>(0)?() (Pdb) step > C:\Articles\<string>(1)?() (Pdb) step > C:\Articles\factor.py(0)?() (Pdb) list 67 62 try: 63 print "For", value, "result =", fac.calculate(value) 64 except ValueError, e: 65 print "Exception -", e 66 67 doFac(-1) 68 doFac(0) 69 doFac(1) 70 doFac(10) 71 doFac(100) 72 doFac(1000) (Pdb) tbreak 67 Breakpoint 1 at C:\Articles\factor.py:67 (Pdb) continue factor.py running... Deleted breakpoint 1 > C:\Articles\factor.py(67)?() -> doFac(-1) (Pdb) next For -1 result = Exception - only positive integers supported: -1 > C:\Articles\factor.py(68)?() -> doFac(0) (Pdb) next For 0 result = 1 > C:\Articles\factor.py(69)?() -> doFac(1) (Pdb) next For 1 result = 1 > C:\Articles\factor.py(70)?() -> doFac(10) (Pdb) next For 10 result = 3628800 > C:\Articles\factor.py(71)?() -> doFac(100) (Pdb) next For 100 result = 93326215443944152681699238856266700490715968264381621468592963895217599 99322991560894146397615651828625 3697920827223758251185210916864000000000000000000000000 > C:\Articles\factor.py(72)?() -> doFac(1000) (Pdb) next For 1000 result = 402387260077 ... many other digits deleted ... 0000000000000000000000 --Return-> C:\Articles\factor.py(72)?()->None -> doFac(1000) (Pdb) next --Return--
Page 26 of 100
ibm.com/developerWorks
To learn more about debugging with the Jython debugger, see Appendix C: Jython debugger commands on page 79 .
Jython profiler
Sometimes you may notice that a Jython program runs longer than you expect. You can use the Jython profiler to find out what sections of the program take the longest time and optimize them. The profiler will let you profile entire programs or just individual functions. Here's an example run, profiling the factor.py program (see The factorial engine: factor.py on page 67 ):
c:\>jython \jython-2.1\lib\profile.py \articles\factor.py \articles\factor.py running... For -1 result = Exception - only positive integers supported: -1 For 0 result = 1 For 1 result = 1 For 10 result = 3628800 For 100 result = 93326215443944152681699238856266700490715968264381621468592963895217599 99322991560894146397615651828625369792082722375825118521091686400000000 0000000000000000 For 1000 result = 402387260077 ... many other digits deleted ... 0000000000000000000000 237 function calls (232 primitive calls) in 0.250 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall 1 0.130 0.130 0.240 0.240 1 0.000 0.000 0.110 0.110 220 0.010 0.000 0.010 0.000 factor.py:27(fireListeners) 6 0.060 0.010 0.070 0.012 1 0.000 0.000 0.000 0.000 1 0.000 0.000 0.000 0.000 6/1 0.040 0.007 0.110 0.110 1 0.010 0.010 0.250 0.250 profile:0(execfile('\\articles\\factor.py')) 0 0.000 0.000 filename:lineno(function) <string>:0(?) factor.py:0(?) \ factor.py:34(calculate) factor.py:5(Factorial) factor.py:6(__init__) factor.py:61(doFac) \ profile:0(profiler)
From this run you can see that (besides the initial startup code) most of the program time is being used by the calculate function. For more information on profiling
Page 27 of 100
ibm.com/developerWorks
Assertions
Like C and the Java language (as of version 1.4), Jython supports assertions. Assertions are conditions that must be true for the program to work correctly; if they are not true the program may behave unpredictably. Often they are used to validate input values to functions. Jython's support for assertions comes in the form of the following assert statement:
Note that expression is any Jython expression; if it is false an exceptions.AssertionError exception is raised. If message is provided, it becomes the message associated with the exception. For example:
: def myFunc(x): assert x >= 0, "argument %r must be >= 0" % x return fac(x) : z = myFunc(20) # no exception raised z = myFunc(-1) # AssertionError raised
Page 28 of 100
ibm.com/developerWorks
from java import util class MyArray(util.ArrayList): # subclass a Java class : def get (self, index): # override the get method "@sig public java.lang.Object get(int index)" if 0 <= index < self.size: return util.ArrayList.get(self, index) return None # OutOfBounds now returns null
After being compiled by jythonc the above class can be used in Java code anywhere an java.util.ArrayList instance can be used. Note that when calling a superclass method, the self value is passed as an argument.
Page 29 of 100
ibm.com/developerWorks
For example the Java methods long getTime() { ... } and void setTime(long t) { ... } define the long property time. Thus a Jython reference d.time is automatically and dynamically converted into the Java expression d.getTime(). Jython can also set properties, thus d.time = 1000000L is allowed. The Jython reference d.time = value is automatically and dynamically converted into the Java expression d.setTime(value). Once this change is applied, the print statement from Calling Java classes from Jython on page 30 results in the following:
Page 30 of 100
ibm.com/developerWorks
1: 2: 3: 4: 5: 6: 7:
from javax import swing import sys f = swing.JFrame(sys.argv[1], size=(200,200), defaultCloseOperation=swing.JFrame.EXIT_ON_CLOSE) f.contentPane.add(swing.JLabel(sys.argv[2])) f.visible = 1
This code sequence creates and shows a GUI frame window. The script's first command-line argument becomes the title and the second the content text. Line 4 creates the frame, passing in the title, the desired size, and a close action. The size and defaultCloseOperation parameters are properties as described above and, as such, may be (quite conveniently) set in the JFrame's constructor when invoked from a Jython program. The title is set as a parameter of the JFrame's equivalent of the __init__ method. Line 6 accesses the JFrame's contentPane property and calls its add method to add a JLabel to show the second argument. Line 7 makes the frame visible by setting its visible property to 1 (true). A sample of this GUI is shown below:
Page 31 of 100
ibm.com/developerWorks
Jython does not support overloaded methods, which are methods with the same name but with differing number and/or types of arguments. Instead, Jython supports defaulted arguments and variable number of arguments, which can create a problem if you inherit from a Java class that uses overloading and you want to override the overloaded methods. In Jython, you must define the base method and accept a varying number of arguments. Consider the (rather impractical) example of an InputStream that always returns a blank:
from java import io class AlwaysBlank(io.InputStream): # covers all forms of read(...) def read(self, *args): if len(args) > 0: # covers forms: int read(byte[]) # int read(byte[], int off, int len) return apply(io.InputStream.read, (self,) + args) else: # covers form: int read() return ord(' ')
Page 32 of 100
ibm.com/developerWorks
Array support is provided by the jarray module. The two functions in the jarray module, zeros and array, are used to create arrays. The array function maps a Jython sequence to a Java array. Some examples are as follows:
from jarray import zeros, array from java import util from javax import swing a1 = zeros(100, 'i') a2 = array([1,2,10,-5,7], 'i') # an array of 100 int 0s # an array of ints as listed
# an array of doubles 0.0 to 49.0 a3 = array([i * 1.0 for i in range(50)], 'd') a4 = zeros(10, util.Map) # an array of 10 null Maps a5 = array((swing.JFrame("F1"), # an array of 3 JFrames swing.JFrame("F2"), swing.JFrame("F3")), swing.JFrame) a6 = array("Hello", 'c') # an array of characters
See Appendix A: Character codes for array types on page 76 for a listing of character codes for array types.
Page 33 of 100
ibm.com/developerWorks
The start_new_thread function runs the function argument in a new Java thread, passing the args tuple value to the function. The exit function can be used in the thread to end it (generally as the target of an if statement).
Java synchronization
When developing multithreaded programs using Java or Jython threads, it is sometimes necessary to create synchronized functions (or methods). Synchronized functions are functions that can only be called from one thread at a time; meaning that other threads are prevented from entering the function until the first thread exits. Jython provides the synchronized module and two functions to create synchronized functions. The functions are of the following form:
The make_synchronized function permanently synchronizes the function argument. The apply_synchronized function temporarily synchronizes on syncobj and then calls the function argument.
Page 34 of 100
ibm.com/developerWorks
from synchronize import * from java import lang # define synchronization helpers def waitForSignal (monitor): """ Wait until the monitor is signaled. """ lang.Object.wait(monitor) # replace with synchronized version; syncs on 1st argument waitForSignal = make_synchronized(waitForSignal) def notifySignal (monitor): """ Signal monitor. """ lang.Object.notifyAll(monitor) # replace with synchronized version; syncs on 1st argument notifySignal = make_synchronized(notifySignal) class Gui: # GUI support : def doExit (self): self.visible = 0 notifySignal(self) if __name__ == "__main__": # main code : gui = Gui() : print "Waiting until GUI exit requested..." waitForSignal(gui) print "Done"
""" A Jython Thread Example. """ from from from from java import lang synchronize import * thread import start_new_thread sys import stdout
Page 35 of 100
ibm.com/developerWorks
def __waitForSignal (monitor): apply_synchronized(monitor, lang.Object.wait, (monitor,)) def __signal (monitor): apply_synchronized(monitor, lang.Object.notifyAll, (monitor,)) def __xprint (stream, msg): print >>stream, msg def xprint (msg, stream=stdout): """ Synchronized print. """ apply_synchronized(stream, __xprint, (stream, msg)) class Buffer: """ A thread-safe buffer. """ def __init__ (self, limit=-1): self.__limit = limit # the max size of the buffer self.__data = [] self.__added = () # used to signal data added self.__removed = () # used to signal data removed def __str__ (self): return "Buffer(%s,%i)" % (self.__data, self.__limit) def __len__ (self): return len(self.__data) def add (self, item): """ Add an item. Wait if full. """ if self.__limit >= 0: while len(self.__data) > self.__limit: __waitForSignal(self.__removed) self.__data.append(item); xprint("Added: %s" % item) __signal(self.__added) def __get (self): item = self.__data.pop(0) __signal(self.__removed) return item def get (self, wait=1): """ Remove an item. Wait if empty. """ item = None if wait: while len(self.__data) == 0: __waitForSignal(self.__added) item = self.__get() else: if len(self.__data) > 0: item = self.__get() xprint("Removed: %s" % item) return item get = make_synchronized(get)
Page 36 of 100
ibm.com/developerWorks
class Producer: def __init__ (self, name, buffer): self.__name = name self.__buffer = buffer def __add (self, item): self.__buffer.add(item) def __produce (self, *args): for item in args: self.__add(item) def produce (self, items): start_new_thread(self.__produce, tuple(items)) class Consumer: def __init__ (self, name, buffer): self.__name = name self.__buffer = buffer def __remove (self): item = self.__buffer.get() return item def __consume (self, count): for i in range(count): self.__remove() def consume (self, count=1): start_new_thread(self.__consume, (count,))
# all producers and consumer share this one buf = Buffer(5) p1 p2 p3 p4 c1 = = = = = Producer("P1", Producer("P2", Producer("P3", Producer("P4", Consumer("C1", buf) buf) buf) buf) buf)
Page 37 of 100
ibm.com/developerWorks
c2 = Consumer("C2", buf) # create 6 items p1.produce(["P1 Message " + str(i) for i in range(3)]) p2.produce(["P2 Message " + str(i) for i in range(3)]) # consume 20 items for i in range(5): c1.consume(2) c2.consume(2) # create 20 more items p3.produce(["P3 Message " + str(i) for i in range(10)]) p4.produce(["P4 Message " + str(i) for i in range(10)]) # consume 4 items c1.consume(2) c2.consume(2) # let other threads run lang.Thread.currentThread().sleep(5000) xprint("Buffer has %i item(s)left" % len(buf))
Added: P1 Message 0 Added: P1 Message 1 Added: P1 Message 2 Added: P2 Message 0 Added: P2 Message 1 Added: P2 Message 2 Removed: P1 Message Removed: P1 Message Removed: P1 Message Removed: P2 Message Removed: P2 Message Removed: P2 Message Added: P3 Message 0 Removed: P3 Message Added: P3 Message 1 Removed: P3 Message Added: P3 Message 2 Removed: P3 Message Added: P3 Message 3 Removed: P3 Message Added: P3 Message 4 Removed: P3 Message Added: P3 Message 5
0 1 2 0 1 2 0 1 2 3 4
Added: P3 Message 7 Removed: P3 Message Added: P3 Message 8 Removed: P3 Message Added: P3 Message 9 Removed: P3 Message Added: P4 Message 0 Removed: P4 Message Added: P4 Message 1 Removed: P4 Message Added: P4 Message 2 Removed: P4 Message Added: P4 Message 3 Removed: P4 Message Added: P4 Message 4 Added: P4 Message 5 Added: P4 Message 6 Added: P4 Message 7 Added: P4 Message 8 Added: P4 Message 9 Removed: P4 Message Removed: P4 Message Removed: P4 Message
7 8 9 0 1 2 3
4 5 6
Page 38 of 100
ibm.com/developerWorks
Page 39 of 100
ibm.com/developerWorks
""" This module defines several functions to ease interfacing with Java code.""" from types import * from java import lang from java import util from java import io # only expose these __all__ = ['loadProperties', 'getProperty', 'mapToJava', 'mapFromJava', 'parseArgs']
def loadProperties (source): """ Load a Java properties file into a Dictionary. """ result = {} if type(source) == type(''): # name provided, use file
Page 40 of 100
ibm.com/developerWorks
source = io.FileInputStream(source) bis = io.BufferedInputStream(source) props = util.Properties() props.load(bis) bis.close() for key in props.keySet().iterator(): result[key] = props.get(key) return result def getProperty (properties, name, default=None): """ Gets a property. """ return properties.get(name, default)
import sys file = sys.argv[1] props = loadProperties(file) print "Properties file: %s, contents:" % file print props print "Property %s = %i" % ('debug', int(getProperty(props, 'debug', '0')))
Properties file: test.properties, contents: {'error.level': 'ERROR', 'debug': '1', 'now.is.the.time': 'false'} Property debug = 1
Page 41 of 100
ibm.com/developerWorks
functions in the following example (a continuation of the JavaUtils.py module excerpt from Wrapping Java services in Jython on page 40 ):
def mapMapFromJava (map): """ Convert a Map to a Dictionary. """ result = {} iter = map.keySet().iterator() while iter.hasNext(): key = iter.next() result[mapFromJava(key)] = mapFromJava(map.get(key)) return result def mapCollectionFromJava (coll): """ Convert a Collection to a List. """ result = [] iter = coll.iterator(); while iter.hasNext(): result.append(mapFromJava(iter.next())) return result def mapFromJava (object): """ Convert a Java type to a Jython type. """ if object is None: return object if isinstance(object, util.Map): result = mapMapFromJava(object) elif isinstance(object, util.Collection): result = mapCollectionFromJava(object) else: result = object return result def mapSeqToJava (seq): """ Convert a sequence to a Java ArrayList. """ result = util.ArrayList(len(seq)) for e in seq: result.add(mapToJava(e)); return result def mapDictToJava (dict): """ Convert a Dictionary to a Java HashMap. """ result = util.HashMap() for key, value in dict.items(): result.put(mapToJava(key), mapToJava(value)) return result def mapToJava (object): """ Convert a Jython type to a Java type. """ if object is None: return object t = type(object) if t == TupleType or t == ListType: result = mapSeqToJava(object) elif t == DictType: result = mapDictToJava(object) else: result = object return result
Page 42 of 100
ibm.com/developerWorks
After using mapToJava, these types can be written to a java.io.ObjectOutputStream. After reading an object from a java.io.ObjectInputStream, you can use mapFromJava to convert the object back to a Jython type. Note that these methods support a limited but broadly used set of built-in Jython types. Jython automatically converts value-like types such as numbers and strings. User defined classes are not supported.
data = (1,2,3, [1,2,3], [c for c in "Hello!"], "Hello!", {1:'one', 2:'two'}) print "data:", data toJava = mapToJava(data) print "toJava:", toJava fromJava = mapFromJava(toJava) print "fromJava:", fromJava print print "type(%s)=%s" % ("data", type(data)) print "type(%s)=%s" % ("toJava", type(toJava)) print "type(%s)=%s" % ("fromJava", type(fromJava))
prints:
data: (1, 2, 3, [1, 2, 3], ['H', 'e', 'l', 'l', 'o', '!'], 'Hello!', \ {2: 'two', 1: 'one'}) toJava: [1, 2, 3, [1, 2, 3], [H, e, l, l, o, !], Hello!, {2=two, 1=one}] fromJava: [1, 2, 3, [1, 2, 3], ['H', 'e', 'l', 'l', 'o', '!'], 'Hello!', \ {2: 'two', 1: 'one'}] type(data)=org.python.core.PyTuple type(toJava)=org.python.core.PyJavaInstance type(fromJava)=org.python.core.PyList
Notice that the PyTuple became a PyJavaInstance and then a PyList. Also notice that the toJava form formats differently. This is because it is a Java object and it's being printed by the Java toString() method, not Jython repr() function. PyJavaInstance is a type Jython will pass as is to a Java API. Finally, notice that the data and fromJava values are the same except that the tuple is now an equivalent list. For more about Jython types see Appendix L: Jython types summary on page 99 .
Page 43 of 100
ibm.com/developerWorks
def parseArgs (args, validNames, nameMap=None): """ Do some simple command line parsing. """ # validNames is a dictionary of valid switch names # the value (if any) is a conversion function switches = {} positionals = [] for arg in args: if arg[0] == '-': # a switch text = arg[1:] name = text; value = None posn = text.find(':') # any value comes after a : if posn >= 0: name = text[:posn] value = text[posn + 1:] if nameMap is not None: # a map of valid switch names name = nameMap.get(name, name) if validNames.has_key(name): # or - if name in validNames: mapper = validNames[name] if mapper is None: switches[name] = value else: switches[name] = mapper(value) else: print "Unknown switch ignored -", name else: positionals.append(arg) return positionals, switches # a positional argument
from sys import argv from JavaUtils import parseArgs switchDefs = {'s1':None, 's2':int, 's3':float, 's4':int} args, switches = parseArgs(argv[1:], switchDefs) print "args:", args print "switches:", switches
Page 44 of 100
ibm.com/developerWorks
args: ['1', '2', '3', 'ss'] switches: {'s4': 2, 's2': 1, 's1': None}
Page 45 of 100
ibm.com/developerWorks
="Barry Feigenbaum" = '12345 Any Street" 100; v2 = v1 * 1.5; v3 = -v2; v4 = 1 / v2 "String 1"; s2 = "String 2" = "The rain in Spain falls mainly on the plain."
Page 46 of 100
ibm.com/developerWorks
line:
print "abc" + "xyz"
prints: abcxyz. To select a character or characters (that is, a substring) from a string you use indexing. For example: "abcxwy"[2] yields c, while "abcxwy"[2:4] yields cx. Many of the string functions test conditions, thus they are often used in conjunction with the if and while statements. Here's an example of how we could use containment testing to see if a character were contained in a string:
if ' ' in name: print "space found" -- or -if 'q' not in sent: print "q not found"
In addition to testing conditions, strings also support methods to test the nature of the string. These are islower, isupper, isalnum, isnum, isalpha, isspace, and istitle. These methods test to see if all the characters in the strings meet these conditions.
Additional methods
Strings support several methods that allow you to find and edit sub-strings, change case, and a host of other actions. To find a string in another string use the find/rfind or startswith/endswidth methods. For example:
if name.find(' ') >= 0: print "space found" -- or -if name.find("Jones") < 0: print "Jones not in name"
Sometimes you need to edit the content of a string, for example to change its case or insert or remove text from it. Jython supplies several methods to do this. To change case, Jython has the lower, upper, swapcase, title, and capitalize methods. To change the text of a string, use the replace method. For example, to match strings often you want to ignore case or you may want to replace sub-strings:
if
-- or --
Page 47 of 100
ibm.com/developerWorks
Often strings have extra blanks around them that are not important, such as when the string is entered by a user. To remove these extra blanks use the lstrip, rstrip, or strip methods. For example, to match a command entered by a user:
Often you need to break strings into parts, such as the words in a sentence or join multiple strings into one string. Jython supports the split, splitlines, and join functions to do this. The split method splits a line into words, while splitlines splits a file of lines into separate lines. The join method reverses split. You can also join strings by concatenation as discussed above. For example, to extract the words from a sentence and then rebuild the sentence use:
fname = "Barry"; lname = "Feigenbaum" address = "1234 any St." city = "Anytown"; state = "TX"; zip = "12345" age = 30 children = 3 : print "Hello %(fname)s from %(city)s, %(state)s." % locals()
prints Hello Barry from Anytown, TX. See Appendix J: Formatting strings and values on page 94 for more about formatting program variables.
Page 48 of 100
ibm.com/developerWorks
Below are some format (%) operator examples. See Appendix J: Formatting strings and values on page 94 for more examples. Expression "Hello %s" % "Barry" "Count: %i, " "Avg Cost: $%.2f; " "Max Cost: $%.2f" % (10, 10.5, 50.25) "This is %i%%" % 10 "My name is %(first)s %(last)s!" % {'last':'Feigenbaum', 'first':'Barry', 'mi':'A'} Result Hello Barry Count: 10, Avg Cost: $10.50; Max Cost: $50.25 This is 10% My name is Barry Feigenbaum!
def printf(stream, format, *pargs, **kwargs): # see Printing to files on page 62 for more information if pargs: print >>stream, format % pargs elif kwargs: print >>stream, format % kwargs else: print >>stream, format
from sys import stdout printf(stdout, "%s is %.1f years old and has %i children", fname, age, children) printf(stdout, "The %(name)s building has %(floors)d floors", floors=105, name="Empire State") printf(stdout, "Hello World!")
print:
Page 49 of 100
ibm.com/developerWorks
Pretty printing
You can use the pprint module functions, in particular the pformat function, to print complex data structures in a formatted form. For example, this code:
data = [[1,2,3], [4,5,6],{'1':'one', '2':'two'}, "jsdlkjdlkadlkad", [i for i in xrange(10)]] print "Unformatted:"; print data print from pprint import pformat print "Formatted:"; print pformat(data)
Unformatted: [[1, 2, 3], [4, 5, 6], {'2': 'two', '1': 'one'}, \ 'jsdlkjdlkadlkad', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] Formatted: [[1, 2, 3], [4, 5, 6], {'2': 'two', '1': 'one'}, 'jsdlkjdlkadlkad', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
import sys def stripLines (lines): """ Removed extra whitespace (that is, newlines). """ newlines = []
Page 50 of 100
ibm.com/developerWorks
for line in lines: line = line.strip() newlines.append(line) return newlines def splitParagraphs (lines): """ Splits a set of lines into paragraphs. """ paras = [] para = "" for line in lines: if len(line) > 0: # in paragraph para += ' ' + line else: # between paragraphs para = para.strip() if len(para) > 0: paras.append(para) para = "" return paras class Formatter: """ Formats and prints paragraphs. """
def __init__ (self, stream, pagelen=66, linewidth=85, lmargin=10, rmargin=10, pindent=5, alignment="justify", headers=None, footers=None): self.stream = stream # stream to print on # format settings self.pagelen = pagelen self.pindent = pindent self.linewidth = linewidth self.lmargin = lmargin self.rmargin = rmargin self.headers = headers self.footers = footers self.alignment = alignment self.pagecount = 1 self.linecount = 0 def genLine (self, line): print >>self.stream, line self.linecount += 1 def outputLine (self, line): self.testEndPage() if not (self.linecount == 0 and len(line) == 0): self.genLine(line) def newPage (self): if self.headers: self.outputHeader() def padPage (self): while self.linecount < self.pagelen: self.genLine("") # current page # current line
Page 51 of 100
ibm.com/developerWorks
def endPage (self): if self.footers: if len(self.footers) + self.linecount < self.pagelen: self.padPage() self.outputFooter() else: if self.linecount < self.pagelen: self.padPage() self.linecount = 0 self.pagecount += 1 self.genLine('-' * 20) def testEndPage (self): if self.footers: if len(self.footers) + 1 + self.linecount >= self.pagelen: self.endPage() self.newPage() else: if self.linecount >= self.pagelen: self.endPage() self.newPage() def padLine (self, line, firstline=0, lastline=0): """ Add spaces as needed by alignment mode. """ if self.alignment == "left": adjust = firstline * self.pindent #line = line
elif self.alignment == "center": adjust = 0 pad = self.linewidth - adjust - len(line) line = ' ' * (pad / 2) + line elif self.alignment == "right": adjust = 0 pad = self.linewidth - adjust - len(line) line = ' ' * pad + line elif self.alignment == "justify": adjust = firstline * self.pindent pad = self.linewidth - adjust - len(line) line = "" # add 1+ spaces between words to extend line words = line.split() xpad = pad for word in words: line += word + ' ' if not lastline and xpad > 0: line += ' ' * (pad / len(words) + 1) xpad -= 1 line = line.strip() return ' ' * adjust + line def format (self, line, firstline=0, lastline=0): # indent by left margin
Page 52 of 100
ibm.com/developerWorks
return ' ' * self.lmargin + \ self.padLine(line.strip(), firstline, lastline) def formatParagraph (self, para): lcount = 0 adjust = self.pindent line = "" # process by words words = para.split(' ') for word in words: line += ' ' # about to get too long if len(line) + len(word) > self.linewidth - adjust: line = self.format(line, lcount == 0, 0) self.outputLine(line) line = "" lcount += 1 adjust = 0 line += word # output last (only) line if len(line) > 0: line = self.format(line, lcount == 0, 1) self.outputLine(line) def outputHeader (self): for line in self.headers: self.genLine(' ' * self.lmargin + line.center(self.linewidth)) self.genLine("") def outputFooter (self): self.genLine("") for line in self.footers: self.genLine(' ' * self.lmargin + line.center(self.linewidth)) def outputPages (self, paras): """ Format and print the paragraphs. """ self.newPage() for para in paras: self.formatParagraph(para) self.outputLine("") self.endPage()
Page 53 of 100
ibm.com/developerWorks
Page 54 of 100
ibm.com/developerWorks
axxxxc ? - optional subpattern a.?c abc ac aabc + - required repeating subpattern a.+c abc abbc axxc ...|... - choice of subpattern abc|def abcef abdef (...) - grouping a(xx)|(yy)c axxc ayyc axxyyc (...)* - repeating grouping a(xx)*c ac axxc axxxxc (...)+ - required repeating grouping \c - match a special character \s - matches white space a(xx)+c axxc axxxxc \.\?\*\+ .?*+ ac axxbxxc ?.*+ abcd a\s*z az az a z za za abyz axxbxxc abef abcdef axc ayc ac abcd
ibm.com/developerWorks
match(pattern, string {, options}) search(pattern, string {, options}) findall(pattern, string) split(pattern, string {, max}) sub(pattern, repl, string {, max})
Matches pattern at the string start Matches pattern somewhere in the string Matches all occurrences of pattern in the string Splits the string at matching points and returns the results in a list Substitutes the match with repl for max or all occurrences; returns the result
subn(pattern, repl, string {, Substitutes the match with repl for max max}) or all occurrences; returns the tuple (result, count) Note that the matching functions return None if no match is found. Otherwise the match functions will return a Match object from which details of the match can be found. See the Python Library Reference for more information on Match objects.
import re # do a fancy string match if re.search(r"^\s*barry\s+feigenbaum\s*$", name, re.I): print "It's Barry alright" # replace the first name with an initial name2 = re.sub(r"(B|b)arry", "B.", name)
If you are going to use the same pattern repeatedly, such as in a loop, you can speed up execution by using the compile function to compile the regular expression into a Pattern object and then using that object's methods, as shown here:
import re patstr = r"\s*abc\s*" pat = re.compile(patstr) # print all lines matching patstr for s in stringList: if pat.match(s, re.I): print "%r matches %r" % (s, patstr)
Page 56 of 100
ibm.com/developerWorks
""" A simplified form of Grep. """ import sys, re if len(sys.argv) != 3: print "Usage: jython grep.py <pattern> <file>" else: # process the arguments pgm, patstr, filestr = sys.argv print "Grep - pattern: %r file: %s" % (patstr, filestr) pat = re.compile(patstr) # prepare the pattern # see File I/O in Jython on page 58 for more information file = open(filestr) # access file for read lines = file.readlines() # get the file file.close() count = 0 # process each line for line in lines: match = pat.search(line) # try a match if match: # got a match print line print "Matching groups: " + str(match.groups()) count += 1 print "%i match(es)" % count
When run on the words.txt file from File I/O in Jython on page 58 , the program produces the following result:
C:\Articles>jython grep.py "(\w*)!" words.txt Grep - pattern: '(\\w*)!' file: words.txt How many times must I say it; Again! again! and again! Matched on: ('Again',) Singing in the rain! I'm singing in the rain! \ Just singing, just singing, in the rain! Matched on: ('rain',) 2 match(es)
Page 57 of 100
ibm.com/developerWorks
The mode string has the following syntax: (r|w|a){+}{b}; the default mode is r. Here is a listing of all the available access mode strings: r: read w: write a: append to the end of the file +: update b: binary (vs. text) The name of the file is accessed through the name attribute. The mode of the file is accessed through the mode attribute.
Comment(s) Flush and close an open file Outputs any buffered data Reads up to size (or the whole file) Read a line (including ending '\n') up to size Reads the file and returns a list of lines (including
Introduction to Jython, Part 2: Programming essentials
ibm.com/developerWorks
ending '\n') seek(offset {, mode}) tell() truncate({size}) write(string) writelines(lines) Seek to a position, mode: 0 - start of file, 1 current offset, 2 - end of file Return the current offset Truncate (delete extra content) to current offset or specified size Write the string to a file. To write lines, end the string in '\n' Write the list as a set of strings. To write lines, end each string in '\n'
import sys f = open(sys.argv[1], "rb") bin = f.read() f.close() f = open(sys.argv[2], "wb") f.write(bin) f.close() # open binary for reading # open binary (truncated) for write
import sys f = open(sys.argv[1], "r") # read the file by lines lines = f.readlines() f.close() lines.sort() # sort and print the lines print "File %s sorted" % f.name print lines
Page 59 of 100
ibm.com/developerWorks
import sys def clean (word): """ Remove any punctuation and map to a common case. """ word = word.lower() # remove any special characters while word and word[-1] in ".,;!": word = word[:-1] while word and word[0] in ".,;!": word = word[1:] return word words = {} # set of unique words and counts
if len(sys.argv) != 2: print "Usage: jython wcount.py <file>" else: file = open(sys.argv[1]) # access file for read lines = file.readlines() # get the file file.close() # process each line for line in lines: # process each word in the line for word in line.split(): word = clean(word) words[word] = words.get(word, 0) + 1 # report the results keys = words.keys() keys.sort() for word in keys: print "%-5i %s" % (words[word], word)
Output of words.txt
Given the following input file (words.txt)
Now is the time for all good men to come to the aid of their country. The rain in Spain falls mainly on the plain. How many times must I say it; Again! again! and again! Singing in the rain! I'm singing in the rain! \ Just singing, just singing, in the rain!
the word-counting program (from A word-counting program in Jython on page 59 ) would return the following results (wrapped into two columns to save space):
3 1 1 1 1
1 1 1 1 1
Page 60 of 100
ibm.com/developerWorks
1 1 1 1 1 1 1 4 1 1 2 1
1 1 4 1 4 1 7 1 1 1 2
import java.io.*; import java.util.*; import java.text.*; public class WordCounter { protected static final String specials = ".,;!"; /** Remove any punctuation and map to a common case. */ protected static String clean(String word) { word = word.toLowerCase(); // remove any special characters while (word.length() > 0 && specials.indexOf(word.charAt(word.length() - 1)) >= 0) { word = word.substring(0, word.length() - 1); } while (word.length() > 0 && specials.indexOf(word.charAt(0)) >= 0) { word = word.substring(1); } return word; } protected static Map words = new HashMap(); public static void main(String[] args) throws IOException { if (args.length != 1) { System.out.println("Usage: java WordCounter <file>"); } else { // access file for read
Page 61 of 100
ibm.com/developerWorks
FileInputStream fis = new FileInputStream(args[0]); DataInputStream dis = new DataInputStream(fis); List lines = new ArrayList(); // get the file for (String line = dis.readLine(); line != null; line = dis.readLine()) { lines.add(line); } dis.close(); // process each line for (int i = 0; i < lines.size(); i++) { String line = (String)lines.get(i); System.out.println("Processing: " + line); String[] xwords = line.split("\\s+"); for (int w = 0; w < xwords.length; w++) { String word = clean(xwords[w]); if (word.length() > 0) { Integer count = (Integer)words.get(word); if (count == null) { count = new Integer(0); } // update the count words.put(word, new Integer(count.intValue() + 1)); } } } // report the results String[] keys = (String[])words.keySet(). toArray(new String[words.size()]); Arrays.sort(keys); MessageFormat form = new MessageFormat( "{0,number, #########0} {1}"); for (int i = 0; i < keys.length; i++) { System.out.println(form.format( new Object[] {words.get(keys[i]), keys[i]})); } } } }
Printing to files
The print statement can print to a file by use of the ">>" operator. By default it prints to the console (actually the value of sys.stdout). For example, the following commands are equivalent:
Page 62 of 100
ibm.com/developerWorks
Jython allows alternate target files. For example, to print to the standard error stream use:
ibm.com/developerWorks
A pickling example
Here's an example of pickle at work. The following code sequence
import pickle class Data: def __init__ (self, x, y): self.__x = x self.__y = y def __str__ (self): return "Data(%s,%s)" % (self.__x, self.__y) def __eq__ (self, other): return self.__x == other.__x and self.__y == other.__y data = Data(10, "hello") file = open("data.pic", 'w') pickle.dump(data, file) file.close() file = open("data.pic", 'r') newdata = pickle.load(file) file.close() print print print print "data:", data "newdata:", newdata "data is newdata:", data is newdata "data == newdata:", data == newdata
prints this:
data: Data(10,hello) newdata: Data(10,hello) data is newdata: 0 (false) data == newdata: 1 (true)
The file created is in (semi-)readable plain text. For example, the above code created the file data.pic:
(i__main__ Data p0
Page 64 of 100
ibm.com/developerWorks
Note that Jython cannot pickle objects that are Java objects, reference Java objects, or subclass Java classes. To do this you need to use the java.io.ObjectOutputStream and java.io.ObjectInputStream classes.
Object shelves
As shown in the previous panel, Jython can store objects into a file. Using a file per object can cause problems (that is, it can waste space and you will need to name each file). Jython supports a file that can hold multiple objects, called a shelf. A shelf acts much like a persistent dictionary. To create shelves, use the open function of module shelve. For example, the following code:
import shelve, sys def printshelf (shelf, stream=sys.stdout): for k in shelf.keys(): print >>stream, k, '=', shelf[k] def clearshelf (shelf): for k in shelf.keys(): del shelf[k] # create shelf shelf = shelve.open("test.shelf") clearshelf(shelf) shelf["x"] = [1,2,3,4] shelf["y"] = {'a':1, 'b':2, 'c':3} printshelf(shelf) shelf.close() print # update shelf shelf = shelve.open("test.shelf") printshelf(shelf) print shelf["z"] = sys.argv[1] printshelf(shelf) shelf.close() print # verify shelf persistent # print the entries in a shelf
Page 65 of 100
ibm.com/developerWorks
x = [1, 2, 3, 4] y = {'b': 2, 'a': 1, 'c': 3} x = [1, 2, 3, 4] y = {'b': 2, 'a': 1, 'c': 3} x = [1, 2, 3, 4] z = This is a test string y = {'b': 2, 'a': 1, 'c': 3} x = [1, 2, 3, 4] z = This is a test string y = {'b': 2, 'a': 1, 'c': 3}
Note that the open function produces two files based on the file name passed to open: <filename>.dir is a directory into the persistent data <filename>.dat is the saved persistent object data
Page 66 of 100
ibm.com/developerWorks
Page 67 of 100
ibm.com/developerWorks
-- import the needed modules import sys import exceptions -- create the Factorial class, a general purpose factorial calculation engine class Factorial: """A general purpose factorial calculation engine""" -- define the constructor def __init__ (self): self.__listeners = [] self.__cancelled = 0 -- allow other classes to register event listeners; --used to track calculation progress -- A "listener" is a function that takes an integer % argument def addListener (self, listener): if listener not in self.__listeners: self.__listeners.append(listener) def addListeners (self, listeners): for l in listeners: self.addListener(l) def removeListener (self, listener): self.__listeners.remove(listener) def removeListeners (self, listeners): for l in listeners: self.removeListener(l) def fireListeners (self, value): # notify all listeners for func in self.__listeners: func(value) -- allow others to cancel a long running calculation def cancel (self): self.__cancelled = 1 -- perform the factorial calculation; -may take a long time (many minutes) for big numbers def calculate (self, value): if type(value) != type(0) or value < 0: raise ValueError, \ "only positive integers supported: " + str(value) self.__cancelled = 0 result = 1L self.fireListeners(0) # 0% done # calculate factorial -- may take quite a while if value > 1: # need to do calculation last = 0 # using iteration (vs. recursion) to increase performance # and eliminate any stack overflow possibility for x in xrange(1, value + 1): if self.__cancelled: break # early abort requested result = result * x # calc next value next = x * 100 / value
Page 68 of 100
ibm.com/developerWorks
if next != last: # signal progress self.fireListeners(next) last = next self.fireListeners(100) # 100% done if self.__cancelled: result = -1 return result # test case if __name__ == "__main__": print sys.argv[0], "running..." fac = Factorial() def doFac (value): try: print "For", value, "result =", fac.calculate(value) except ValueError, e: print "Exception -", e doFac(-1) doFac(0) doFac(1) doFac(10) doFac(100) doFac(1000)
-- import the needed modules import sys import string from types import * from from from from java import lang java import awt java.awt import event as awtevent javax import swing
from factor import Factorial -- PromptedValueLayout is a customized Java LayoutManager not discussed here -but included with the resources from com.ibm.articles import PromptedValueLayout as ValueLayout -- support asynchronous processing class LongRunningTask(lang.Thread): def __init__ (self, runner, param=None): self.__runner = runner # function to run self.__param = param # function parameter (if any) self.complete = 0
Page 69 of 100
ibm.com/developerWorks
self.running = 0 -- Java thread body def run (self): self.complete = if self.__param self.result else: self.result self.complete =
-- start a long running activity def doAsync (func, param): LongRunningTask(func, param).start() -- Swing GUI services must be called only on the AWT event thread, class SwingNotifier(lang.Runnable): def __init__ (self, processor, param=None): self.__runner = processor # function to do GUI updates self.__param = param # function parameter (if any) -- Java thread body def run (self): if self.__param is not None: self.__runner(self.__param) else: self.__runner() def execute (self): swing.SwingUtilities.invokeLater(self) -- define and construct a GUI for factorial calculation class FactorialGui(swing.JPanel): """Create and process the GUI.""" def __init__ (self, engine): swing.JPanel.__init__(self) self.__engine = engine engine.addListener(self.update) self.createGui() def update (self, value): # do on AWT thread SwingNotifier(self.updateProgress, value).execute() def updateProgress (self, value): # display progress updates self.__progressBar.value = value -- Calculate button press handler def doCalc (self, event): # request a factorial self.__outputArea.text = "" ivalue = self.__inputField.text # get value to calculate value = -1 try: value = int(ivalue) # convert it except: pass if value < 0: # verify it self.__statusLabel.text = \ "Cannot make into a positive integer value: " + ivalue else: self.__calcButton.enabled = 0 self.__cancelButton.enabled = 1
Page 70 of 100
ibm.com/developerWorks
msg = "Calculating factorial of %i..." % value if value > 25000: msg += \ "; May take a very long time to complete!" self.__statusLabel.text = msg # tell user we're busy doAsync(self.calcFac, value) # do the calculation -- main calculation worker def calcFac (self, value): stime = lang.System.currentTimeMillis() fac = self.__engine.calculate(value) # time calculation etime = lang.System.currentTimeMillis() svalue = ""; order = 0 if fac >= 0: # we have a result, not cancelled svalue = str(fac); order = len(svalue) - 1 formatted = "" while len(svalue) > 100: # wrap long numbers formatted += svalue[0:100] + '\n' svalue = svalue[100:] formatted += svalue svalue = formatted ftime = lang.System.currentTimeMillis() SwingNotifier(self.setResult, \ (svalue, order, ftime - stime, etime - stime)).execute() -- display the result def setResult (self, values): svalue, order, ttime, ftime = values self.__cancelButton.enabled = 0 if len(svalue) > 0: self.__statusLabel.text = \ "Setting result - Order: 10**%i" % order self.__outputArea.text = svalue self.__statusLabel.text = \ "Total time: %ims, Calc time: %ims, Order: 10**%i" % \ (ttime, ftime, order) else: self.__statusLabel.text = "Cancelled" self.__calcButton.enabled = 1 -- Cancel button press handler def doCancel (self, event): # request a cancel self.__cancelButton.enabled = 0 self.__engine.cancel() -- create (layout) the GUI def createGui (self): # build the GUI self.layout = awt.BorderLayout() progB = self.__progressBar = \ swing.JProgressBar(0, 100, stringPainted=1); inf = self.__inputField = swing.JTextField(5) inl = swing.JLabel("Calculate value of:", swing.JLabel.RIGHT) inl.labelFor = inf outf = self.__outputArea = swing.JTextArea()
Page 71 of 100
ibm.com/developerWorks
outl = swing.JLabel("Result:", swing.JLabel.RIGHT) outl.labelFor = outf calcb = self.__calcButton = \ swing.JButton("Calculate", actionPerformed=self.doCalc, enabled=1, mnemonic=awtevent.KeyEvent.VK_C) cancelb = self.__cancelButton = \ swing.JButton("Cancel", actionPerformed=self.doCancel, enabled=0, mnemonic=awtevent.KeyEvent.VK_L) vl = ValueLayout(5, 5) inp = swing.JPanel(vl) vl.setLayoutAlignmentX(inp, 0.2) inp.add(inl); inp.add(inf, inl) self.add(inp, awt.BorderLayout.NORTH) vl = ValueLayout(5, 5) outp = swing.JPanel(vl) vl.setLayoutAlignmentX(outp, 0.2) outp.add(outl); outp.add(swing.JScrollPane(outf), outl) xoutp = swing.JPanel(awt.BorderLayout()) xoutp.add(progB, awt.BorderLayout.NORTH) xoutp.add(outp, awt.BorderLayout.CENTER) self.add(xoutp, awt.BorderLayout.CENTER) sp = swing.JPanel(awt.BorderLayout()) bp = swing.JPanel() bp.add(calcb) bp.add(cancelb) sp.add(bp, awt.BorderLayout.NORTH) sl = self.__statusLabel = swing.JLabel(" ") sp.add(sl, awt.BorderLayout.SOUTH) self.add(sp, awt.BorderLayout.SOUTH) -- main entry point; launches the GUI in a frame if __name__ == "__main__": print sys.argv[0], "running..." frame = swing.JFrame("Factorial Calculator", defaultCloseOperation=swing.JFrame.EXIT_ON_CLOSE) cp = frame.contentPane cp.layout = awt.BorderLayout() cp.add( FactorialGui(Factorial()) ) frame.size = 900, 500 frame.visible = 1
Page 72 of 100
ibm.com/developerWorks
Resources
Download the jython2-source.zip for this tutorial. Visit the Jython home page to download Jython. Take the first part of this tutorial "Introduction to Jython, Part 1: Java programming made easier" (developerWorks, April 2004). Jython modules and packages enable reuse of the extensive standard Java libraries. Learn more about the Java libraries (and download the current version of the JDK) on the Sun Microsystems Java technology homepage. You'll find an entire collection of Python docs and tutorials (including the Python Library Reference) and more information about regular expressions on the Python
Page 73 of 100
ibm.com/developerWorks
home page. You can also learn more about regular expressions from the tutorial "Using regular expressions" (developerWorks, September 2000). Greg Travis's "Getting started with NIO" (developerWorks, July 2003) is a good, hands-on introduction to the Java platform's new I/O. In "Charming Jython" (developerWorks, May 2003) regular developerWorks contributor Uche Ogbuji offers a short introduction to Jython. Try your hand at using Jython to build a read-eval-print-loop, with Eric Allen's "Repls provide interactive evaluation" (developerWorks, March 2002). Charming Python is regular developerWorks column devoted to programming with Python. Jeffrey Friedl's Mastering Regular Expressions, Second Edition (O'Reilly, July 2002) is a comprehensive introduction to regular expressions. For a solid introduction to Jython, see Samuele Pedroni and Noel Rappin's Jython Essentials (O'Reilly, March 2002). Jython for Java Programmers focuses on application development, deployment, and optimization with Jython (Robert W. Bill, New Riders, December 2001). Python Programming with the Java Class Libraries is a good introduction to building Web and enterprise applications with Jython (Richard Hightower, Addison Wesley, 2003). You'll find articles about every aspect of Java programming in the developerWorks Java technology zone. Visit the Developer Bookstore for a comprehensive listing of technical books, including hundreds of Java-related titles . Also see the Java technology zone tutorials page for a complete listing of free Java-focused tutorials from developerWorks.
Feedback
Page 74 of 100 Introduction to Jython, Part 2: Programming essentials
ibm.com/developerWorks
Page 75 of 100
ibm.com/developerWorks
Implements - operator
ibm.com/developerWorks
__neg__(self) x*y x *= y __mul__(self, other) __rmul__(self, other) __imul__(self, other) x/y x /= y __div__(self, other) __rdiv__(self, other) __idiv__(self, other) x%y x %= y __mod__(self, other) __rmod__(self, other) __imod__(self, other) x&y x &= y __and__(self, other) __rand__(self, other) __iand__(self, other) x|y x |= y __or__(self, other) __ror__(self, other) __ior__(self, other) x^y x ^= y __xor__(self, other) __rxor__(self, other) __ixor__(self, other) ~x x << y x <<= y __invert__(self) __lshift__(self, other) __rlshift__(self, other) __ilshift__(self, other) x >> y x >>= y __rshift__(self, other) __ rrshift__(self, other) __ irshift__(self, other) x ** y x **= y __pow__(self, other) __rpow__(self, other) __ipow__(self, other) divmod(x,y) __divmod__(self, other) __rdivmod__(self, other)
Introduction to Jython, Part 2: Programming essentials Page 77 of 100
Implements * operator
Implements / operator
Implements % operator
Implements | operator
Implements ^ operator
Implements ** operator
Implements divmod()
ibm.com/developerWorks
x<y
__lt__(self, other)
Implements < operator. This should return the opposite value returned by __ge__. Implements <= operator. This should return the opposite value returned by __gt__. Implements > operator. This should return the opposite value returned by __le__. Implements >= operator. This should return the opposite value returned by __lt__. Implements == operator. This should return the opposite value returned by __ne__. Implements != operator. This should return the opposite value returned by __eq__. Implements cmp(); also used for relational tests if above specific overrides are not defined. This should return a < 0, 0 or > 0 value (say -1, 0 or 1). Implements logical tests. This should return 0 or 1. Implements hash(). Returns an integer value. Instances that are equal (that is, __eq__ returns true) should return the same __hash__ value (that is, (x == y) and (hash(x) == hash(y)) should be true. Similar to java.lang.Object.hashCode(). Implements abs() Implements int() Implements long() Implements float() Implements complex() Implements oct() Implements hex() Implements coerce()
Introduction to Jython, Part 2: Programming essentials
x <= y
__le__(self, other)
x>y
__gt__(self, other)
x >= y
__ge__(self, other)
x == y
__eq__(self, other)
x != y x <> y cmp(x,y)
__ne__(self, other)
__cmp__(self, other)
x hash(x)
__nonzero__(self) __hash__(self)
ibm.com/developerWorks
y = x.name x.name = y del x.name y = c[i] c[i] = y del c[i] x(arg, ...) len(c) x in c x not in c class() del x repr(x) -- or -`x` str(x)
__getattr__ (self, name) __setattr__ (self, name) __delattr__ (self, name) __getitem_ (self, i) __setitem__ (self, i) __delitem__ (self, i) __call__ (self, arg, ...) __len__ (self) __contains__ (self, other)
Implements attribute lookup Implements attribute creation/update Implements attribute removal Implements item lookup Implements item creation/update Implements item removal Implements the call operator Implements len() Implements in operator
Instance constructor; called when the class is created Instance destructor; called just before being deallocated Implements repr() on this class
__str__(self)
Implements str() on this class; Jython uses __repr__ if __str__ is not defined. str() is used like x.toString() in Java
Note: For the binary operators, the __xxx__ form is used when the left (or both) argument implements the function; the __rxxx__ form is used only if the right argument implements the function and the left argument does not; the __ixxx__ form is used to implement the augmented assignment (x ?= y) operation. See the Python Reference Manual for more details and overload-able functions.
Page 79 of 100
ibm.com/developerWorks
d, down u, up b, break
Move down one stack frame Move up one stack frame Set a breakpoint at a line number or function with an optional expression to evaluate - stop only if true Set a breakpoint at a line number or function with an optional expression to evaluate - stop only if true; the breakpoint is automatically cleared when hit Clears all or listed breakpoints Enables breakpoints Disabled breakpoints Sets the breakpoint ignore (auto-resume) count Sets the breakpoint condition expression Steps over the next line, possibly into a function Resume until the next line is reached Resume until the current function returns Resume execution Set a new current line Lists source from line#1 to line#2, if omitted, then list the lines around the current line Show the arguments of the current function Evaluate the expression and print its result; pp formats the result Do the print statement, that is, - !print expr Create a named expression to simplify printing of repeated values Delete an alias End the debugging session
Introduction to Jython, Part 2: Programming essentials
tbreak
cl, clear enable disable ignore condition s, step n, next r, return c, cont, continue j, jump l, list
bpid... bpid... bpid... bpid, count bpid, condition_expr -- none --- none --- none --- none -line# line#1, line#1
ibm.com/developerWorks
statement
java.lang.String, byte[], char[] String java.lang.Class Foobar[] java.lang.Object org.python.core.PyObject Foobar Class or JavaClass Array (must contain objects of class or subclass of Foobar) String->java.lang.String, all others unchanged All unchanged Instance --> Foobar (if Instance is subclass of Foobar); JavaInstance --> Foobar (if JavaInstance is instance of Foobar or subclass)
Jython uses these rules to map return value types: Java Return Type char Boolean byte, short, int, long float, double java.lang.String java.lang.Class Returned Python Type String (of length 1) Integer (true = 1, false = 0) Integer Float String JavaClass which represents given Java class
Page 81 of 100
ibm.com/developerWorks
Foobar[]
org.python.core.PyObject (or Unchanged subclass) Foobar JavaInstance which represents the Java Class Foobar
Note: the above two tables are from the www.jython.org site.
maxint minint platform path stdin stdout stderr modules version version_info
The version of Java Jython is running on The module search path Standard input, output and error streams
The sys module has some important functions: Function exit(int) exc_info() Comment(s) Exits the program Get information on the most recent exception
Page 82 of 100
ibm.com/developerWorks
The sys module has some important functions: Function getcwd() mkdir(path) makedirs(path) rmdir(path) remove(path) -- or -unlink(path) listdir(path) rename(path, new) system(command) List the files in a path Renames a file/directory to new Run a shell command Delete a file Comment(s) Get the current directory Create/delete a directory
Page 83 of 100
ibm.com/developerWorks
ibm.com/developerWorks
below . ^ $ ? ?? + +? * *? {m,n} {m,n}? [...] [^...] ...|... (...) (?...) (?P<name>...) (?P=name) (?=...) (?!...) \c Matches any character Matches the start of the string Matches the end of the string Matches longest 0 or 1 of the proceeding Matches shortest 0 or 1 of the proceeding Matches longest 1 or more of the proceeding Matches shortest 1 or more of the proceeding Matches longest 0 or more of the proceeding Matches shortest 0 or more of the proceeding Matches longest m to n of the proceeding Matches shortest m to n of the proceeding Defines the set of enclosed characters Defines the set of non-enclosed characters Matches a choice (or) Matches the sequence (or group) ...; groups are ordered from left to right with origin 1 Matches a sequence but does not define a group Matches a sequence (or group) ... giving it a name Matches the sequence defined with the name Matches ... but does not consume the test Matches not ... but does not consume the test Special characters: \c literal escapes: .?*+&^$|()[] \c function escapes: see below See the Python Library Reference for more information. Function escapes: Function Escapes \A Comment(s) Matches at start of line
Page 85 of 100
ibm.com/developerWorks
\Z \B \b \D \d \S \s \W \w \#
Matches at end of line Matches not at beginning or end of a word Matches at beginning or end of a word Matches not any decimal digit (0..9) Matches any decimal digit (0..9) Matches not any white space Matches any white space Matches not any alpha-numeric characters Matches any alpha-numeric characters Matches group #
Several options exist to modify how regular expression are processed. Options are bit flags and may be combined by OR-ing (|) them together. Some of the more useful options are: Option IGNORECASE -- or -I MULTILINE -- or -M DOTALL -- or -S Causes '.' to match a newline Causes '^' and '$' to match internal line boundaries (vs. just the start and end of the string) Comment(s) Match ignoring case
import org.python.core.*;
Page 86 of 100
ibm.com/developerWorks
public class factor extends java.lang.Object { static String[] jpy$mainProperties = new String[] {"python.modules.builtin", "exceptions:org.python.core.exceptions"}; static String[] jpy$proxyProperties = new String[] {"python.modules.builtin", "exceptions:org.python.core.exceptions", "python.options.showJavaExceptions", "true"}; static String[] jpy$packages = new String[] {}; public static class _PyInner extends PyFunctionTable implements PyRunnable { private static PyObject i$0; private static PyObject i$1; private static PyObject s$2; private static PyObject l$3; private static PyObject i$4; private static PyObject s$5; private static PyObject s$6; private static PyObject s$7; private static PyObject s$8; private static PyObject s$9; private static PyObject i$10; private static PyObject i$11; private static PyObject s$12; private static PyFunctionTable funcTable; private static PyCode c$0___init__; private static PyCode c$1_addListener; private static PyCode c$2_addListeners; private static PyCode c$3_removeListener; private static PyCode c$4_removeListeners; private static PyCode c$5_fireListeners; private static PyCode c$6_cancel; private static PyCode c$7_calculate; private static PyCode c$8_Factorial; private static PyCode c$9_doFac; private static PyCode c$10_main; private static void initConstants() { i$0 = Py.newInteger(0); i$1 = Py.newInteger(1); s$2 = Py.newString("only positive integers supported: "); l$3 = Py.newLong("1"); i$4 = Py.newInteger(100); s$5 = Py.newString("__main__"); s$6 = Py.newString("running..."); s$7 = Py.newString("For"); s$8 = Py.newString("result ="); s$9 = Py.newString("Exception -"); i$10 = Py.newInteger(10); i$11 = Py.newInteger(1000); s$12 = Py.newString("C:\\Articles\\factor.py"); funcTable = new _PyInner(); c$0___init__ = Py.newCode(1, new String[] {"self"}, "C:\\Articles\\factor.py", "__init__", false, false,
Page 87 of 100
ibm.com/developerWorks
funcTable, 0, null, null, 0, 1); c$1_addListener = Py.newCode(2, new String[] {"self", "listener", "ll"}, "C:\\Articles\\factor.py", "addListener", false, false, funcTable, 1, null, null, 0, 1); c$2_addListeners = Py.newCode(2, new String[] {"self", "listeners", "l"}, "C:\\Articles\\factor.py", "addListeners", false, false, funcTable, 2, null, null, 0, 1); c$3_removeListener = Py.newCode(2, new String[] {"self", "listener", "ll"}, "C:\\Articles\\factor.py", "removeListener", false, false, funcTable, 3, null, null, 0, 1); c$4_removeListeners = Py.newCode(2, new String[] {"self", "listeners", "l"}, "C:\\Articles\\factor.py", "removeListeners", false, false, funcTable, 4, null, null, 0, 1); c$5_fireListeners = Py.newCode(2, new String[] {"self", "value", "func"}, "C:\\Articles\\factor.py", "fireListeners", false, false, funcTable, 5, null, null, 0, 1); c$6_cancel = Py.newCode(1, new String[] {"self"}, "C:\\Articles\\factor.py", "cancel", false, false, funcTable, 6, null, null, 0, 1); c$7_calculate = Py.newCode(2, new String[] {"self", "value", "next", "x", "last", "result"}, "C:\\Articles\\factor.py", "calculate", false, false, funcTable, 7, null, null, 0, 1); c$8_Factorial = Py.newCode(0, new String[] {}, "C:\\Articles\\factor.py", "Factorial", false, false, funcTable, 8,
Page 88 of 100
ibm.com/developerWorks
null, null, 0, 0); c$9_doFac = Py.newCode(1, new String[] {"value", "e"}, "C:\\Articles\\factor.py", "doFac", false, false, funcTable, 9, null, null, 0, 1); c$10_main = Py.newCode(0, new String[] {}, "C:\\Articles\\factor.py", "main", false, false, funcTable, 10, null, null, 0, 0); } public PyCode getMain() { if (c$10_main == null) _PyInner.initConstants(); return c$10_main; } public PyObject call_function(int index, PyFrame frame) { switch (index){ case 0: return _PyInner.__init__$1(frame); case 1: return _PyInner.addListener$2(frame); case 2: return _PyInner.addListeners$3(frame); case 3: return _PyInner.removeListener$4(frame); case 4: return _PyInner.removeListeners$5(frame); case 5: return _PyInner.fireListeners$6(frame); case 6: return _PyInner.cancel$7(frame); case 7: return _PyInner.calculate$8(frame); case 8: return _PyInner.Factorial$9(frame); case 9: return _PyInner.doFac$10(frame); case 10: return _PyInner.main$11(frame); default: return null; } } private static PyObject __init__$1(PyFrame frame) { frame.getlocal(0).__setattr__("_Factorial__listeners", new PyList(new PyObject[] {})); frame.getlocal(0).__setattr__("_Factorial__cancelled", i$0); return Py.None; }
Page 89 of 100
ibm.com/developerWorks
private static PyObject addListener$2(PyFrame frame) { frame.setlocal(2, frame.getlocal(0).__getattr__("_Factorial__listeners")); if (frame.getlocal(1)._notin( frame.getlocal(2) ).__nonzero__()) { frame.getlocal(2).invoke("append", frame.getlocal(1)); } return Py.None; } private static PyObject addListeners$3(PyFrame frame) { // Temporary Variables int t$0$int; PyObject t$0$PyObject, t$1$PyObject; // Code t$0$int = 0; t$1$PyObject = frame.getlocal(1); while ((t$0$PyObject = t$1$PyObject.__finditem__(t$0$int++)) != null) { frame.setlocal(2, t$0$PyObject); frame.getlocal(0).invoke("addListener", frame.getlocal(2)); } return Py.None; } private static PyObject removeListener$4(PyFrame frame) { frame.setlocal(2, frame.getlocal(0).__getattr__("_Factorial__listeners")); frame.getlocal(2).invoke("remove", frame.getlocal(1)); return Py.None; } private static PyObject removeListeners$5(PyFrame frame) { // Temporary Variables int t$0$int; PyObject t$0$PyObject, t$1$PyObject; // Code t$0$int = 0; t$1$PyObject = frame.getlocal(1); while ((t$0$PyObject = t$1$PyObject.__finditem__(t$0$int++)) != null) { frame.setlocal(2, t$0$PyObject); frame.getlocal(0).invoke("removeListener", frame.getlocal(2)); } return Py.None; } private static PyObject fireListeners$6(PyFrame frame) { // Temporary Variables int t$0$int; PyObject t$0$PyObject, t$1$PyObject; // Code t$0$int = 0;
Page 90 of 100
ibm.com/developerWorks
t$1$PyObject = frame.getlocal(0).__getattr__("_Factorial__listeners"); while ((t$0$PyObject = t$1$PyObject.__finditem__(t$0$int++)) != null) { frame.setlocal(2, t$0$PyObject); frame.getlocal(2).__call__(frame.getlocal(1)); } return Py.None; } private static PyObject cancel$7(PyFrame frame) { frame.getlocal(0).__setattr__("_Factorial__cancelled", i$1); return Py.None; } private static PyObject calculate$8(PyFrame frame) { // Temporary Variables int t$0$int; PyObject t$0$PyObject, t$1$PyObject; // Code if (((t$0$PyObject = frame.getglobal("type"). __call__(frame.getlocal(1)). _ne(frame.getglobal("types"). __getattr__("IntType"))).__nonzero__() ? t$0$PyObject : frame.getlocal(1)._lt(i$0)).__nonzero__()) { throw Py.makeException( frame.getglobal("ValueError"), s$2._add(frame.getglobal("str"). __call__(frame.getlocal(1)))); } frame.getlocal(0).__setattr__("_Factorial__cancelled", i$0); frame.setlocal(5, l$3); frame.getlocal(0).invoke("fireListeners", i$0); if (frame.getlocal(1)._le(i$1).__nonzero__()) { frame.setlocal(5, l$3); } else { frame.setlocal(4, i$0); t$0$int = 0; t$1$PyObject = frame.getglobal("range"). __call__(i$1,frame.getlocal(1)._add(i$1)); while ((t$0$PyObject = t$1$PyObject. __finditem__(t$0$int++)) != null) { frame.setlocal(3, t$0$PyObject); if (frame.getlocal(0). __getattr__("_Factorial__cancelled").__nonzero__()) { break; } frame.setlocal(5, frame.getlocal(5)._mul(frame.getlocal(3))); frame.setlocal(2, frame.getlocal(3)._mul(i$4)._div(frame.getlocal(1))); if (frame.getlocal(2)._ne(frame.getlocal(4)).__nonzero__()) { frame.getlocal(0).invoke("fireListeners", frame.getlocal(2));
Page 91 of 100
ibm.com/developerWorks
frame.setlocal(4, frame.getlocal(2)); } } } frame.getlocal(0).invoke("fireListeners", i$4); if (frame.getlocal(0). __getattr__("_Factorial__cancelled").__nonzero__()) { frame.setlocal(5, i$1.__neg__()); } return frame.getlocal(5); } private static PyObject Factorial$9(PyFrame frame) { frame.setlocal("__init__", new PyFunction(frame.f_globals, new PyObject[] {}, c$0___init__)); frame.setlocal("addListener", new PyFunction(frame.f_globals, new PyObject[] {}, c$1_addListener)); frame.setlocal("addListeners", new PyFunction(frame.f_globals, new PyObject[] {}, c$2_addListeners)); frame.setlocal("removeListener", new PyFunction(frame.f_globals, new PyObject[] {}, c$3_removeListener)); frame.setlocal("removeListeners", new PyFunction(frame.f_globals, new PyObject[] {}, c$4_removeListeners)); frame.setlocal("fireListeners", new PyFunction(frame.f_globals, new PyObject[] {}, c$5_fireListeners)); frame.setlocal("cancel", new PyFunction(frame.f_globals, new PyObject[] {}, c$6_cancel)); frame.setlocal("calculate", new PyFunction(frame.f_globals, new PyObject[] {}, c$7_calculate)); return frame.getf_locals(); } private static PyObject doFac$10(PyFrame frame) { // Temporary Variables PyException t$0$PyException; // Code try { Py.printComma(s$7); Py.printComma(frame.getlocal(0)); Py.printComma(s$8); Py.println(frame.getglobal("fac"). invoke("calculate", frame.getlocal(0))); } catch (Throwable x$0) { t$0$PyException = Py.setException(x$0, frame); if (Py.matchException(t$0$PyException, frame.getglobal("ValueError"))) { frame.setlocal(1, t$0$PyException.value); Py.printComma(s$9);
Page 92 of 100
ibm.com/developerWorks
Py.println(frame.getlocal(1)); } else throw t$0$PyException; } return Py.None; } private static PyObject main$11(PyFrame frame) { frame.setglobal("__file__", s$12); frame.setlocal("sys", org.python.core.imp.importOne("sys", frame)); frame.setlocal("types", org.python.core.imp.importOne("types", frame)); frame.setlocal("exceptions", org.python.core.imp.importOne("exceptions", frame)); frame.setlocal("Factorial", Py.makeClass("Factorial", new PyObject[] {}, c$8_Factorial, null)); if (frame.getname("__name__")._eq(s$5).__nonzero__()) { Py.printComma(frame.getname("sys"). __getattr__("argv").__getitem__(i$0)); Py.println(s$6); frame.setlocal("fac", frame.getname("Factorial").__call__()); frame.setlocal("doFac", new PyFunction(frame.f_globals, new PyObject[] {}, c$9_doFac)); frame.getname("doFac").__call__(i$1.__neg__()); frame.getname("doFac").__call__(i$0); frame.getname("doFac").__call__(i$1); frame.getname("doFac").__call__(i$10); frame.getname("doFac").__call__(i$4); frame.getname("doFac").__call__(i$11); } return Py.None; } } public static void moduleDictInit(PyObject dict) { dict.__setitem__("__name__", new PyString("factor")); Py.runCode(new _PyInner().getMain(), dict, dict); } public static void main(String[] args) throws java.lang.Exception { String[] newargs = new String[args.length+1]; newargs[0] = "factor"; System.arraycopy(args, 0, newargs, 1, args.length); Py.runMain(factor._PyInner.class, newargs, factor.jpy$packages, factor.jpy$mainProperties, null, new String[] {"factor"}); } }
Page 93 of 100
ibm.com/developerWorks
Note: The above code has been reformatted for line length.
%{(key)}{flag}...{width}{.precision}x
Here's a guide to the format items: key: Optional key to lookup in a supplied dictionary flag: Optional flags (reasonable combinations supported) #: Display any special format prefix (for example, "0" for octal, "0x" for hex) +: Display a "+" on positive numbers blank: Display a leading space on positive numbers -: Left (vs. right) justify the value in the width 0: Left pad with "0" (vs. spaces) width: Minimum width of the field (will be longer for large values) precision: Number of digits after any decimal point x: Format code as described below
The format operator supports the following format characters: Character(s) Result Format Comment(s)
Page 94 of 100
ibm.com/developerWorks
String Integer Decimal Unsigned Value Floating Decimal Exponential Character Character
%s does str(x), %r does repr(x) Basically the same format In octal, unsigned decimal, hexadecimal Shows fraction after decimal point %g is %f unless the value is small; else %e Must be a single character or integer The % character
Note: more details on the structure and options of the format item can be found in the Python Library Reference (Resources on page 73 ). Use of case in format characters (for example, X vs x causes the symbol to show in matching case. For example
print "%s is %i %s %s than %s!" % ("John", 5, "years", "older", "Mark") print "Name: %(last)s, %(first)s" % \ {'first':"Barry", 'last':"Feigenbaum", 'age':18}
prints
Page 95 of 100
ibm.com/developerWorks
func(*pargs {, **kargs}) callable(x) Tests to see if the object is callable(MyClass) --> 1 callable (i.e, is a function, class or implements __call__) Converts the integer (0 65535) to a 1-character string Compares x to y: returns: negative if x < y; 0 if x == y; positive if x > y Returns the tuple of x and y coerced to a common type chr(9) --> "\t" cmp("Hello", "Goodbye") --> > 0
chr(x) cmp(x, y)
Compile the text string from the x = 2 source name. Kind is: "exec", c = compile("x * 2", "eval" or "single" "<string>", "eval")
eval(c) --> 4
complex(r, i)
dir({namespace})
dir() --> [n1, ..., nN] vars() --> {n1:v1, ..., nN:vN} divmod(100, 33) --> (3, 1)
myvalues = {'x':1, 'y':2} eval("x + y", myvalues) --> 3
vars({namespace}) Returns the namespace (local if omitted); do not change it divmod(x, y) Returns the tuple (x /y, x % y)
eval(expr {, globals Evaluate the expression in the {, locals}}) supplied namespaces execfile(name {,globals {, locals}}) filter(func, list) float(x) Read and execute the named file in the supplied namespaces
execfile("myfile.py")
Creates a list of items for which filter(lambda x: x > 0, [-1, 0, 1, -5, 10]) func returns true --> [1, 10] Converts x to a float float(10) --> 10.0 float("10.3") --> 10.3
Gets the value of the object's attribute; if not defined return default (or an exception if no default) Creates/sets the value of the
setattr(object,
Page 96 of 100
ibm.com/developerWorks
object's attribute Test to see if the object has an hasattr(myObj, "size") --> 0 attribute Returns the current global namespace dictionary Returns the current local namespace dictionary {n1:v1, ..., nN:vN} {n1:v1, ..., nN:vN}
Returns the object's hash hash(x) --> 10030939 value. Similar to java.lang.Object.hashCode() Returns a hex string of x hex(-2) --> "FFFFFFFE"
Returns a unique stable integer id(myObj) --> 39839888 id for the object Prompts and evaluates the input("Enter expression:") supplied input expression; with "1 + 2" --> 3 equivalent to eval(raw_input(prompt))
raw_input(prompt) Prompts for and inputs a string raw_input("Enter value:") with "1 + 2" --> "1 + 2" int(x{, radix}) Converts to an integer; radix: 0, 2..36; 0 implies guess int(10.2) --> 10 int("10") --> 10 int("1ff", 16) --> 511 isinstance(object, class) Tests to see if object is an isinstance(myObj, MyObject) --> 0 instance of class or a subclass of class; class may be a tuple isinstance(x, (Class1, Class2)) --> 1 of classes to test multiple types Tests to see if xclass is a issubclass(MyObject, (Class1, Class2)) sub-(or same) class of class; --> 0 class may be a tuple of classes to test multiple types Returns the length (number of items) in the sequence or map Converts the sequence into a list Converts the sequence into a tuple Converts to a long integer; radix: 0, 2..36; 0 implies guess len("Hello") --> 5 list((1, 2, 3)) --> [1,2,3] list("Hello") --> ['H','e','l','l','o'] tuple((1, 2, 3)) --> (1,2,3) tuple("Hello")--> ('H','e','l','l','o') long(10) --> 10L
issubclass(xclass, clsss)
len(x) list(seq)
Page 97 of 100
ibm.com/developerWorks
long("10000000000") --> 10000000000L map(func, list, ...) Creates a new list from the map(lambda x,y: x+y, [1,2],[3,4]) --> results of applying func to each [4,6] element of each list map(None, [1,2],[3,4]) --> [[1,3],[2,4]] Returns the maximum value max(1,2,3) --> 3 max([1,2,3]) --> 3 min(x) Returns the minimum value min(1,2,3) --> 1 min([1,2,3]) --> 1 oct(x) Converts to an octal string oct(10) --> "012 oct(-1) --> "037777777777" open(name, mode Returns an open file. Mode {, bufsize}) is:(r|w|a){+}{b} ord(x) pow(x,y) pow(x,y,z) range({start,} stop {, inc}) Returns the integer value of the character Computes x ** y Computes x ** y % z range(10) --> [0,1,2,3,4,5,6,7,8,9] range(9,-1,-1) --> [9,8,7,6,5,4,3,2,1,0] open("useful.dat", "wb", 2048) ord('\t') --> 9 pow(2,3) --> 8
max(x)
Returns a sequence ranging from start to stop in steps of inc; start defaults to 0; inc xrange({start,} stop defaults to 1. Use xrange for {, inc}) large sequences (say more than 20 items) reduce(func, list {, init}) Applies func to each pair of items in turn accumulating a result
reduce(lambda x,y:x+y, [1,2,3,4],5) --> 15 reduce(lambda x,y:x&y, [1,0,1]) --> 0 reduce(None, [], 1) --> 1
repr(10 * 2) --> "'20'" repr('xxx') --> "'xxx'" x = 10; `x` --> "10'"
str(object)
Page 98 of 100
ibm.com/developerWorks
type(object)
Returns the type (not the same x = "1"; type(x) is type('') --> 1 as class) of the object. To get the class use object.__class__. Module types has symbolic names for all Jython types Zips sequences together; results is only as long as the shortest input sequence zip([1,2,3],"abc") --> [(1,'a'),(2,'b'),(3,'c')]
zip(seq, ...)
Page 99 of 100
ibm.com/developerWorks
expression object ListType LongType MethodType ModuleType NoneType StringType TracebackType TupleType TypeType UnboundMethodType UnicodeType XRangeType PyList PyLong PyMethod PyModule PyNone PyString PyTraceback PyTuple PyJavaClass PyMethod PyString PyXRange Any list object Any long object Any non-built-in method object Any module object Any None (only one) object Any ASCII string object Any exception traceback object Any tuple object Any type object Any method (without a bound instancee) object Any Unicode string object Any extended range object
Note: several types map to the same Java runtime type. For more information on types see the Python Library Reference (Resources on page 73 ).
Colophon
This tutorial was written entirely in XML, using the developerWorks Toot-O-Matic tutorial generator. The open source Toot-O-Matic tool is an XSLT stylesheet and several XSLT extension functions that convert an XML file into a number of HTML pages, a zip file, JPEG heading graphics, and two PDF files. Our ability to generate multiple text and binary formats from a single source file illustrates the power and flexibility of XML. (It also saves our production team a great deal of time and effort.) You can get the source code for the Toot-O-Matic at www6.software.ibm.com/dl/devworks/dw-tootomatic-p. The tutorial Building tutorials with the Toot-O-Matic demonstrates how to use the Toot-O-Matic to create your own tutorials. developerWorks also hosts a forum devoted to the Toot-O-Matic; it's available at www-105.ibm.com/developerworks/xml_df.nsf/AllViewTemplate?OpenForm&RestrictToCategory=11. We'd love to know what you think about the tool.