private PyObject call(String module, String function, PyObject[] args) { PyObject sre = imp.importName(module, true); return sre.invoke(function, args); }
@ExposedMethod(defaults = "Py.None") public PyObject dictproxy_get(PyObject key, PyObject default_object) { return dict.invoke("get", key, default_object); }
@ExposedMethod public PyObject dictproxy_iteritems() { return dict.invoke("iteritems"); }
@ExposedMethod public PyObject dictproxy_values() { return dict.invoke("values"); }
@ExposedMethod public PyObject dictproxy_itervalues() { return dict.invoke("itervalues"); }
private void printToFileObject(PyObject file, PyObject o) { if (!(o instanceof PyUnicode)) { o = o.__str__(); } file.invoke("write", o); }
@Override public void flush() { PyObject out = myFile(); if (out instanceof PyFile) { ((PyFile) out).flush(); } else { try { out.invoke("flush"); } catch (PyException pye) { // ok } } }
private static PyString readline(PyObject file) { if (file instanceof PyFile) { return ((PyFile) file).readline(); } else { PyObject ret = file.invoke("readline"); if (!(ret instanceof PyString)) { throw Py.TypeError("object.readline() returned non-string"); } return (PyString) ret; } }
void reload() throws PyIgnoreMethodTag { __dict__.invoke("update", getType().fastGetDict()); }
@Override public PyObject __call__(PyObject memo) { Set<Object> newSet = new HashSet<>(); for (Object obj : asSet()) { PyObject pyobj = Py.java2py(obj); PyObject newobj = pyobj.invoke("__deepcopy__", memo); newSet.add(newobj.__tojava__(Object.class)); } return makePySet(newSet); } };
private void merge(PyObject other) { if (other instanceof PyDictionary) { jmap.putAll(((PyDictionary) other).getMap()); } else if (other instanceof PyStringMap) { mergeFromKeys(other, ((PyStringMap) other).keys()); } else { mergeFromKeys(other, other.invoke("keys")); } }
public void write(String s) { PyObject out = myFile(); if (out instanceof PyFile) { ((PyFile) out).write(s); } else { out.invoke("write", new PyString(s)); } }
/** * Calls _strptime.strptime(), for cases that our SimpleDateFormat backed * strptime can't handle. */ private static PyTuple pystrptime(String data_string, String format) { return (PyTuple) __builtin__.__import__("_strptime") .invoke("_strptime_time", Py.newUnicode(data_string), Py.newUnicode(format)); }
public static void classDictInit(PyObject dict) { dict.invoke("clear"); dict.__setitem__("Random", PyRandom.TYPE); dict.__setitem__("__name__", Py.newString("_random")); } }
public static PyObject dir() { PyObject l = locals(); PyList ret; PyObject retObj = l.invoke("keys"); try { ret = (PyList) retObj; } catch (ClassCastException e) { throw Py.TypeError(String.format("Expected keys() to be a list, not '%s'", retObj.getType().fastGetName())); } ret.sort(); return ret; }
@ExposedMethod final PyObject methodcaller___call__(PyObject[] args, String[] keywords) { if (args.length > 1) { throw Py.TypeError("methodcaller expected 1 arguments, got " + args.length); } ArgParser ap = new ArgParser("methodcaller", args, Py.NoKeywords, "obj"); PyObject obj = ap.getPyObject(0); return obj.invoke(name, this.args, this.keywords); }
public String[] getEnv() { PyObject items = imp.load("os").__getattr__("environ").invoke("items"); String[] env = new String[items.__len__()]; int i = 0; for (PyObject item : items.asIterable()) { env[i++] = String.format("%s=%s", item.__getitem__(0), item.__getitem__(1)); } return env; }
public PyObject groupdict(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("groupdict", args, kws, "default"); PyObject def = ap.getPyObject(0, Py.None); PyObject result = new PyDictionary(); if (pattern.groupindex == null) return result; PyObject keys = pattern.groupindex.invoke("keys"); PyObject key; for (int i = 0; (key = keys.__finditem__(i)) != null; i++) { PyObject item = getslice(key, def); result.__setitem__(key, item); } return result; }
private void save_dict(PyObject object) { if (protocol > 0) file.write(EMPTY_DICT); else { file.write(MARK); file.write(DICT); } put(putMemo(get_id(object), object)); batch_setitems(object.invoke("iteritems")); }
public void testCallInstancesFromJava() { PythonInterpreter interp = new PythonInterpreter(); interp.exec("class Blah(object):\n" + " def __init__(self, val):\n" + " self.val = val\n" + " def incval(self):\n" + " self.val += 1\n" + " return self.val"); PyObject blahClass = interp.get("Blah"); int base = 42; PyObject blahInstance = blahClass.__call__(new PyInteger(base)); for (int i = 0; i < 4; i++) { assertEquals(++base, blahInstance.invoke("incval").__tojava__(Integer.class)); } }