Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/generators.py
1396 views
1
# vim:fileencoding=utf-8
2
# License: BSD
3
# Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
4
5
6
def g1():
7
yield 1
8
yield 2
9
10
11
def g2():
12
for i in range(2):
13
yield from g1()
14
15
16
def g3():
17
data = yield 1
18
yield data
19
20
21
class A:
22
def __init__(self):
23
self.items = [1, 2, 3]
24
25
def __iter__(self):
26
for x in self.items:
27
yield x
28
29
30
assrt.deepEqual([x for x in g1()], [1, 2])
31
assrt.deepEqual([x for x in g2()], [1, 2, 1, 2])
32
assrt.deepEqual([x for x in A()], [1, 2, 3])
33
34
g = g3()
35
assrt.equal(g.next().value, 1)
36
assrt.equal(g.next('a').value, 'a')
37
38
a = (x for x in range(3))
39
assrt.deepEqual(list(a), [0, 1, 2])
40
a = ([x, x**2] for x in range(3))
41
assrt.deepEqual(list(a), [[0, 0], [1, 1], [2, 4]])
42
assrt.deepEqual(list(x for x in range(3)), [0, 1, 2])
43
44
45
def t(a, b):
46
assrt.deepEqual(list(a), list(b))
47
48
49
t((x for x in range(1)), (y for y in range(1)))
50
51