carfield.com.hk PyUtil.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:python:PyUtil.java // PythonInterpreter utilities package com.bruceeckel.python; import org.python.util.PythonInterpreter; import org.python.core.*; import java.util.*; public class PyUtil { /** Extract a Python tuple or array into a Java List (which can be converted into other kinds of lists and sets inside Java). @param interp The Python interpreter object @param pyName The id of the python list object */ public static List toList(PythonInterpreter interp, String pyName){ return new ArrayList(Arrays.asList( (Object[])interp.get( pyName, Object[].class))); } /** Extract a Python dictionary into a Java Map @param interp The Python interpreter object @param pyName The id of the python dictionary */ public static Map toMap(PythonInterpreter interp, String pyName){ PyList pa = ((PyDictionary)interp.get( pyName)).items(); Map map = new HashMap(); while(pa.__len__() != 0) { PyTuple po = (PyTuple)pa.pop(); Object first = po.__finditem__(0) .__tojava__(Object.class); Object second = po.__finditem__(1) .__tojava__(Object.class); map.put(first, second); } return map; } /** Turn a Java Map into a PyDictionary, suitable for placing into a PythonInterpreter @param map The Java Map object */ public static PyDictionary toPyDictionary(Map map) { Map m = new HashMap(); Iterator it = map.entrySet().iterator(); while(it.hasNext()) { Map.Entry e = (Map.Entry)it.next(); m.put(Py.java2py(e.getKey()), Py.java2py(e.getValue())); } // PyDictionary constructor wants a Hashtable: return new PyDictionary(new Hashtable(m)); } } ///:~ </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 RunUnitTests.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:test:RunUnitTests.java // Discovering the unit test // class and running each test. package com.bruceeckel.test; import java.lang.reflect.*; import java.util.Iterator; public class RunUnitTests { public static void require(boolean requirement, String errmsg) { if(!requirement) { System.err.println(errmsg); System.exit(1); } } public static void main(String[] args) { require(args.length == 1, &quot;Usage: RunUnitTests qualified-class&quot;); try { Class c = Class.forName(args[0]); // Only finds the inner classes // declared in the current class: Class[] classes = c.getDeclaredClasses(); Class ut = null; for(int j = 0; j &lt; classes.length; j++) { // Skip inner classes that are // not derived from UnitTest: if(!UnitTest.class. isAssignableFrom(classes[j])) continue; ut = classes[j]; break; // Finds the first test class only } // If it found an inner class, // that class must be static: if(ut != null) require( Modifier.isStatic(ut.getModifiers()), &quot;inner UnitTest class must be static&quot;); // If it couldn't find the inner class, // maybe it's a regular class (for black- // box testing: if(ut == null) if(UnitTest.class.isAssignableFrom(c)) ut = c; require(ut != null, &quot;No UnitTest class found&quot;); require( Modifier.isPublic(ut.getModifiers()), &quot;UnitTest class must be public&quot;); Method[] methods = ut.getDeclaredMethods(); for(int k = 0; k &lt; methods.length; k++) { Method m = methods[k]; // Ignore overridden UnitTest methods: if(m.getName().equals(&quot;cleanup&quot;)) continue; // Only public methods with no // arguments and void return // types will be used as test code: if(m.getParameterTypes().length == 0 &amp;&amp; m.getReturnType() == void.class &amp;&amp; Modifier.isPublic(m.getModifiers())) { // The name of the test is // used in error messages: UnitTest.testID = m.getName(); // A new instance of the // test object is created and // cleaned up for each test: Object test = ut.newInstance(); m.invoke(test, new Object[0]); ((UnitTest)test).cleanup(); } } } catch(Exception e) { e.printStackTrace(System.err); // Any exception will return a nonzero // value to the console, so that // 'make' will abort: System.err.println(&quot;Aborting make&quot;); System.exit(1); } // After all tests in this class are run, // display any results. If there were errors, // abort 'make' by returning a nonzero value. if(UnitTest.errors.size() != 0) { Iterator it = UnitTest.errors.iterator(); while(it.hasNext()) System.err.println(it.next()); System.exit(1); } } } ///:~ </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 StringList.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:util:StringList.java // General-purpose tool that reads a file of text // lines into a List, one line per list. package com.bruceeckel.util; import java.io.*; import java.util.*; public class StringList extends ArrayList { public StringList(String textFile) { try { BufferedReader inputs = new BufferedReader ( new FileReader(textFile)); String line; while((line = inputs.readLine()) != null) add(line.trim()); } catch (IOException e) { e.printStackTrace(System.err); } } } ///:~ </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 Test.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:python:Test.java package com.bruceeckel.python; import org.python.util.PythonInterpreter; import java.util.*; import com.bruceeckel.test.*; public class Test extends UnitTest { PythonInterpreter pi = new PythonInterpreter(); public void test1() { pi.exec(&quot;tup=('fee','fi','fo','fum','fi')&quot;); List lst = PyUtil.toList(pi, &quot;tup&quot;); System.out.println(lst); System.out.println(new HashSet(lst)); } public void test2() { pi.exec(&quot;ints=[1,3,5,7,9,11,13,17,19]&quot;); List lst = PyUtil.toList(pi, &quot;ints&quot;); System.out.println(lst); } public void test3() { pi.exec(&quot;dict = { 1 : 'a', 3 : 'b', &quot; + &quot;5 : 'c', 9 : 'd', 11 : 'e'}&quot;); Map mp = PyUtil.toMap(pi, &quot;dict&quot;); System.out.println(mp); } public void test4() { Map m = new HashMap(); m.put(&quot;twas&quot;, new Integer(11)); m.put(&quot;brillig&quot;, new Integer(27)); m.put(&quot;and&quot;, new Integer(47)); m.put(&quot;the&quot;, new Integer(42)); m.put(&quot;slithy&quot;, new Integer(33)); m.put(&quot;toves&quot;, new Integer(55)); System.out.println(m); pi.set(&quot;m&quot;, PyUtil.toPyDictionary(m)); pi.exec(&quot;print m&quot;); pi.exec(&quot;print m['slithy']&quot;); } public static void main(String args[]) { Test t = new Test(); t.test1(); t.test2(); t.test3(); t.test4(); } } ///:~ </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 TypedIterator.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:util:TypedIterator.java package com.bruceeckel.util; import java.util.*; public class TypedIterator implements Iterator { private Iterator imp; private Class type; public TypedIterator(Iterator it, Class type) { imp = it; this.type = type; } public boolean hasNext() { return imp.hasNext(); } public void remove() { imp.remove(); } public Object next() { Object obj = imp.next(); if(!type.isInstance(obj)) throw new ClassCastException( &quot;TypedIterator for type &quot; + type + &quot; encountered type: &quot; + obj.getClass()); return obj; } } ///:~ </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 UnitTest.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:test:UnitTest.java // The basic unit testing class package com.bruceeckel.test; import java.util.*; public class UnitTest { static String testID; static List errors = new ArrayList(); // Override cleanup() if test object // creation allocates non-memory // resources that must be cleaned up: protected void cleanup() {} // Verify the truth of a condition: protected final void affirm(boolean condition){ if(!condition) errors.add(&quot;failed: &quot; + testID); } } ///:~ </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 .\com\bruceeckel\util # 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: \ StringList.class \ TypedIterator.class jikes: \ StringList.class \ TypedIterator.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif StringList.class: StringList.java TypedIterator.class: TypedIterator.java </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