Skip to content

builtin: Implement enumerate feature #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func init() {
"classmethod": py.ClassMethodType,
"complex": py.ComplexType,
"dict": py.StringDictType, // FIXME
// "enumerate": py.EnumType,
"enumerate": py.EnumerateType,
// "filter": py.FilterType,
"float": py.FloatType,
"frozenset": py.FrozenSetType,
Expand Down
5 changes: 5 additions & 0 deletions builtin/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
doc="divmod"
assert divmod(34,7) == (4, 6)

doc="enumerate"
a = [3, 4, 5, 6, 7]
for idx, value in enumerate(a):
assert value == a[idx]

doc="eval"
# smoke test only - see vm/tests/builtin.py for more tests
assert eval("1+2") == 3
Expand Down
88 changes: 88 additions & 0 deletions py/enumerate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package py

// A python Enumerate object
type Enumerate struct {
Iterable Object
Start Int
}

// A python Enumerate iterator
type EnumerateIterator struct {
Enumerate
Index Int
}

var EnumerateType = NewTypeX("enumerate", `enumerate(iterable, start=0)

Return an enumerate object.`,
EnumerateNew, nil)

var EnumerateIteratorType = NewType("enumerate_iterator", `enumerate_iterator object`)

// Type of this object
func (e *Enumerate) Type() *Type {
return EnumerateType
}

// Type of this object
func (ei *EnumerateIterator) Type() *Type {
return EnumerateIteratorType
}

// EnumerateTypeNew
func EnumerateNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
var iterable Object
var start Object
err := UnpackTuple(args, kwargs, "enumerate", 1, 2, &iterable, &start)
if err != nil {
return nil, err
}

if start == nil {
start = Int(0)
}
startIndex, err := Index(start)
if err != nil {
return nil, err
}
iter, err := Iter(iterable)
if err != nil {
return nil, err
}

return &Enumerate{Iterable: iter, Start: startIndex}, nil
}

// Enumerate iterator
func (e *Enumerate) M__iter__() (Object, error) {
return &EnumerateIterator{
Enumerate: *e,
Index: e.Start,
}, nil
}

// EnumerateIterator iterator
func (ei *EnumerateIterator) M__iter__() (Object, error) {
return ei, nil
}

// EnumerateIterator iterator next
func (ei *EnumerateIterator) M__next__() (Object, error) {
value, err := Next(ei.Enumerate.Iterable)
if err != nil {
return nil, err
}
res := make(Tuple, 2)
res[0] = ei.Index
res[1] = value
ei.Index += 1
return res, nil
}

// Check interface is satisfied
var _ I__iter__ = (*Enumerate)(nil)
var _ I_iterator = (*EnumerateIterator)(nil)
8 changes: 8 additions & 0 deletions py/tests/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
assert repr([1,[2,3],4]) == "[1, [2, 3], 4]"
assert repr(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"

doc="enumerate"
a = [e for e in enumerate([3,4,5,6,7], 4)]
idxs = [4, 5, 6, 7, 8]
values = [3, 4, 5, 6, 7]
for idx, value in enumerate(values):
assert idxs[idx] == a[idx][0]
assert values[idx] == a[idx][1]

doc="finished"