This repository has been archived by the owner on May 31, 2020. It is now read-only.
/
DictKeys.java
254 lines (224 loc) · 8.67 KB
/
DictKeys.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package org.python.types;
public class DictKeys extends org.python.types.FrozenSet {
static {
org.python.types.Type.declarePythonType(DictKeys.class,"dict_keys",null,null);
}
/**
* 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.DictKeys) obj).value;
}
@Override
public org.python.Object __hash__() {
throw new org.python.exceptions.AttributeError(this, "__hash__");
}
public DictKeys(org.python.types.Dict dict) {
this.value = dict.value.keySet();
}
/**
* A utility method to create a set from an iterable object
* <p>
* Used by the __and__,__or__,__sub__ and __xor__ operations
* obj must have a valid iterator
*/
private java.util.Set<org.python.Object> fromIter(org.python.Object obj) {
java.util.Set<org.python.Object> generated = new java.util.HashSet<org.python.Object>();
org.python.Object iterator = org.Python.iter(obj);
try {
while (true) {
org.python.Object next = iterator.__next__();
generated.add(next);
}
} catch (org.python.exceptions.StopIteration si) {
}
return generated;
}
@org.python.Method(
__doc__ = "Return repr(self)."
)
public org.python.types.Str __repr__() {
java.lang.StringBuilder buffer = new java.lang.StringBuilder("dict_keys([");
boolean first = true;
for(org.python.Object item : this.value) {
if (first) {
first = false;
} else {
buffer.append(", ");
}
buffer.append(String.format("%s",item.__repr__()));
}
buffer.append("])");
return new org.python.types.Str(buffer.toString());
}
@org.python.Method(
__doc__ = "default object formatter"
)
public org.python.types.Str __format__(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("dict_keys.__format__() has not been implemented.");
}
@org.python.Method(
__doc__ = "__dir__() -> list\ndefault dir() implementation"
)
public org.python.types.List __dir__() {
throw new org.python.exceptions.NotImplementedError("dict_keys.__dir__() has not been implemented.");
}
public boolean __setattr_null(java.lang.String name, org.python.Object value) {
// Builtin types can't have attributes set on them.
return false;
}
public boolean __delattr_null(java.lang.String name) {
// Can't delete attributes of Builtin types
return false;
}
@org.python.Method(
__doc__ = "",
args = {"index"}
)
public org.python.Object __getitem__(org.python.Object index) {
if (index instanceof org.python.types.Int || index instanceof org.python.types.Bool) {
throw new org.python.exceptions.TypeError("'dict_keys' object does not support indexing");
} else {
throw new org.python.exceptions.TypeError("'dict_keys' object is not subscriptable");
}
}
@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object item, org.python.Object value) {
throw new org.python.exceptions.TypeError("'dict_keys' object does not support item assignment");
}
@org.python.Method(
__doc__ = "",
args = {"index"}
)
public void __delitem__(org.python.Object item) {
throw new org.python.exceptions.TypeError("'dict_keys' object doesn't support item deletion");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __invert__() {
throw new org.python.exceptions.TypeError("bad operand type for unary ~: 'dict_keys'");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __pos__() {
throw new org.python.exceptions.TypeError("bad operand type for unary +: 'dict_keys'");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __neg__() {
throw new org.python.exceptions.TypeError("bad operand type for unary -: 'dict_keys'");
}
@org.python.Method(
__doc__ = "Return self&value."
)
public org.python.types.Set __and__(org.python.Object other) {
java.util.Set<org.python.Object> generated = this.fromIter(other);
generated.retainAll(this.value);
return new org.python.types.Set(generated);
}
@org.python.Method(
__doc__ = "Return self|value."
)
public org.python.types.Set __or__(org.python.Object other) {
java.util.Set<org.python.Object> generated = this.fromIter(other);
generated.addAll(this.value);
return new org.python.types.Set(generated);
}
@org.python.Method(
__doc__ = "Return self-value."
)
public org.python.types.Set __sub__(org.python.Object other) {
java.util.Set<org.python.Object> set1 = new java.util.HashSet<org.python.Object>(this.value);
java.util.Set<org.python.Object> set2 = this.fromIter(other);
set1.removeAll(set2);
return new org.python.types.Set(set1);
}
@org.python.Method(
__doc__ = "Return self^value."
)
public org.python.types.Set __xor__(org.python.Object other) {
java.util.Set<org.python.Object> s1 = this.fromIter(other);
java.util.Set<org.python.Object> sym_dif = new java.util.HashSet<org.python.Object>(s1);
sym_dif.addAll(this.value);
java.util.Set<org.python.Object> tmp = new java.util.HashSet<org.python.Object>(s1);
tmp.retainAll(this.value);
sym_dif.removeAll(tmp);
return new org.python.types.Set(sym_dif);
}
@org.python.Method(
__doc__ = "Return value&self."
)
public org.python.types.Set __rand__(org.python.Object other) {
return this.__and__(other);
}
@org.python.Method(
__doc__ = "Return value|self."
)
public org.python.types.Set __ror__(org.python.Object other) {
return this.__or__(other);
}
@org.python.Method(
__doc__ = "Return value^self."
)
public org.python.types.Set __rxor__(org.python.Object other) {
return this.__xor__(other);
}
@org.python.Method(
__doc__ = "Return value-self."
)
public org.python.types.Set __rsub__(org.python.Object other) {
java.util.Set<org.python.Object> generated = this.fromIter(other);
generated.removeAll(this.value);
return new org.python.types.Set(generated);
}
@org.python.Method(
__doc__ = "Return True if the set has no elements in common with other. Sets are\n" +
"disjoint if and only if their intersection is the empty set.",
args = {"other"}
)
public org.python.Object isdisjoint(org.python.Object other) {
java.util.Set<org.python.Object> generated = new java.util.HashSet<org.python.Object>();
org.python.Object iterator = org.Python.iter(other);
try {
while (true) {
org.python.Object next = iterator.__next__();
generated.add(next);
}
} catch (org.python.exceptions.StopIteration si) {
}
generated.retainAll(this.value);
return org.python.types.Bool.getBool(generated.size() > 0);
}
/**
* The following methods are not present in Python's dict_keys but are present in DictKeys (inherited from FrozenSet)
* <p>
* Hence, throw an AttributeError every time any of these methods is called
*/
public org.python.Object issubset(org.python.Object other) {
throw new org.python.exceptions.AttributeError(this,"issubset");
}
public org.python.Object issuperset(org.python.Object other) {
throw new org.python.exceptions.AttributeError(this,"issuperset");
}
public org.python.Object union(org.python.types.Tuple others) {
throw new org.python.exceptions.AttributeError(this,"union");
}
public org.python.Object intersection(org.python.types.Tuple others) {
throw new org.python.exceptions.AttributeError(this,"intersection");
}
public org.python.Object difference(org.python.types.Tuple others) {
throw new org.python.exceptions.AttributeError(this,"difference");
}
public org.python.Object symmetric_difference(org.python.Object other) {
throw new org.python.exceptions.AttributeError(this,"issuperset");
}
}