@org.python.Method( __doc__ = "L.index(value, [start, [stop]]) -> integer -- return first index of value.\nRaises ValueError if the value is not present.", args = {"item"}, default_args = {"start", "end"}) public org.python.Object index( org.python.Object item, org.python.Object start, org.python.Object end) { if (start != null && !(start instanceof org.python.types.Int)) { throw new org.python.exceptions.TypeError( "list indices must be integers, not " + start.typeName()); } if (end != null && !(end instanceof org.python.types.Int)) { throw new org.python.exceptions.TypeError( "list indices must be integers, not " + end.typeName()); } int iStart = 0, iEnd = this.value.size(); if (end != null) { iEnd = toPositiveIndex(((Long) end.toJava()).intValue()); } if (start != null) { iStart = toPositiveIndex(((Long) start.toJava()).intValue()); } for (int i = iStart; i < Math.min(iEnd, this.value.size()); i++) { if (((org.python.types.Bool) this.value.get(i).__eq__(item)).value) { return new org.python.types.Int(i); } } throw new org.python.exceptions.ValueError( String.format("%d is not in list", ((org.python.types.Int) item).value)); }
public org.python.types.Str __repr__() { java.lang.StringBuilder buffer = new java.lang.StringBuilder("["); boolean first = true; for (org.python.Object obj : this.value) { if (first) { first = false; } else { buffer.append(", "); } buffer.append(obj.__repr__()); } buffer.append("]"); return new org.python.types.Str(buffer.toString()); }
@org.python.Method( __doc__ = "", args = {"other", "modulus"}) public org.python.Object __pow__(org.python.Object other, org.python.Object modulus) { throw new org.python.exceptions.TypeError( "unsupported operand type(s) for ** or pow(): 'super()' and '" + other.typeName() + "'"); }
@org.python.Method( __doc__ = "", args = {"other"}) public org.python.Object __or__(org.python.Object other) { throw new org.python.exceptions.TypeError( "unsupported operand type(s) for |: 'super()' and '" + other.typeName() + "'"); }
@org.python.Method( __doc__ = "L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*", args = {}, default_args = {"key", "reverse"}) public org.python.Object sort(final org.python.Object key, org.python.Object reverse) { if (key == null && reverse == null) { Collections.sort(this.value); } else { // needs to be final in order to use inside the comparator final boolean shouldReverse = reverse == null ? false : ((org.python.types.Bool) reverse.__bool__()).value; Collections.sort( this.value, new Comparator<org.python.Object>() { @Override public int compare(org.python.Object o1, org.python.Object o2) { org.python.Object val1 = o1; org.python.Object val2 = o2; if (key != null) { val1 = ((org.python.types.Function) key).invoke(o1, null, null); val2 = ((org.python.types.Function) key).invoke(o2, null, null); } return shouldReverse ? val2.compareTo(val1) : val1.compareTo(val2); } }); } return org.python.types.NoneType.NONE; }
@org.python.Method(__doc__ = "") public org.python.Object __ge__(org.python.Object other) { if (other instanceof org.python.types.List) { org.python.types.List otherList = (org.python.types.List) other; int size = this.value.size(); int otherSize = otherList.value.size(); int count = Math.min(size, otherSize); for (int i = 0; i < count; i++) { org.python.types.Bool b = (org.python.types.Bool) this.value.get(i).__ge__(otherList.value.get(i)); if (!b.value) { return b; } } // At this point the lists are different sizes or every comparison is true. return new org.python.types.Bool(size >= otherSize); } else { throw new org.python.exceptions.TypeError( String.format("unorderable types: list() >= %s()", Python.typeName(other.getClass()))); } }
@org.python.Method(__doc__ = "") public org.python.Object __getitem__(org.python.Object index) { try { if (index instanceof org.python.types.Slice) { org.python.types.Slice slice = (org.python.types.Slice) index; java.util.List<org.python.Object> sliced = new java.util.ArrayList<org.python.Object>(); if (slice.start == null && slice.stop == null && slice.step == null) { sliced.addAll(this.value); } else { long start; if (slice.start != null) { start = Math.min(slice.start.value, this.value.size()); } else { start = 0; } long stop; if (slice.stop != null) { stop = Math.min(slice.stop.value, this.value.size()); } else { stop = this.value.size(); } long step; if (slice.step != null) { step = slice.step.value; } else { step = 1; } for (long i = start; i < stop; i += step) { sliced.add(this.value.get((int) i)); } } return new org.python.types.List(sliced); } else { int idx = (int) ((org.python.types.Int) index).value; if (idx < 0) { if (-idx > this.value.size()) { throw new org.python.exceptions.IndexError("list index out of range"); } else { return this.value.get(this.value.size() + idx); } } else { if (idx >= this.value.size()) { throw new org.python.exceptions.IndexError("list index out of range"); } else { return this.value.get(idx); } } } } catch (ClassCastException e) { throw new org.python.exceptions.TypeError( "list indices must be integers, not " + index.typeName()); } }
public Super(org.python.Object klass, org.python.Object instance) { try { this.klass = (org.python.types.Type) klass; } catch (java.lang.ClassCastException e) { throw new org.python.exceptions.TypeError( java.lang.String.format("must be type, not %s", klass.typeName())); } this.instance = instance; }
@org.python.Method( __doc__ = "", args = {"other"}) public void __ior__(org.python.Object other) { try { this.setValue(this.__or__(other)); } catch (org.python.exceptions.TypeError e) { throw new org.python.exceptions.TypeError( "unsupported operand type(s) for |=: 'super()' and '" + other.typeName() + "'"); } }
@org.python.Method( __doc__ = "", args = {"item"}) public org.python.Object remove(org.python.Object item) { for (int i = 0; i < this.value.size(); i++) { if (((org.python.types.Bool) item.__eq__(this.value.get(i))).value) { this.value.remove(i); return org.python.types.NoneType.NONE; } } throw new org.python.exceptions.ValueError("list.remove(x): x not in list"); }
@org.python.Method(__doc__ = "") public org.python.Object __contains__(org.python.Object item) { boolean found = false; int len = (int) this.__len__().value; for (int i = 0; i < len; i++) { if (((org.python.types.Bool) item.__eq__(this.value.get(i))).value) { found = true; break; } } return new org.python.types.Bool(found); }
@org.python.Method(__doc__ = "") public org.python.Object __iadd__(org.python.Object other) { if (other instanceof org.python.types.List) { this.value.addAll(((org.python.types.List) other).value); return this; } else if (other instanceof org.python.types.Tuple) { this.value.addAll(((org.python.types.Tuple) other).value); return this; } else { throw new org.python.exceptions.TypeError( String.format("'%s' object is not iterable", Python.typeName(other.getClass()))); } }
@org.python.Method( __doc__ = "", args = {"other"}) public org.python.Object count(org.python.Object other) { int count = 0; int len = (int) this.__len__().value; for (int i = 0; i < len; i++) { if (((org.python.types.Bool) other.__eq__(this.value.get(i))).value) { count++; } } return new org.python.types.Int(count); }
@org.python.Method(__doc__ = "") public org.python.Object __add__(org.python.Object other) { if (other instanceof org.python.types.List) { org.python.types.List result = new org.python.types.List(); result.value.addAll(this.value); result.value.addAll(((org.python.types.List) other).value); return result; } else { throw new org.python.exceptions.TypeError( String.format( "can only concatenate list (not \"%s\") to list", Python.typeName(other.getClass()))); } }
public org.python.Object __getattribute_null(java.lang.String name) { // Look for local instance attributes first // org.Python.debug("SUPER GETATTRIBUTE ", name); // Look to the class for a super-proxy attribute org.python.Object value = this.klass.__getattribute_null(name + "$super"); // If no super proxy exists, check the base class. if (value == null) { // org.Python.debug("no instance attributes on super;"); // org.Python.debug(" look to super of ", this.klass); // org.Python.debug(" which is ", this.klass.__base__); value = this.klass.__base__.__getattribute_null(name); } if (value == null) { throw new org.python.exceptions.NotImplementedError("Can't get attributes on super() (yet!)"); } // org.Python.debug("SUPER GETATTRIBUTE " + name, value); // org.Python.debug("INSTANCE ", this.instance); // Post-process the value retrieved, using the binding fr return value.__get__(this.instance, this.klass); }
@org.python.Method(__doc__ = "") public org.python.Object __mul__(org.python.Object other) { if (other instanceof org.python.types.Int) { long count = ((org.python.types.Int) other).value; org.python.types.List result = new org.python.types.List(); for (long i = 0; i < count; i++) { result.value.addAll(this.value); } return result; } else if (other instanceof org.python.types.Bool) { boolean count = ((org.python.types.Bool) other).value; org.python.types.List result = new org.python.types.List(); if (count) { result.value.addAll(this.value); } return result; } throw new org.python.exceptions.TypeError( "can't multiply sequence by non-int of type '" + other.typeName() + "'"); }
@org.python.Method(__doc__ = "") public void __delitem__(org.python.Object index) { try { int idx = (int) ((org.python.types.Int) index).value; if (idx < 0) { if (-idx > this.value.size()) { throw new org.python.exceptions.IndexError("list index out of range"); } else { this.value.remove(this.value.size() + idx); } } else { if (idx >= this.value.size()) { throw new org.python.exceptions.IndexError("list index out of range"); } else { this.value.remove(idx); } } } catch (ClassCastException e) { throw new org.python.exceptions.TypeError( "list indices must be integers, not " + index.typeName()); } }
public KeyError(org.python.Object key) { super(key.__repr__().toString()); }