-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.go
65 lines (50 loc) · 1.3 KB
/
dict.go
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
package python
// #include <Python.h>
import "C"
type Dict C.PyObject
func NewDict() *Dict {
return (*Dict)(C.PyDict_New())
}
func (dict *Dict) C() CPyObject {
return dict.Object().C()
}
func (dict *Dict) Object() *Object {
return (*Object)(dict)
}
func (dict *Dict) SetItem(key interface{}, value interface{}) int {
return int(C.PyDict_SetItem(dict.C(), toC(key), toC(value)))
}
func (dict *Dict) String() string {
return dict.Object().String()
}
func (dict *Dict) Interface() interface{} {
return dict.Object().Interface()
}
func (dict *Dict) Clear() {
C.PyDict_Clear(dict.C())
}
func (dict *Dict) DelItem(key interface{}) int {
return int(C.PyDict_DelItem(dict.C(), toC(key)))
}
func (dict *Dict) Copy() *Dict {
return (*Dict)(C.PyDict_Copy(dict.C()))
}
func (dict *Dict) GetItem(key interface{}) *Object {
return (*Object)(C.PyDict_GetItem(dict.C(), toC(key)))
}
func (dict *Dict) Size() int {
return int(C.PyDict_Size(dict.C()))
}
func (dict *Dict) Items() *List {
return (*List)(C.PyDict_Items(dict.C()))
}
func (dict *Dict) Map() map[interface{}]interface{} {
m := make(map[interface{}]interface{})
items := dict.Items()
for i := 0; i < items.Size(); i++ {
item := items.GetItem(i).AsTuple()
key, value := item.GetItem(0).Interface(), item.GetItem(1).Interface()
m[key] = value
}
return m
}