Created
January 26, 2010 15:28
-
-
Save tzellman/286908 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gist; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.Iterator; | |
import org.apache.commons.lang.exception.ExceptionUtils; | |
import org.python.core.PyCode; | |
import org.python.core.PyFunction; | |
import org.python.core.PyList; | |
import org.python.util.PythonInterpreter; | |
public class JythonTest | |
{ | |
protected PythonInterpreter interpreter; | |
public JythonTest() | |
{ | |
interpreter = new PythonInterpreter(); | |
} | |
public void runFile(String filename) throws IOException | |
{ | |
interpreter.execfile(filename); | |
} | |
public void compileFileAndRun(String filename) throws IOException | |
{ | |
FileReader reader = new FileReader(filename); | |
PyCode code = interpreter.compile(reader); | |
reader.close(); | |
interpreter.exec(code); | |
} | |
public void importAndRun(String filename) throws IOException | |
{ | |
File file = new File(filename); | |
String module = file.getName(); | |
if (module.endsWith(".py")) | |
module = module.substring(0, module.length() - 3); | |
StringBuffer cmd = new StringBuffer(); | |
cmd.append("import sys, imp\n"); | |
cmd.append("funcs = []\n"); | |
cmd.append("sys.path.insert(0, r'" | |
+ file.getAbsoluteFile().getParentFile().getAbsolutePath() | |
+ "')\n"); | |
cmd.append("try:\n"); | |
cmd.append(" f, p, d = imp.find_module('" + module + "')\n"); | |
cmd.append(" t = imp.load_module('" + module + "', f, p, d)\n"); | |
cmd.append(" for i in dir(t):\n"); | |
cmd.append(" if not i.startswith('_'):\n"); | |
cmd.append(" obj = getattr(t, i)\n"); | |
cmd.append(" if hasattr(obj, 'func_name'):\n"); | |
cmd.append(" funcs.append(obj)\n"); | |
cmd.append("except Exception, e:\n"); | |
cmd.append(" raise e\n"); | |
cmd.append("del sys.path[0]\n"); | |
interpreter.exec(cmd.toString()); | |
PyList funcs = (PyList) interpreter.get("funcs"); | |
for (Iterator<PyFunction> iterator = funcs.iterator(); iterator | |
.hasNext();) | |
{ | |
PyFunction func = iterator.next(); | |
func.__call__(); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
try | |
{ | |
if (args.length < 1) | |
{ | |
throw new Exception("Must provide at least 1 python file"); | |
} | |
JythonTest tester = new JythonTest(); | |
for (String arg : args) | |
{ | |
System.out.println("Execing: " + arg); | |
tester.runFile(arg); | |
System.out.println("=========\nCompiling and running: " + arg); | |
tester.compileFileAndRun(arg); | |
System.out.println("=========\nImporting and running: " + arg); | |
tester.importAndRun(arg); | |
} | |
} | |
catch (Exception e) | |
{ | |
System.out.println(ExceptionUtils.getStackTrace(e)); | |
System.err.println("Error: " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gist import Utils | |
from gist.p2 import Utils2 | |
def run(): | |
print Utils.random() | |
print Utils2.randomString(10) | |
if __name__ == '__main__': | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gist; | |
import java.util.Random; | |
public class Utils | |
{ | |
public static final double random() | |
{ | |
return new Random(System.currentTimeMillis()).nextDouble(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gist.p2; | |
import java.util.Random; | |
public class Utils2 | |
{ | |
public static final String CHARS = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
public static final String randomString(int length) | |
{ | |
StringBuffer buf = new StringBuffer(); | |
Random random = new Random(System.currentTimeMillis()); | |
int charsLen = CHARS.length(); | |
for (int i = 0; i < length; ++i) | |
{ | |
int nextInt = random.nextInt(charsLen); | |
char c = CHARS.charAt(nextInt); | |
if (random.nextBoolean()) | |
buf.append(String.valueOf(c).toUpperCase()); | |
else | |
buf.append(c); | |
} | |
return buf.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment