-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
65 lines (51 loc) · 1.32 KB
/
list.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"
import "errors"
// ErrCouldNotInsert could not insert item to list
var ErrCouldNotInsert = errors.New("could not insert item to list")
// PyList represents a python list
type PyList struct {
PyObject
}
// NewList returns a new python list
func NewList(length int) *PyList {
ptr := C.PyList_New(C.long(length))
if ptr != nil {
return &PyList{PyObject{ptr}}
}
return nil
}
// Insert inserts an item to a specified index
func (list *PyList) Insert(index int, obj *PyObject) error {
if C.PyList_Insert(list.ptr, C.long(index), obj.ptr) == -1 {
return ErrCouldNotInsert
}
return nil
}
// Append appends an item to the list
func (list *PyList) Append(obj *PyObject) error {
if C.PyList_Append(list.ptr, obj.ptr) == -1 {
return ErrCouldNotInsert
}
return nil
}
// Size returns the size of the list
func (list *PyList) Size() int {
return int(C.PyList_Size(list.ptr))
}
// GetItem returns an item from the list
func (list *PyList) GetItem(index int) *PyObject {
ptr := C.PyList_GetItem(list.ptr, C.long(index))
if ptr != nil {
return &PyObject{ptr}
}
return nil
}
// SetItem set item into given index
func (list *PyList) SetItem(index int, obj *PyObject) error {
if C.PyList_SetItem(list.ptr, C.long(index), obj.ptr) == -1 {
return ErrCouldNotInsert
}
return nil
}