interpreter.set("myvariable", Integer(21)); PyObject answer = interpreter.eval("'the answer is: %s' % (2*myvariable)"); System.out.println(answer.toString());
PyObject str = interpreter.eval("repr(myPythonClass().abc())"); System.out.println(str.toString());
public void setError(Throwable e) { StringBuilder sb = new StringBuilder("<html> "); if (e instanceof NodeRenderException) { Node node = ((NodeRenderException) e).getNode(); sb.append("<b>"); sb.append(node.getName()); sb.append(":</b> "); } sb.append("<u>"); nodeRenderException = getRootCause(e); if (nodeRenderException instanceof PyException) { PyException ex = (PyException) nodeRenderException; if (ex.value != null) { sb.append(ex.value.toString()); } else { sb.append(ex.toString()); } } else if (nodeRenderException instanceof OutOfMemoryError) { sb.append("Out of memory. Are you trying to process an infinite list?"); } else { sb.append(nodeRenderException.getMessage()); } sb.append("</u></html>"); errorLabel.setText(sb.toString()); errorLabel.setVisible(true); }
StructLayout(Field[] fields, com.kenai.jffi.Type struct, MemoryOp op) { super(NativeType.STRUCT, struct, op); Map<Object, Field> m = new HashMap<Object, Field>(fields.length); for (Field f : fields) { m.put(f.name, f); m.put(f.name.toString(), f); } this.fieldMap = m; this.fields = Collections.unmodifiableList(Arrays.asList(fields)); }
private static void initCacheDirectory(Properties props) { String skip = props.getProperty(PYTHON_CACHEDIR_SKIP, "false"); if (skip.equalsIgnoreCase("true")) { cachedir = null; return; } cachedir = new File(props.getProperty(PYTHON_CACHEDIR, CACHEDIR_DEFAULT_NAME)); if (!cachedir.isAbsolute()) { cachedir = new File(prefix == null ? null : prefix.toString(), cachedir.getPath()); } }
/** * Construct a procedure name for the relevant schema and catalog information. */ protected String getProcedureName() { StringBuffer proc = new StringBuffer(); if (this.procedureCatalog.__nonzero__()) { proc.append(this.procedureCatalog.toString()).append("."); } return proc.append(this.procedureName.toString()).toString(); } }
private static void checkTrailingSlash(PyObject path, Map<String, Object> attributes) { Boolean isDirectory = (Boolean) attributes.get("isDirectory"); if (isDirectory != null && !isDirectory.booleanValue()) { String pathStr = path.toString(); if (pathStr.endsWith(File.separator) || pathStr.endsWith("/.")) { throw Py.OSError(Errno.ENOTDIR, path); } } }
private static String toStr(String name, PyObject src, String dflt) { if (src == null) { return dflt; } if (src == Py.None) { return null; } if (!(src instanceof PyBaseString)) { throw Py.TypeError(String.format("\"%s\" must be an string", name)); } return src.toString(); }
/** * Write a string to the file. * @param obj The data to write. */ public void write(PyObject obj) { write(obj.toString()); }
int doPack(ByteStream buf, int count, int pos, PyObject[] args) { PyObject value = args[pos]; if (!(value instanceof PyString)) throw StructError("argument for 'p' must be a string"); buf.writeByte(Math.min(0xFF, Math.min(value.toString().length(), count-1))); return super.doPack(buf, count-1, pos, args); }
void pack(ByteStream buf, PyObject value) { if (!(value instanceof PyString) || value.__len__() != 1) throw StructError("char format require string of length 1"); buf.writeByte(value.toString().charAt(0)); }
private String[] read_strings(int depth) { PyTuple t = (PyTuple) read_object_notnull(depth); String some_strings[] = new String[t.__len__()]; int i = 0; for (PyObject item : t.asIterable()) { some_strings[i++] = item.toString().intern(); } return some_strings; }
@Override public String toString() { PyObject mod = __dict__.__finditem__("__module__"); String modStr = (mod == null || !Py.isInstance(mod, PyString.TYPE)) ? "?" : mod.toString(); return String.format("<class %s.%s at %s>", modStr, __name__, Py.idstr(this)); }
private static char toChar(String name, PyObject src, char dflt) { if (src == null) { return dflt; } boolean isStr = Py.isInstance(src, PyString.TYPE); if (src == Py.None || isStr && src.__len__() == 0) { return '\0'; } else if (!isStr || src.__len__() != 1) { throw Py.TypeError(String.format("\"%s\" must be an 1-character string", name)); } return src.toString().charAt(0); }
private void assertParseError(String spec, String expected) { String error = null; try { InternalFormat.fromText(spec); } catch (PyException e) { assertEquals(Py.ValueError, e.type); error = e.value.toString(); } assertEquals(expected, error); }
@Override public final String toString() { return getType().getName() + "(" + getValue().toString() + ")"; } }
public void marshal(HeapInvocationBuffer buffer, PyObject parameter) { if (parameter instanceof StringCData) { buffer.putAddress(((StringCData) parameter).getMemory().getAddress()); } else if (parameter instanceof PyNone) { buffer.putAddress(0); } else { byte[] bytes = parameter.toString().getBytes(); buffer.putArray(bytes, 0, bytes.length, ArrayFlags.IN | ArrayFlags.NULTERMINATE); } } }
static public void pack_into(PyObject[] args) { if (args.length < 3) Py.TypeError("illegal argument type for built-in operation"); String format = args[0].toString(); FormatDef[] f = whichtable(format); int size = calcsize(format, f); pack_into(format, f, size, 1, args); }
private void assertValueError(String formatSpec, String expected) { try { IntegerFormatter f = PyInteger.prepareFormatter(InternalFormat.fromText(formatSpec)); // f.format(123).pad().getResult(); fail("ValueError not thrown, expected: " + expected); } catch (PyException pye) { assertEquals(expected, pye.value.toString()); } }
@ExposedMethod public String toString() { String owner = _lock.getOwnerName(); return Py.newString("<_threading.RLock owner=%r count=%d>"). __mod__(new PyTuple( owner != null ? Py.newStringOrUnicode(owner) : Py.None, Py.newInteger(_lock.getHoldCount()))).toString(); } }