-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyNamespace.java
More file actions
74 lines (62 loc) · 2.11 KB
/
Copy pathPyNamespace.java
File metadata and controls
74 lines (62 loc) · 2.11 KB
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
package org.python.modules;
import org.python.core.BuiltinDocs;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import java.util.HashMap;
import java.util.Map;
/**
* namespace object implementation
*/
@ExposedType(name = "SimpleNamespace", doc = BuiltinDocs.SimpleNamespace_doc)
public class PyNamespace extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyNamespace.class);
@ExposedGet(name = "__dict__")
public Map<String, PyObject> dict;
public PyNamespace(PyType subtype) {
super(subtype);
this.dict = new HashMap<>();
}
public PyNamespace(Map<String, PyObject> dict) {
super(TYPE);
this.dict = dict;
}
@ExposedNew
@ExposedMethod(doc = BuiltinDocs.SimpleNamespace___init___doc)
final void SimpleNamespace___init__(PyObject[] args, String[] kwds) {
}
@Override
public String toString() {
StringBuilder items = new StringBuilder("namespace(");
boolean first = true;
for (String key : dict.keySet()) {
if (!first) {
items.append(", ");
} else {
first = false;
}
items.append(key).append("=").append(dict.get(key));
}
return items.append(")").toString();
}
final PyObject SimpleNamespace___eq__(PyObject other) {
return Py.newBoolean(dict.equals(other.__getattr__("__dict__")));
}
@ExposedMethod(doc = BuiltinDocs.SimpleNamespace___getattribute___doc)
final PyObject SimpleNamespace___getattribute__(PyObject name) {
return dict.get(name.asString());
}
@ExposedMethod(doc = BuiltinDocs.SimpleNamespace___setattr___doc)
final PyObject SimpleNamespace___setattr__(PyObject name, PyObject value) {
dict.put(name.asString(), value);
return Py.None;
}
@Override
public void __setattr__(String name, PyObject value) {
dict.put(name, value);
}
}