Skip to content

Commit d8a0825

Browse files
corona10ncw
authored andcommitted
py: Support zip builtin feature
1 parent c140988 commit d8a0825

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

builtin/builtin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func init() {
9292
// "super": py.SuperType,
9393
"tuple": py.TupleType,
9494
"type": py.TypeType,
95-
// "zip": py.ZipType,
95+
"zip": py.ZipType,
9696

9797
// Exceptions
9898
"ArithmeticError": py.ArithmeticError,

builtin/tests/builtin.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@ class C: pass
232232
ok = True
233233
assert ok, "TypeError not raised"
234234

235+
doc="zip"
236+
ok = False
237+
a = [3, 4, 5, 6, 7]
238+
b = [8, 9, 10, 11, 12]
239+
assert [e for e in zip(a, b)] == [(3,8), (4,9), (5,10), (6,11), (7,12)]
240+
try:
241+
zip(1,2,3)
242+
except TypeError as e:
243+
print(e.args[0])
244+
if e.args[0] != "zip argument #1 must support iteration":
245+
raise
246+
ok = True
247+
assert ok, "TypeError not raised"
248+
235249
doc="__import__"
236250
lib = __import__("lib")
237251
assert lib.libfn() == 42

py/tests/zip.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
doc="zip"
6+
a = [1, 2, 3, 4, 5]
7+
b = [10, 11, 12]
8+
c = [e for e in zip(a, b)]
9+
assert len(c) == 3
10+
for idx, e in enumerate(c):
11+
assert a[idx] == c[idx][0]
12+
assert b[idx] == c[idx][1]
13+
doc="finished"

py/zip.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 Zip object
8+
type Zip struct {
9+
itTuple Tuple
10+
size int
11+
}
12+
13+
// A python ZipIterator iterator
14+
type ZipIterator struct {
15+
zip Zip
16+
}
17+
18+
var ZipType = NewTypeX("zip", `zip(iter1 [,iter2 [...]]) --> zip object
19+
20+
Return a zip object whose .__next__() method returns a tuple where
21+
the i-th element comes from the i-th iterable argument. The .__next__()
22+
method continues until the shortest iterable in the argument sequence
23+
is exhausted and then it raises StopIteration.`,
24+
ZipTypeNew, nil)
25+
26+
// Type of this object
27+
func (z *Zip) Type() *Type {
28+
return ZipType
29+
}
30+
31+
// ZipTypeNew
32+
func ZipTypeNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
33+
tupleSize := len(args)
34+
itTuple := make(Tuple, tupleSize)
35+
for i := 0; i < tupleSize; i++ {
36+
item := args[i]
37+
iter, err := Iter(item)
38+
if err != nil {
39+
return nil, ExceptionNewf(TypeError, "zip argument #%d must support iteration", i+1)
40+
}
41+
itTuple[i] = iter
42+
}
43+
44+
return &Zip{itTuple: itTuple, size: tupleSize}, nil
45+
}
46+
47+
// Zip iterator
48+
func (z *Zip) M__iter__() (Object, error) {
49+
return z, nil
50+
}
51+
52+
func (z *Zip) M__next__() (Object, error) {
53+
result := make(Tuple, z.size)
54+
for i := 0; i < z.size; i++ {
55+
value, err := Next(z.itTuple[i])
56+
if err != nil {
57+
return nil, err
58+
}
59+
result[i] = value
60+
}
61+
return result, nil
62+
}
63+
64+
// Check interface is satisfied
65+
var _ I__iter__ = (*Zip)(nil)
66+
var _ I__next__ = (*Zip)(nil)

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