carfield.com.hk GreenHouseController.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c09:GreenHouseController.java import org.python.util.PythonInterpreter; import org.python.core.*; import com.bruceeckel.test.*; public class GreenHouseController extends UnitTest { PythonInterpreter interp = new PythonInterpreter(); public void test() throws PyException { System.out.println( &quot;Loading GreenHouse Language&quot;); interp.execfile(&quot;GreenHouseLanguage.py&quot;); System.out.println( &quot;Loading GreenHouse Script&quot;); interp.execfile(&quot;Schedule.ghs&quot;); System.out.println( &quot;Executing GreenHouse Script&quot;); interp.exec(&quot;run()&quot;); } public static void main(String[] args) throws PyException { new GreenHouseController().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z GreenHouseLanguage.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#:c09:GreenHouseLanguage.py class Event: events = [] # static def __init__(self, action, time): self.action = action self.time = time Event.events.append(self) # Used by sort(). This will cause # comparisons to be based only on time: def __cmp__ (self, other): if self.time &lt; other.time: return -1 if self.time &gt; other.time: return 1 return 0 def run(self): print &quot;%.2f: %s&quot; % (self.time, self.action) class LightOn(Event): def __init__(self, time): Event.__init__(self, &quot;Light on&quot;, time) class LightOff(Event): def __init__(self, time): Event.__init__(self, &quot;Light off&quot;, time) class WaterOn(Event): def __init__(self, time): Event.__init__(self, &quot;Water on&quot;, time) class WaterOff(Event): def __init__(self, time): Event.__init__(self, &quot;Water off&quot;, time) class ThermostatNight(Event): def __init__(self, time): Event.__init__(self,&quot;Thermostat night&quot;, time) class ThermostatDay(Event): def __init__(self, time): Event.__init__(self, &quot;Thermostat day&quot;, time) class Bell(Event): def __init__(self, time): Event.__init__(self, &quot;Ring bell&quot;, time) def run(): Event.events.sort(); for e in Event.events: e.run() # To test, this will be run when you say: # python GreenHouseLanguage.py if __name__ == &quot;__main__&quot;: ThermostatNight(5.00) LightOff(2.00) WaterOn(3.30) WaterOff(4.45) LightOn(1.00) ThermostatDay(6.00) Bell(7.00) run() #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z JavaClass.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c09:javaclass:JavaClass.java package c09.javaclass; import com.bruceeckel.test.*; import com.bruceeckel.util.*; public class JavaClass { private String s = &quot;&quot;; public JavaClass() { System.out.println(&quot;JavaClass()&quot;); } public JavaClass(String a) { s = a; System.out.println(&quot;JavaClass(String)&quot;); } public String getVal() { System.out.println(&quot;getVal()&quot;); return s; } public void setVal(String a) { System.out.println(&quot;setVal()&quot;); s = a; } public Character[] getChars() { System.out.println(&quot;getChars()&quot;); Character[] r = new Character[s.length()]; for(int i = 0; i &lt; s.length(); i++) r[i] = new Character(s.charAt(i)); return r; } public static class Test extends UnitTest { JavaClass x1 = new JavaClass(), x2 = new JavaClass(&quot;UnitTest&quot;); public void test1() { System.out.println(x2.getVal()); x1.setVal(&quot;SpamEggsSausageAndSpam&quot;); Arrays2.print(x1.getChars()); } } public static void main(String[] args) { Test test = new Test(); test.test1(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z JavaClassInPython.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:JavaClassInPython.py #=M jython.bat JavaClassInPython.py # Using Java classes within Jython from java.util import Date, HashSet, HashMap from c09.javaclass import JavaClass from math import sin d = Date() # Creating a Java Date object print d # Calls toString() # A &quot;generator&quot; to easily create data: class ValGen: def __init__(self, maxVal): self.val = range(maxVal) # Called during 'for' iteration: def __getitem__(self, i): # Returns a tuple of two elements: return self.val[i], sin(self.val[i]) # Java standard containers: map = HashMap() set = HashSet() for x, y in ValGen(10): map.put(x, y) set.add(y) set.add(y) print map print set # Iterating through a set: for z in set: print z, z.__class__ print map[3] # Uses Python dictionary indexing for x in map.keySet(): # keySet() is a Map method print x, map[x] # Using a Java class that you create yourself is # just as easy: jc = JavaClass() jc2 = JavaClass(&quot;Created within Jython&quot;) print jc2.getVal() jc.setVal(&quot;Using a Java class is trivial&quot;) print jc.getVal() print jc.getChars() jc.val = &quot;Using bean properties&quot; print jc.val #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z MultipleJythons.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c09:MultipleJythons.java // You can run multiple interpreters, each // with its own name space. import org.python.util.PythonInterpreter; import org.python.core.*; import com.bruceeckel.test.*; public class MultipleJythons extends UnitTest { PythonInterpreter interp1 = new PythonInterpreter(), interp2 = new PythonInterpreter(); public void test() throws PyException { interp1.set(&quot;a&quot;, new PyInteger(42)); interp2.set(&quot;a&quot;, new PyInteger(47)); interp1.exec(&quot;print a&quot;); interp2.exec(&quot;print a&quot;); PyObject x1 = interp1.get(&quot;a&quot;); PyObject x2 = interp2.get(&quot;a&quot;); System.out.println(&quot;a from interp1: &quot; + x1); System.out.println(&quot;a from interp2: &quot; + x2); } public static void main(String[] args) throws PyException { new MultipleJythons().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z PythonDialogs.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:PythonDialogs.py # Dialogs.java from &quot;Thinking in Java, 2nd # edition,&quot; Chapter 13, converted into Jython. # Don't run this as part of the automatic make: #=M @echo skipping PythonDialogs.py from java.awt import FlowLayout from javax.swing import JFrame, JDialog, JLabel from javax.swing import JButton class MyDialog(JDialog): def __init__(self, parent=None): JDialog.__init__(self, title=&quot;My dialog&quot;, modal=1) self.contentPane.layout = FlowLayout() self.contentPane.add(JLabel(&quot;A dialog!&quot;)) self.contentPane.add(JButton(&quot;OK&quot;, actionPerformed = lambda e, t=self: t.dispose())) self.pack() frame = JFrame(&quot;Dialogs&quot;, visible=1, defaultCloseOperation=JFrame.EXIT_ON_CLOSE) dlg = MyDialog() frame.contentPane.add( JButton(&quot;Press here to get a Dialog Box&quot;, actionPerformed = lambda e: dlg.show())) frame.pack() #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z PythonInterpreterGetting.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c09:PythonInterpreterGetting.java // Getting data from the PythonInterpreter object. import org.python.util.PythonInterpreter; import org.python.core.*; import java.util.*; import com.bruceeckel.python.*; import com.bruceeckel.test.*; public class PythonInterpreterGetting extends UnitTest{ PythonInterpreter interp = new PythonInterpreter(); public void test() throws PyException { interp.exec(&quot;a = 100&quot;); // If you just use the ordinary get(), // it returns a PyObject: PyObject a = interp.get(&quot;a&quot;); // There's not much you can do with a generic // PyObject, but you can print it out: System.out.println(&quot;a = &quot; + a); // If you know the type it's supposed to be, // you can &quot;cast&quot; it using __tojava__() to // that Java type and manipulate it in Java. // To use 'a' as an int, you must use // the Integer wrapper class: int ai= ((Integer)a.__tojava__(Integer.class)) .intValue(); // There are also convenience functions: ai = Py.py2int(a); System.out.println(&quot;ai + 47 = &quot; + (ai + 47)); // You can convert it to different types: float af = Py.py2float(a); System.out.println(&quot;af + 47 = &quot; + (af + 47)); // If you try to cast it to an inappropriate // type you'll get a runtime exception: //! String as = (String)a.__tojava__( //! String.class); // If you know the type, a more useful method // is the overloaded get() that takes the // desired class as the 2nd argument: interp.exec(&quot;x = 1 + 2&quot;); int x = ((Integer)interp .get(&quot;x&quot;, Integer.class)).intValue(); System.out.println(&quot;x = &quot; + x); // Since Python is so good at manipulating // strings and files, you will often need to // extract an array of Strings. Here, a file // is read as a Python array: interp.exec(&quot;lines = &quot; + &quot;open('PythonInterpreterGetting.java')&quot; + &quot;.readlines()&quot;); // Pull it in as a Java array of String: String[] lines = (String[]) interp.get(&quot;lines&quot;, String[].class); for(int i = 0; i &lt; 10; i++) System.out.print(lines[i]); // As an example of useful string tools, // global expansion of ambiguous file names // using glob is very useful, but it's not // part of the standard Jython package, so // you'll have to make sure that your // Python path is set to include these, or // that you deliver the necessary Python // files with your application. interp.exec(&quot;from glob import glob&quot;); interp.exec(&quot;files = glob('*.java')&quot;); String[] files = (String[]) interp.get(&quot;files&quot;, String[].class); for(int i = 0; i &lt; files.length; i++) System.out.println(files[i]); // You can extract tuples and arrays into // Java Lists with com.bruceeckel.PyUtil: interp.exec( &quot;tup = ('fee', 'fi', 'fo', 'fum', 'fi')&quot;); List tup = PyUtil.toList(interp, &quot;tup&quot;); System.out.println(tup); // It really is a list of String objects: System.out.println(tup.get(0).getClass()); // You can easily convert it to a Set: Set tups = new HashSet(tup); System.out.println(tups); interp.exec(&quot;ints=[1,3,5,7,9,11,13,17,19]&quot;); List ints = PyUtil.toList(interp, &quot;ints&quot;); System.out.println(ints); // It really is a List of Integer objects: System.out.println((ints.get(1)).getClass()); // If you have a Python dictionary, it can // be extracted into a Java Map, again with // com.bruceeckel.PyUtil: interp.exec(&quot;dict = { 1 : 'a', 3 : 'b',&quot; + &quot;5 : 'c', 9 : 'd', 11 : 'e' }&quot;); Map map = PyUtil.toMap(interp, &quot;dict&quot;); System.out.println(&quot;map: &quot; + map); // It really is Java objects, not PyObjects: Iterator it = map.entrySet().iterator(); Map.Entry e = (Map.Entry)it.next(); System.out.println(e.getKey().getClass()); System.out.println(e.getValue().getClass()); } public static void main(String[] args) throws PyException { new PythonInterpreterGetting().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z PythonInterpreterSetting.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c09:PythonInterpreterSetting.java // Passing data from Java to python when using // the PythonInterpreter object. import org.python.util.PythonInterpreter; import org.python.core.*; import java.util.*; import com.bruceeckel.python.*; import com.bruceeckel.test.*; public class PythonInterpreterSetting extends UnitTest { PythonInterpreter interp = new PythonInterpreter(); public void test() throws PyException { // It automatically converts Strings // into native Python strings: interp.set(&quot;a&quot;, &quot;This is a test&quot;); interp.exec(&quot;print a&quot;); interp.exec(&quot;print a[5:]&quot;); // A slice // It also knows what to do with arrays: String[] s = { &quot;How&quot;, &quot;Do&quot;, &quot;You&quot;, &quot;Do?&quot; }; interp.set(&quot;b&quot;, s); interp.exec(&quot;for x in b: print x[0], x&quot;); // set() only takes Objects, so it can't // figure out primitives. Instead, // you have to use wrappers: interp.set(&quot;c&quot;, new PyInteger(1)); interp.set(&quot;d&quot;, new PyFloat(2.2)); interp.exec(&quot;print c + d&quot;); // You can also use Java's object wrappers: interp.set(&quot;c&quot;, new Integer(9)); interp.set(&quot;d&quot;, new Float(3.14)); interp.exec(&quot;print c + d&quot;); // Define a Python function to print arrays: interp.exec( &quot;def prt(x): \n&quot; + &quot; print x \n&quot; + &quot; for i in x: \n&quot; + &quot; print i, \n&quot; + &quot; print x.__class__\n&quot;); // Arrays are Objects, so it has no trouble // figuring out the types contained in arrays: Object[] types = { new boolean[]{ true, false, false, true }, new char[]{ 'a', 'b', 'c', 'd' }, new byte[]{ 1, 2, 3, 4 }, new int[]{ 10, 20, 30, 40 }, new long[]{ 100, 200, 300, 400 }, new float[]{ 1.1f, 2.2f, 3.3f, 4.4f }, new double[]{ 1.1, 2.2, 3.3, 4.4 }, }; for(int i = 0; i &lt; types.length; i++) { interp.set(&quot;e&quot;, types[i]); interp.exec(&quot;prt(e)&quot;); } // It uses toString() to print Java objects: interp.set(&quot;f&quot;, new Date()); interp.exec(&quot;print f&quot;); // You can pass it a List // and index into it... List x = new ArrayList(); for(int i = 0; i &lt; 10; i++) x.add(new Integer(i * 10)); interp.set(&quot;g&quot;, x); interp.exec(&quot;print g&quot;); interp.exec(&quot;print g[1]&quot;); // ... But it's not quite smart enough // to treat it as a Python array: interp.exec(&quot;print g.__class__&quot;); // interp.exec(&quot;print g[5:]); // Fails // If you want it to be a python array, you // must extract the Java array: System.out.println(&quot;ArrayList to array:&quot;); interp.set(&quot;h&quot;, x.toArray()); interp.exec(&quot;print h.__class__&quot;); interp.exec(&quot;print h[5:]&quot;); // Passing in a Map: Map m = new HashMap(); m.put(new Integer(1), new Character('a')); m.put(new Integer(3), new Character('b')); m.put(new Integer(5), new Character('c')); m.put(new Integer(7), new Character('d')); m.put(new Integer(11), new Character('e')); System.out.println(&quot;m: &quot; + m); interp.set(&quot;m&quot;, m); interp.exec(&quot;print m, m.__class__, &quot; + &quot;m[1], m[1].__class__&quot;); // Not a Python dictionary, so this fails: //! interp.exec(&quot;for x in m.keys():&quot; + //! &quot;print x, m[x]&quot;); // To convert a Map to a Python dictionary, // use com.bruceeckel.python.PyUtil: interp.set(&quot;m&quot;, PyUtil.toPyDictionary(m)); interp.exec(&quot;print m, m.__class__, &quot; + &quot;m[1], m[1].__class__&quot;); interp.exec(&quot;for x in m.keys():print x,m[x]&quot;); } public static void main(String[] args) throws PyException { new PythonInterpreterSetting().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z PythonSwing.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:PythonSwing.py # The HTMLButton.java example from # &quot;Thinking in Java, 2nd edition,&quot; Chapter 13, # converted into Jython. # Don't run this as part of the automatic make: #=M @echo skipping PythonSwing.py from javax.swing import JFrame, JButton, JLabel from java.awt import FlowLayout frame = JFrame(&quot;HTMLButton&quot;, visible=1, defaultCloseOperation=JFrame.EXIT_ON_CLOSE) def kapow(e): frame.contentPane.add(JLabel(&quot;&lt;html&gt;&quot;+ &quot;&lt;i&gt;&lt;font size=+4&gt;Kapow!&quot;)) # Force a re-layout to # include the new label: frame.validate() button = JButton(&quot;&lt;html&gt;&lt;b&gt;&lt;font size=+2&gt;&quot; + &quot;&lt;center&gt;Hello!&lt;br&gt;&lt;i&gt;Press me now!&quot;, actionPerformed=kapow) frame.contentPane.layout = FlowLayout() frame.contentPane.add(button) frame.pack() frame.size=200, 500 #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z PythonToJavaClass.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:PythonToJavaClass.py #=T python\java\test\PythonToJavaClass.class #=M jythonc.bat --package python.java.test \ #=M PythonToJavaClass.py # A Python class created to produce a Java class from jarray import array import java class PythonToJavaClass(java.lang.Object): # The '@sig' signature string is used to create # the proper signature in the resulting # Java code: def __init__(self): &quot;@sig public PythonToJavaClass()&quot; print &quot;Constructor for PythonToJavaClass&quot; def simple(self): &quot;@sig public void simple()&quot; print &quot;simple()&quot; # Returning values to Java: def returnString(self): &quot;@sig public java.lang.String returnString()&quot; return &quot;howdy&quot; # You must construct arrays to return along # with the type of the array: def returnArray(self): &quot;@sig public java.lang.String[] returnArray()&quot; test = [ &quot;fee&quot;, &quot;fi&quot;, &quot;fo&quot;, &quot;fum&quot; ] return array(test, java.lang.String) def ints(self): &quot;@sig public java.lang.Integer[] ints()&quot; test = [ 1, 3, 5, 7, 11, 13, 17, 19, 23 ] return array(test, java.lang.Integer) def doubles(self): &quot;@sig public java.lang.Double[] doubles()&quot; test = [ 1, 3, 5, 7, 11, 13, 17, 19, 23 ] return array(test, java.lang.Double) # Passing arguments in from Java: def argIn1(self, a): &quot;@sig public void argIn1(java.lang.String a)&quot; print &quot;a: %s&quot; % a print &quot;a.__class__&quot;, a.__class__ def argIn2(self, a): &quot;@sig public void argIn1(java.lang.Integer a)&quot; print &quot;a + 100: %d&quot; % (a + 100) print &quot;a.__class__&quot;, a.__class__ def argIn3(self, a): &quot;@sig public void argIn3(java.util.List a)&quot; print &quot;received List:&quot;, a, a.__class__ print &quot;element type:&quot;, a[0].__class__ print &quot;a[3] + a[5]:&quot;, a[5] + a[7] #! print &quot;a[2:5]:&quot;, a[2:5] # Doesn't work def argIn4(self, a): &quot;@sig public void \ argIn4(org.python.core.PyArray a)&quot; print &quot;received type:&quot;, a.__class__ print &quot;a: &quot;, a print &quot;element type:&quot;, a[0].__class__ print &quot;a[3] + a[5]:&quot;, a[5] + a[7] print &quot;a[2:5]:&quot;, a[2:5] # A real Python array # A map must be passed in as a PyDictionary: def argIn5(self, m): &quot;@sig public void \ argIn5(org.python.core.PyDictionary m)&quot; print &quot;received Map: &quot;, m, m.__class__ print &quot;m['3']:&quot;, m['3'] for x in m.keys(): print x, m[x] #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z Schedule.ghs 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z Simple2.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:Simple2.py from SimpleClass import Simple class Simple2(Simple): def __init__(self, str): print &quot;Inside Simple2 constructor&quot; # You must explicitly call # the base-class constructor: Simple.__init__(self, str) def display(self): self.showMsg(&quot;Called from display()&quot;) # Overriding a base-class method def show(self): print &quot;Overridden show() method&quot; # Calling a base-class method from inside # the overridden method: Simple.show(self) class Different: def show(self): print &quot;Not derived from Simple&quot; if __name__ == &quot;__main__&quot;: x = Simple2(&quot;Simple2 constructor argument&quot;) x.display() x.show() x.showMsg(&quot;Inside main&quot;) def f(obj): obj.show() # One-line definition f(x) f(Different()) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z SimpleClass.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:SimpleClass.py class Simple: def __init__(self, str): print &quot;Inside the Simple constructor&quot; self.s = str # Two methods: def show(self): print self.s def showMsg(self, msg): print msg + ':', self.show() # Calling another method if __name__ == &quot;__main__&quot;: # Create an object: x = Simple(&quot;constructor argument&quot;) x.show() x.showMsg(&quot;A message&quot;) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z TestPythonToJavaClass.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c09:TestPythonToJavaClass.java //+D python\java\test\PythonToJavaClass.class import java.lang.reflect.*; import java.util.*; import org.python.core.*; import com.bruceeckel.test.*; import com.bruceeckel.util.*; import com.bruceeckel.python.*; // The package with the Python-generated classes: import python.java.test.*; public class TestPythonToJavaClass extends UnitTest { PythonToJavaClass p2j = new PythonToJavaClass(); public void test1() { p2j.simple(); System.out.println(p2j.returnString()); Arrays2.print(p2j.returnArray()); Arrays2.print(p2j.ints()); Arrays2.print(p2j.doubles()); p2j.argIn1(&quot;Testing argIn1()&quot;); p2j.argIn2(new Integer(47)); ArrayList a = new ArrayList(); for(int i = 0; i &lt; 10; i++) a.add(new Integer(i)); p2j.argIn3(a); p2j.argIn4( new PyArray(Integer.class, a.toArray())); Map m = new HashMap(); for(int i = 0; i &lt; 10; i++) m.put(&quot;&quot; + i, new Float(i)); p2j.argIn5(PyUtil.toPyDictionary(m)); } public void dumpClassInfo() { Arrays2.print( p2j.getClass().getConstructors()); Method[] methods = p2j.getClass().getMethods(); for(int i = 0; i &lt; methods.length; i++) { String nm = methods[i].toString(); if(nm.indexOf(&quot;PythonToJavaClass&quot;) != -1) System.out.println(nm); } } public static void main(String[] args) { TestPythonToJavaClass test = new TestPythonToJavaClass(); test.dumpClassInfo(); test.test1(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z differentReturns.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:differentReturns.py def differentReturns(arg): if arg == 1: return &quot;one&quot; if arg == &quot;one&quot;: return 1 print differentReturns(1) print differentReturns(&quot;one&quot;) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z if.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:if.py response = &quot;yes&quot; if response == &quot;yes&quot;: print &quot;affirmative&quot; val = 1 print &quot;continuing...&quot; #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z list.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:list.py list = [ 1, 3, 5, 7, 9, 11 ] print list list.append(13) for x in list: print x #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z makefile 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel # At http://www.BruceEckel.com # (c)2001 Bruce Eckel # Copyright notice in Copyright.txt # Automatically-generated MAKEFILE # For examples in directory .\c09\javaclass # using the JDK 1.3 compiler # Invoke with: make HOME := ../../ ifndef MAKECMDGOALS MAKECMDGOALS := javac endif # Command.com is too weak to build this under Windows NT/2000: ifeq ($(OS),Windows_NT) COMSPEC=$(SYSTEMROOT)\system32\cmd.exe endif ifneq ($(MAKECMDGOALS),clean) include $(HOME)/$(MAKECMDGOALS).mac endif .SUFFIXES : .class .java .java.class : $(JVC) $(JVCFLAGS) $&lt; javac: \ JavaClass.class jikes: \ JavaClass.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif JavaClass.class: JavaClass.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests c09.javaclass.JavaClass </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z myFunction.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:myFunction.py def myFunction(response): val = 0 if response == &quot;yes&quot;: print &quot;affirmative&quot; val = 1 print &quot;continuing...&quot; return val print myFunction(&quot;no&quot;) print myFunction(&quot;yes&quot;) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z stringFormatting.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:stringFormatting.py val = 47 print &quot;The number is %d&quot; % val val2 = 63.4 s = &quot;val: %d, val2: %f&quot; % (val, val2) print s #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z strings.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:strings.py print &quot;That isn't a horse&quot; print 'You are not a &quot;Viking&quot;' print &quot;&quot;&quot;You're just pounding two coconut halves together.&quot;&quot;&quot; print '''&quot;Oh no!&quot; He exclaimed. &quot;It's the blemange!&quot;''' print r'c:\python\lib\utils' #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z sum.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c09:sum.py def sum(arg1, arg2): return arg1 + arg2 print sum(42, 47) print sum('spam ', &quot;eggs&quot;) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z