Skip to content

Commit

Permalink
Merge branch 'sets' of https://github.com/Saurabh--Kumar/voc into sets
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabh--Kumar committed Apr 19, 2017
2 parents 257e9a4 + ee0dc95 commit eef7b41
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 142 deletions.
60 changes: 57 additions & 3 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ public static org.python.Object max(org.python.types.Tuple args, org.python.Obje
value = iter.__next__();
if (key == null) {
key_value = value;

} else {
throw new org.python.exceptions.NotImplementedError("Keyword argument 'key' for builtin function 'max' not implemented");
}
Expand Down Expand Up @@ -1707,9 +1708,62 @@ public static org.python.types.Type type(org.python.Object object_or_name, org.p
"Return a zip object whose .__next__() method returns a tuple where\n" +
"the i-th element comes from the i-th iterable argument. The .__next__()\n" +
"method continues until the shortest iterable in the argument sequence\n" +
"is exhausted and then it raises StopIteration.\n"
"is exhausted and then it raises StopIteration.\n",

args = {"item"},
varargs = "moreItems"
)
public static org.python.Object zip() {
throw new org.python.exceptions.NotImplementedError("Builtin function 'zip' not implemented");
public static org.python.Object zip(org.python.Object item, org.python.types.Tuple moreItems) {
java.util.List result = new java.util.ArrayList();
int count = 0;
if (item != null) {
count = 1;
org.python.Iterable iter;
try {
iter = org.Python.iter(item);
} catch (org.python.exceptions.TypeError e) {
throw new org.python.exceptions.TypeError("zip argument #" + count + " must support iteration");
}
java.util.List<org.python.Iterable> iters = new java.util.ArrayList<org.python.Iterable>();
iters.add(iter);
if (moreItems != null) {
org.python.Iterable tupIter = org.Python.iter(moreItems);
while (true) {
count++;
org.python.Iterable it;
org.python.Object obj;
try {
obj = tupIter.__next__();
} catch (org.python.exceptions.StopIteration e) {
break;
}
try {
it = org.Python.iter(obj);
} catch (org.python.exceptions.TypeError e) {
throw new org.python.exceptions.TypeError("zip argument #" + count + " must support iteration");
}
iters.add(it);
}
}
boolean flag = false;
while (true) {
java.util.List tuple = new java.util.ArrayList();
for (int i = 0; i < count - 1; i++) {
try {
org.python.Iterable it = iters.get(i);
tuple.add(it.__next__());
} catch (IndexOutOfBoundsException | org.python.exceptions.StopIteration e) {
flag = true;
break;
}
}
if (flag) {
break;
}
org.python.types.Tuple pythonTuple = new org.python.types.Tuple(tuple);
result.add(pythonTuple);
}
}
return new org.python.types.List(result);
}
}
117 changes: 117 additions & 0 deletions python/common/org/python/types/FrozenSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
public class FrozenSet extends org.python.types.Object {
public java.util.Set<org.python.Object> value;

/**
* A utility method to update the internal value of this object.
*
* Used by __i*__ operations to do an in-place operation.
* obj must be of type org.python.types.FrozenSet
*/
void setValue(org.python.Object obj) {
this.value = ((org.python.types.FrozenSet) obj).value;
}

public java.lang.Object toJava() {
return this.value;
}

public int hashCode() {
return this.value.hashCode();
}

public FrozenSet() {
super();
this.value = java.util.Collections.emptySet();
}

public FrozenSet(java.util.Set<org.python.Object> frozenSet) {
super();
this.value = java.util.Collections.unmodifiableSet(frozenSet);
}

@org.python.Method(
__doc__ = "frozenset() -> empty frozenset object" +
"frozenset(iterable) -> frozenset object\n" +
Expand Down Expand Up @@ -206,4 +234,93 @@ public org.python.Object __ge__(org.python.Object other) {
}
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}


@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __sub__(org.python.Object other) {
java.util.Set frozenSet = new java.util.HashSet<org.python.Object>(this.value);
if (other instanceof org.python.types.FrozenSet) {
frozenSet.removeAll(((org.python.types.FrozenSet) other).value);
return new org.python.types.FrozenSet(frozenSet);
} else if (other instanceof org.python.types.Set) {
frozenSet.removeAll(((org.python.types.Set) other).value);
return new org.python.types.FrozenSet(frozenSet);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for -: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __mul__(org.python.Object other) {
if (other instanceof org.python.types.List ||
other instanceof org.python.types.Tuple ||
other instanceof org.python.types.Str ||
other instanceof org.python.types.Bytes ||
other instanceof org.python.types.ByteArray
) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type '" + this.typeName() + "'");
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __and__(org.python.Object other) {
java.util.Set frozenSet = new java.util.HashSet<org.python.Object>(this.value);
if (other instanceof org.python.types.FrozenSet) {
frozenSet.retainAll(((org.python.types.FrozenSet) other).value);
return new org.python.types.FrozenSet(frozenSet);
} else if (other instanceof org.python.types.Set) {
frozenSet.retainAll(((org.python.types.Set) other).value);
return new org.python.types.FrozenSet(frozenSet);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for &: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __or__(org.python.Object other) {
java.util.Set frozenSet = new java.util.HashSet<org.python.Object>(this.value);
if (other instanceof org.python.types.FrozenSet) {
frozenSet.addAll(((org.python.types.FrozenSet) other).value);
return new org.python.types.FrozenSet(frozenSet);
} else if (other instanceof org.python.types.Set) {
frozenSet.addAll(((org.python.types.Set) other).value);
return new org.python.types.FrozenSet(frozenSet);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for |: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __xor__(org.python.Object other) {
java.util.Set frozenSet = new java.util.HashSet<org.python.Object>(this.value);
if (other instanceof org.python.types.FrozenSet) {
java.util.Set otherFrozenSet = ((org.python.types.FrozenSet) other).value;
frozenSet.addAll(otherFrozenSet);
java.util.Set temp = new java.util.HashSet<org.python.Object>(this.value);
temp.retainAll(otherFrozenSet);
frozenSet.removeAll(temp);
return new org.python.types.FrozenSet(frozenSet);
} else if (other instanceof org.python.types.Set) {
java.util.Set otherFrozenSet = ((org.python.types.Set) other).value;
frozenSet.addAll(otherFrozenSet);
java.util.Set temp = new java.util.HashSet<org.python.Object>(this.value);
temp.retainAll(otherFrozenSet);
frozenSet.removeAll(temp);
return new org.python.types.FrozenSet(frozenSet);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for ^: '" + this.typeName() + "' and '" + other.typeName() + "'");
}
}
103 changes: 79 additions & 24 deletions python/common/org/python/types/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public org.python.Object __lt__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(otherSet.value.containsAll(this.value) && !this.value.equals(otherSet.value));
} else if (other instanceof org.python.types.FrozenSet) {
org.python.types.FrozenSet otherSet = (org.python.types.FrozenSet) other;
return new org.python.types.Bool(otherSet.value.containsAll(this.value) && !this.value.equals(otherSet.value));
}
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
Expand All @@ -144,6 +147,9 @@ public org.python.Object __le__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(otherSet.value.containsAll(this.value));
} else if (other instanceof org.python.types.FrozenSet) {
org.python.types.FrozenSet otherSet = (org.python.types.FrozenSet) other;
return new org.python.types.Bool(otherSet.value.containsAll(this.value));
}
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
Expand All @@ -156,6 +162,9 @@ public org.python.Object __eq__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(this.value.equals(otherSet.value));
} else if (other instanceof org.python.types.FrozenSet) {
org.python.types.FrozenSet otherSet = (org.python.types.FrozenSet) other;
return new org.python.types.Bool(this.value.equals(otherSet.value));
}
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
Expand All @@ -168,6 +177,9 @@ public org.python.Object __gt__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(this.value.containsAll(otherSet.value) && !this.value.equals(otherSet.value));
} else if (other instanceof org.python.types.FrozenSet) {
org.python.types.FrozenSet otherSet = (org.python.types.FrozenSet) other;
return new org.python.types.Bool(this.value.containsAll(otherSet.value) && !this.value.equals(otherSet.value));
}
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
Expand All @@ -180,6 +192,9 @@ public org.python.Object __ge__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(this.value.containsAll(otherSet.value));
} else if (other instanceof org.python.types.FrozenSet) {
org.python.types.FrozenSet otherSet = (org.python.types.FrozenSet) other;
return new org.python.types.Bool(this.value.containsAll(otherSet.value));
}
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
Expand Down Expand Up @@ -268,19 +283,35 @@ public org.python.Object __mul__(org.python.Object other) {
return super.__mul__(other);
}

// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __sub__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__sub__() has not been implemented");
// }
@org.python.Method(
__doc__ = ""
)
public org.python.Object __sub__(org.python.Object other) {
java.util.Set set = ((org.python.types.Set) this.copy()).value;
if (other instanceof org.python.types.Set) {
set.removeAll(((org.python.types.Set) other).value);
return new org.python.types.Set(set);
} else if (other instanceof org.python.types.FrozenSet) {
set.removeAll(((org.python.types.FrozenSet) other).value);
return new org.python.types.Set(set);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for -: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __and__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__and__() has not been implemented");
// }
@org.python.Method(
__doc__ = ""
)
public org.python.Object __and__(org.python.Object other) {
java.util.Set set = ((org.python.types.Set) this.copy()).value;
if (other instanceof org.python.types.Set) {
set.retainAll(((org.python.types.Set) other).value);
return new org.python.types.Set(set);
} else if (other instanceof org.python.types.FrozenSet) {
set.retainAll(((org.python.types.FrozenSet) other).value);
return new org.python.types.Set(set);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for &: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

// @org.python.Method(
// __doc__ = ""
Expand All @@ -289,12 +320,20 @@ public org.python.Object __mul__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__xor__() has not been implemented");
// }

// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __or__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__or__() has not been implemented");
// }
@org.python.Method(
__doc__ = ""
)
public org.python.Object __or__(org.python.Object other) {
java.util.Set set = ((org.python.types.Set) this.copy()).value;
if (other instanceof org.python.types.Set) {
set.addAll(((org.python.types.Set) other).value);
return new org.python.types.Set(set);
} else if (other instanceof org.python.types.FrozenSet) {
set.addAll(((org.python.types.FrozenSet) other).value);
return new org.python.types.Set(set);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for |: '" + this.typeName() + "' and '" + other.typeName() + "'");
}

// @org.python.Method(
// __doc__ = ""
Expand Down Expand Up @@ -381,9 +420,17 @@ public org.python.Object copy() {
args = {"other"}
)
public org.python.Object difference(org.python.Object other) {
java.util.Set set = ((Set) this.copy()).value;
set.removeAll(((Set) other).value);
return new Set(set);
try {
org.python.types.Set otherSet = null;
if (other instanceof org.python.types.Set) {
otherSet = (org.python.types.Set) other;
} else {
otherSet = new org.python.types.Set(new org.python.Object[] {other}, null);
}
return this.__sub__(otherSet);
} catch (org.python.exceptions.AttributeError e) {
throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable");
}
}

@org.python.Method(
Expand Down Expand Up @@ -416,9 +463,17 @@ public org.python.Object __iadd__(org.python.Object other) {
args = {"other"}
)
public org.python.Object intersection(org.python.Object other) {
java.util.Set set = ((Set) this.copy()).value;
set.retainAll(((Set) other).value);
return new Set(set);
try {
org.python.types.Set otherSet = null;
if (other instanceof org.python.types.Set) {
otherSet = (org.python.types.Set) other;
} else {
otherSet = new org.python.types.Set(new org.python.Object[] {other}, null);
}
return this.__and__(otherSet);
} catch (org.python.exceptions.AttributeError e) {
throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable");
}
}

@org.python.Method(
Expand Down
Loading

0 comments on commit eef7b41

Please sign in to comment.