Skip to content

Commit 5e97b9b

Browse files
corona10ncw
authored andcommitted
builtin: Implement enumerate feature
Now, gpython supports enumerate feature
1 parent 82240ed commit 5e97b9b

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

builtin/builtin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func init() {
7474
"classmethod": py.ClassMethodType,
7575
"complex": py.ComplexType,
7676
"dict": py.StringDictType, // FIXME
77-
// "enumerate": py.EnumType,
77+
"enumerate": py.EnumerateType,
7878
// "filter": py.FilterType,
7979
"float": py.FloatType,
8080
"frozenset": py.FrozenSetType,

builtin/tests/builtin.py

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
doc="divmod"
3333
assert divmod(34,7) == (4, 6)
3434

35+
doc="enumerate"
36+
a = [3, 4, 5, 6, 7]
37+
for idx, value in enumerate(a):
38+
assert value == a[idx]
39+
3540
doc="eval"
3641
# smoke test only - see vm/tests/builtin.py for more tests
3742
assert eval("1+2") == 3

py/enumerate.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2018 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package py
6+
7+
// A python Enumerate object
8+
type Enumerate struct {
9+
Iterable Object
10+
Start Int
11+
}
12+
13+
// A python Enumerate iterator
14+
type EnumerateIterator struct {
15+
Enumerate
16+
Index Int
17+
}
18+
19+
var EnumerateType = NewTypeX("enumerate", `enumerate(iterable, start=0)
20+
21+
Return an enumerate object.`,
22+
EnumerateNew, nil)
23+
24+
var EnumerateIteratorType = NewType("enumerate_iterator", `enumerate_iterator object`)
25+
26+
// Type of this object
27+
func (e *Enumerate) Type() *Type {
28+
return EnumerateType
29+
}
30+
31+
// Type of this object
32+
func (ei *EnumerateIterator) Type() *Type {
33+
return EnumerateIteratorType
34+
}
35+
36+
// EnumerateTypeNew
37+
func EnumerateNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
38+
var iterable Object
39+
var start Object
40+
err := UnpackTuple(args, kwargs, "enumerate", 1, 2, &iterable, &start)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
if start == nil {
46+
start = Int(0)
47+
}
48+
startIndex, err := Index(start)
49+
if err != nil {
50+
return nil, err
51+
}
52+
iter, err := Iter(iterable)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
return &Enumerate{Iterable: iter, Start: startIndex}, nil
58+
}
59+
60+
// Enumerate iterator
61+
func (e *Enumerate) M__iter__() (Object, error) {
62+
return &EnumerateIterator{
63+
Enumerate: *e,
64+
Index: e.Start,
65+
}, nil
66+
}
67+
68+
// EnumerateIterator iterator
69+
func (ei *EnumerateIterator) M__iter__() (Object, error) {
70+
return ei, nil
71+
}
72+
73+
// EnumerateIterator iterator next
74+
func (ei *EnumerateIterator) M__next__() (Object, error) {
75+
value, err := Next(ei.Enumerate.Iterable)
76+
if err != nil {
77+
return nil, err
78+
}
79+
res := make(Tuple, 2)
80+
res[0] = ei.Index
81+
res[1] = value
82+
ei.Index += 1
83+
return res, nil
84+
}
85+
86+
// Check interface is satisfied
87+
var _ I__iter__ = (*Enumerate)(nil)
88+
var _ I_iterator = (*EnumerateIterator)(nil)

py/tests/list.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@
1414
assert repr([1,[2,3],4]) == "[1, [2, 3], 4]"
1515
assert repr(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"
1616

17+
doc="enumerate"
18+
a = [e for e in enumerate([3,4,5,6,7], 4)]
19+
idxs = [4, 5, 6, 7, 8]
20+
values = [3, 4, 5, 6, 7]
21+
for idx, value in enumerate(values):
22+
assert idxs[idx] == a[idx][0]
23+
assert values[idx] == a[idx][1]
24+
1725
doc="finished"

0 commit comments

Comments
 (0)