📄 __builtin__.java
字号:
return val; return def; } public static PyObject globals() { return Py.getFrame().f_globals; } public static boolean hasattr(PyObject o, PyString n) { try { return o.__findattr__(n) != null; } catch (PyException exc) { if (Py.matchException(exc, Py.AttributeError)) return false; throw exc; } } public static PyInteger hash(PyObject o) { return o.__hash__(); } public static PyString hex(PyObject o) { return o.__hex__(); } public static long id(PyObject o) { return Py.id(o); } public static PyObject input(PyObject prompt) { String line = raw_input(prompt); return eval(new PyString(line)); } public static PyObject input() { return input(new PyString("")); } public static PyInteger int$(PyString o, int base) { return Py.newInteger(o.__str__().atoi(base)); } public static PyInteger int$(PyObject o) { return o.__int__(); } public static PyObject object() { return new PyObject(); } private static PyStringMap internedStrings; public static PyString intern(PyString s) { if (internedStrings == null) { internedStrings = new PyStringMap(); } String istring = s.internedString(); PyObject ret = internedStrings.__finditem__(istring); if (ret != null) return (PyString)ret; internedStrings.__setitem__(istring, s); return s; } public static boolean isinstance(PyObject obj, PyObject cls) { if (cls instanceof PyClass) { return issubclass(obj.__class__, (PyClass) cls); } if (cls.getClass() == PyTuple.class) { for (int i = 0; i < cls.__len__(); i++) { if (isinstance(obj, cls.__getitem__(i))) return true; } return false; } else { throw Py.TypeError("isinstance(): 2nd arg is not a class"); } } public static boolean issubclass(PyClass subClass, PyClass superClass) { if (subClass == null || superClass == null) throw Py.TypeError("arguments must be classes"); if (subClass == superClass) return true; if (subClass.proxyClass != null && superClass.proxyClass != null) { if (superClass.proxyClass.isAssignableFrom(subClass.proxyClass)) return true; } if (subClass.__bases__ == null || superClass.__bases__ == null) return false; PyObject[] bases = subClass.__bases__.list; int n = bases.length; for(int i=0; i<n; i++) { PyClass c = (PyClass)bases[i]; if (issubclass(c, superClass)) return true; } return false; } public static int len(PyObject o) { try { return o.__len__(); } catch (PyException e) { // Make this work like CPython where // // a = 7; len(a) raises a TypeError, // a.__len__() raises an AttributeError // and // class F: pass // f = F(); len(f) also raises an AttributeError // // Testing the type of o feels unclean though if (e.type == Py.AttributeError && !(o instanceof PyInstance)) throw Py.TypeError("len() of unsized object"); else throw e; } } public static PyList list(PyObject o) { if (o instanceof PyList) return (PyList) o.__getslice__(Py.None, Py.None, Py.One); if (o instanceof PyTuple) { // always make a copy, otherwise the list will share the // underlying data structure with the tuple object, which // renders the tuple mutable! PyTuple t = (PyTuple)o; PyObject[] a = new PyObject[t.__len__()]; System.arraycopy(t.list, 0, a, 0, a.length); return new PyList(a); } return new PyList(o); } public static PyObject locals() { return Py.getFrame().getf_locals(); } public static PyLong long$(BigInteger o) { return Py.newLong(o); } public static PyLong long$(PyObject o) { return o.__long__(); } public static PyLong long$(PyString o, int base) { return o.__str__().atol(base); } public static PyObject map(PyObject[] argstar) { int n = argstar.length-1; if (n < 1) throw Py.TypeError("map requires at least two arguments"); PyObject element; PyObject f = argstar[0]; PyList list = new PyList(); PyObject[] args = new PyObject[n]; PyObject[] iters = new PyObject[n]; for (int j = 0; j < n; j++) { iters[j] = Py.iter(argstar[j+1], "argument " + (j+1) + " to map() must support iteration"); } while (true) { boolean any_items = false; for(int j = 0; j < n; j++) { if ((element = iters[j].__iternext__()) != null) { args[j] = element; any_items = true; } else { args[j] = Py.None; } } if (!any_items) break; if (f == Py.None) { if (n == 1) { list.append(args[0]); } else { list.append(new PyTuple((PyObject[])args.clone())); } } else { list.append(f.__call__(args)); } } return list; } // I've never been happy with max and min builtin's... public static PyObject max(PyObject[] l) { if (l.length == 1) return max(l[0]); else return max(new PyTuple(l)); } private static PyObject max(PyObject o) { PyObject max = null; PyObject iter = o.__iter__(); for (PyObject item; (item = iter.__iternext__()) != null; ) { if (max == null || item._gt(max).__nonzero__()) max = item; } if (max == null) throw Py.ValueError("max of empty sequence"); return max; } public static PyObject min(PyObject[] l) { if (l.length == 1) return min(l[0]); else return min(new PyTuple(l)); } private static PyObject min(PyObject o) { PyObject min = null; PyObject iter = o.__iter__(); for (PyObject item; (item = iter.__iternext__()) != null; ) { if (min == null || item._lt(min).__nonzero__()) min = item; } if (min == null) throw Py.ValueError("min of empty sequence"); return min; } public static PyString oct(PyObject o) { return o.__oct__(); } /** * Open a file read-only. * @param name the file to open. * @exception java.io.IOException */ public static PyFile open(String name) throws java.io.IOException { return new PyFile(name, "r", -1); } /** * Open a file with the specified mode. * @param name name of the file to open. * @param mode open mode of the file. Use "r", "w", "r+", "w+" and "a". * @exception java.io.IOException */ public static PyFile open(String name, String mode) throws java.io.IOException { return new PyFile(name, mode, -1); } /** * Open a file with the specified mode and buffer size. * @param name name of the file to open. * @param mode open mode of the file. Use "r", "w", "r+", "w+" and "a". * @param bufsize size of the internal buffer. Not currently used. * @exception java.io.IOException */ public static PyFile open(String name, String mode, int bufsize) throws java.io.IOException { return new PyFile(name, mode, bufsize); } /** * Open a file read-only. * @param name the file to open. * @exception java.io.IOException */ public static PyFile file(String name) throws java.io.IOException { return open(name); } /** * Open a file with the specified mode. * @param name name of the file to open. * @param mode open mode of the file. Use "r", "w", "r+", "w+" and "a". * @exception java.io.IOException */ public static PyFile file(String name, String mode) throws java.io.IOException { return open(name, mode); } /** * Open a file with the specified mode and buffer size. * @param name name of the file to open. * @param mode open mode of the file. Use "r", "w", "r+", "w+" and "a". * @param bufsize size of the internal buffer. Not currently used. * @exception java.io.IOException */ public static PyFile file(String name, String mode, int bufsize) throws java.io.IOException { return open(name, mode, bufsize); } public static final int ord(char c) { return (int)(c); } public static PyObject pow(PyObject x, PyObject y) { return x._pow(y); } private static boolean coerce(PyObject[] objs) { PyObject x = objs[0]; PyObject y = objs[1]; if (x.__class__ == y.__class__) return true; Object ctmp = x.__coerce_ex__(y); if (ctmp != null && ctmp != Py.None) { if (ctmp instanceof PyObject[]) { x = ((PyObject[])ctmp)[0]; y = ((PyObject[])ctmp)[1]; } else { y = (PyObject)ctmp; } } objs[0] = x; objs[1] = y; if (x.__class__ == y.__class__) return true; ctmp = y.__coerce_ex__(x); if (ctmp != null && ctmp != Py.None) { if (ctmp instanceof PyObject[]) { y = ((PyObject[])ctmp)[0]; x = ((PyObject[])ctmp)[1]; } else { x = (PyObject)ctmp; } } objs[0] = x; objs[1] = y; //System.out.println(""+x.__class__+" : "+y.__class__); return x.__class__ == y.__class__; } public static PyObject pow(PyObject xi, PyObject yi, PyObject zi) { PyObject x=xi; PyObject y=yi; PyObject z=zi; boolean doit=false; PyObject[] tmp = new PyObject[2]; tmp[0] = x; tmp[1] = y; if (coerce(tmp)) { x = tmp[0]; y = tmp[1]; tmp[1] = z; if (coerce(tmp)) { x = tmp[0]; z = tmp[1]; tmp[0] = y; if (coerce(tmp)) { z = tmp[1]; y = tmp[0]; doit=true; } } } else { tmp[1] = z; if (coerce(tmp)) { x=tmp[0]; z=tmp[1]; tmp[0] = y; if (coerce(tmp)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -