@Override public PyString read(Kryo kryo, Input input, Class<PyString> type) { return new PyString(input.readString()); } }
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("import sys\nsys.path.append('pathToModiles if they're not there by default')\nimport yourModule"); // execute a function that takes a string and returns a string PyObject someFunc = interpreter.get("funcName"); PyObject result = someFunc.__call__(new PyString("Test!")); String realResult = (String) result.__tojava__(String.class);
return new PyString(((TemplateScalarModel) model).getAsString());
public synchronized static void initializePython() { if (isInitialized.get()) return; // Set the Jython package cache directory. Properties jythonProperties = new Properties(); String jythonCacheDir = Platform.getUserDataDirectory() + Platform.SEP + "_jythoncache"; jythonProperties.put("python.cachedir", jythonCacheDir); // Initialize Python. PySystemState.initialize(System.getProperties(), jythonProperties, new String[]{""}); // Add the built-in Python libraries. File nodeBoxLibraries = new File(libDir, "nodeboxlibs.zip"); Py.getSystemState().path.add(new PyString(nodeBoxLibraries.getAbsolutePath())); // This folder contains unarchived NodeBox libraries. // Only used in development. File developmentLibraries = new File("src/main/python"); Py.getSystemState().path.add(new PyString(developmentLibraries.getAbsolutePath())); // Add the user's Python directory. Py.getSystemState().path.add(new PyString(Platform.getUserPythonDirectory().getAbsolutePath())); isInitialized.set(true); }
public ImmutableMap<String, Function> call() throws Exception { // This creates a dependency between function and the client. // However, we need to know the load paths before we can do anything, so this is necessary. PythonUtils.initializePython(); Py.getSystemState().path.append(new PyString(file.getParentFile().getCanonicalPath())); PythonInterpreter interpreter = new PythonInterpreter(); try { interpreter.execfile(file.getCanonicalPath()); } catch (IOException e) { throw new LoadException(file, e); } catch (PyException e) { throw new LoadException(file, e); } PyStringMap map = (PyStringMap) interpreter.getLocals(); ImmutableMap.Builder<String, Function> builder = ImmutableMap.builder(); for (Object key : map.keys()) { Object o = map.get(Py.java2py(key)); if (o instanceof PyFunction) { String name = (String) key; Function f = new PythonFunction(name, (PyFunction) o); builder.put(name, f); } } return builder.build(); } });
/** * Create an instance of the same type as this object, from the Java String given as argument. * This is to be overridden in a subclass to return its own type. * * @param str to wrap * @return */ public PyString createInstance(String str) { return new PyString(str); }
/** * Create an instance of the same type as this object, from the Java String given as argument. * This is to be overridden in a subclass to return its own type. * * @param string UTF-16 string encoding the characters (as Java). * @param isBasic is ignored in <code>PyString</code> (effectively true). * @return */ protected PyString createInstance(String str, boolean isBasic) { // ignore isBasic, doesn't apply to PyString, just PyUnicode return new PyString(str); }
@ExposedGet(name = "name") public PyObject getName() { if (name == null) return Py.None; return new PyString(name); } @ExposedSet(name = "name")
@ExposedGet(name = "vararg") public PyObject getVararg() { if (vararg == null) return Py.None; return new PyString(vararg); } @ExposedSet(name = "vararg")
static final PyString makeCharacter(int codepoint, boolean toUnicode) { if (toUnicode) { return new PyUnicode(codepoint); } else if (codepoint < 0 || codepoint > 255) { // This will throw IllegalArgumentException since non-byte value return new PyString('\uffff'); } return letters[codepoint]; }
private static void addPaths(PyList path, String pypath) { StringTokenizer tok = new StringTokenizer(pypath, java.io.File.pathSeparator); while (tok.hasMoreTokens()) { path.append(new PyString(tok.nextToken().trim())); } }
@Override public PyObject __dir__() { PyString members[] = new PyString[__members__.length]; for (int i = 0; i < __members__.length; i++) { members[i] = new PyString(__members__[i]); } return new PyList(members); }
private static PyTuple toPyStringTuple(String[] ar) { if (ar == null) { return Py.EmptyTuple; } int sz = ar.length; PyString[] pystr = new PyString[sz]; for (int i = 0; i < sz; i++) { pystr[i] = new PyString(ar[i]); } return new PyTuple(pystr); }
@ExposedMethod(doc = tobytes_doc) final PyString memoryview_tobytes() { checkNotReleased(); if (backing instanceof BaseBuffer) { // In practice, it always is return new PyString(backing.toString()); } else { // But just in case ... String s = StringUtil.fromBytes(backing); return new PyString(s); } }
/** * Common code used by the number-base conversion method __oct__ and __hex__. * * @param spec prepared format-specifier. * @return converted value of this object */ private PyString formatImpl(Spec spec) { // Traditional formatter (%-format) because #o means "-0123" not "-0o123". IntegerFormatter f = new IntegerFormatter.Traditional(spec); f.format(value); return new PyString(f.getResult()); }
public static void classDictInit(PyObject dict) { dict.__setitem__("__name__", new PyString("_functools")); dict.__setitem__("__doc__", __doc__); dict.__setitem__("partial", PyPartial.TYPE); // Hide from Python dict.__setitem__("classDictInit", null); }
/** * Common code used by the number-base conversion method __oct__ and __hex__. * * @param spec prepared format-specifier. * @return converted value of this object */ private PyString formatImpl(Spec spec) { // Traditional formatter (%-format) because #o means "-0123" not "-0o123". IntegerFormatter f = new IntegerFormatter.Traditional(spec); f.format(value).append('L'); return new PyString(f.getResult()); }
public static void classDictInit(PyObject dict) { dict.__setitem__("__name__", new PyString("zipimport")); dict.__setitem__("__doc__", __doc__); dict.__setitem__("zipimporter", zipimporter.TYPE); dict.__setitem__("_zip_directory_cache", _zip_directory_cache); dict.__setitem__("ZipImportError", ZipImportError); // Hide from Python dict.__setitem__("classDictInit", null); dict.__setitem__("initClassExceptions", null); }