Skip to content

Commit 0f75714

Browse files
committed
builtin: Implement enumerate feature
Now, gpython supports enumerate feature
1 parent f7ea0a4 commit 0f75714

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

‎builtin/builtin.go‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
doc="divmod"
3333
assertdivmod(34,7) == (4, 6)
3434

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

‎py/enumerate.go‎

Lines changed: 88 additions & 0 deletions
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+
typeEnumeratestruct{
9+
IterableObject
10+
StartInt
11+
}
12+
13+
// A python Enumerate iterator
14+
typeEnumerateIteratorstruct{
15+
Enumerate
16+
IndexInt
17+
}
18+
19+
varEnumerateType=NewTypeX("enumerate", `enumerate(iterable, start=0)
20+
21+
Return an enumerate object.`,
22+
EnumerateNew, nil)
23+
24+
varEnumerateIteratorType=NewType("enumerate_iterator", `enumerate_iterator object`)
25+
26+
// Type of this object
27+
func (o*Enumerate) Type() *Type{
28+
returnEnumerateType
29+
}
30+
31+
// Type of this object
32+
func (o*EnumerateIterator) Type() *Type{
33+
returnEnumerateIteratorType
34+
}
35+
36+
// EnumerateTypeNew
37+
funcEnumerateNew(metatype*Type, argsTuple, kwargsStringDict) (Object, error){
38+
variterableObject
39+
varstartObject
40+
err:=UnpackTuple(args, kwargs, "enumerate", 1, 2, &iterable, &start)
41+
iferr!=nil{
42+
returnnil, err
43+
}
44+
45+
ifstart==nil{
46+
start=Int(0)
47+
}
48+
startIndex, err:=Index(start)
49+
iferr!=nil{
50+
returnnil, err
51+
}
52+
iter, err:=Iter(iterable)
53+
iferr!=nil{
54+
returnnil, err
55+
}
56+
57+
return&Enumerate{Iterable: iter, Start: startIndex}, nil
58+
}
59+
60+
// Enumerate iterator
61+
func (it*Enumerate) M__iter__() (Object, error){
62+
return&EnumerateIterator{
63+
Enumerate: *it,
64+
Index: it.Start,
65+
}, nil
66+
}
67+
68+
// EnumerateIterator iterator
69+
func (it*EnumerateIterator) M__iter__() (Object, error){
70+
returnit, nil
71+
}
72+
73+
// EnumerateIterator iterator next
74+
func (iter*EnumerateIterator) M__next__() (Object, error){
75+
value, err:=Next(iter.Enumerate.Iterable)
76+
iferr!=nil{
77+
returnnil, err
78+
}
79+
res:=make(Tuple, 2)
80+
res[0] =iter.Index
81+
res[1] =value
82+
iter.Index+=1
83+
returnres, nil
84+
}
85+
86+
// Check interface is satisfied
87+
var_I__iter__= (*Enumerate)(nil)
88+
var_I_iterator= (*EnumerateIterator)(nil)

‎py/tests/list.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@
1414
assertrepr([1,[2,3],4]) =="[1, [2, 3], 4]"
1515
assertrepr(["1",[2.5,17,[]]]) =="['1', [2.5, 17, []]]"
1616

17+
doc="enumerate"
18+
a= [eforeinenumerate([3,4,5,6,7], 4)]
19+
idxs= [4, 5, 6, 7, 8]
20+
values= [3, 4, 5, 6, 7]
21+
foridx, valueinenumerate(values):
22+
assertidxs[idx] ==a[idx][0]
23+
assertvalues[idx] ==a[idx][1]
24+
1725
doc="finished"

0 commit comments

Comments
(0)