Skip to content

Commit cfd27cb

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

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
doc="divmod"
3333
assert divmod(34,7) == (4, 6)
3434

35+
doc="enumerate"
36+
a = [e for e in enumerate([3,4,5,6,7], 4)]
37+
idxs = [4, 5, 6, 7, 8]
38+
values = [3, 4, 5, 6, 7]
39+
for idx, value in enumerate(values):
40+
assert idxs[idx] == a[idx][0]
41+
assert values[idx] == a[idx][1]
42+
3543
doc="eval"
3644
# smoke test only - see vm/tests/builtin.py for more tests
3745
assert eval("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+
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 (o *Enumerate) Type() *Type {
28+
return EnumerateType
29+
}
30+
31+
// Type of this object
32+
func (o *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 (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+
return it, nil
71+
}
72+
73+
// EnumerateIterator iterator next
74+
func (iter *EnumerateIterator) M__next__() (Object, error) {
75+
value, err := Next(iter.Enumerate.Iterable)
76+
if err != nil {
77+
return nil, err
78+
}
79+
res := make(Tuple, 2)
80+
res[0] = iter.Index
81+
res[1] = value
82+
iter.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

Lines changed: 8 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy