| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,7 +74,7 @@ func init() { | |||
| 74 | 74 | "classmethod": py.ClassMethodType, | |
| 75 | 75 | "complex": py.ComplexType, | |
| 76 | 76 | "dict": py.StringDictType, // FIXME | |
| 77 | - // "enumerate": py.EnumType, | ||
| 77 | + "enumerate": py.EnumerateType, | ||
| 78 | 78 | // "filter": py.FilterType, | |
| 79 | 79 | "float": py.FloatType, | |
| 80 | 80 | "frozenset": py.FrozenSetType, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,14 @@ | |||
| 32 | 32 | doc="divmod" | |
| 33 | 33 | assert divmod(34,7) == (4, 6) | |
| 34 | 34 | ||
| 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 | + | ||
| 35 | 43 | doc="eval" | |
| 36 | 44 | # smoke test only - see vm/tests/builtin.py for more tests | |
| 37 | 45 | assert eval("1+2") == 3 | |
| Original file line number | Diff line number | Diff 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) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,4 +14,12 @@ | |||
| 14 | 14 | assert repr([1,[2,3],4]) == "[1, [2, 3], 4]" | |
| 15 | 15 | assert repr(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]" | |
| 16 | 16 | ||
| 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 | + | ||
| 17 | 25 | doc="finished" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments