-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerators.py
68 lines (57 loc) · 1.98 KB
/
Generators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'''
Created on 21 mar 2014
@author: Dominik
'''
class Generators(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
'''
1) Generator is basically object that is iterable
2) yield keyword returns new loop position in it
3) it can be iterable ONE TIME PER GENERATOR INSTANCE
4) if you want to iterate over it again
you have to create new generator object
'''
def makeSquaresGenerator(self, rangeNum):
for x in range(rangeNum):
z = x**2
yield z
# this will be invoked after yield
# but REMEMBER, following +=1 won't change returned value
# because only yield statement can return value from loop
# and any statement after yield won't affect returned value
z += 1
print "--right after yield z is ", z
# following code will be invoked after generator object
# has no items to iterate over
# but you shouldn't add any code after yield,
# because if generator loop is broken, print won't be invoked
print "---code after generator loop---"
def testGenerator(self):
list1 = []
for num in self.makeSquaresGenerator(5):
list1.append(num)
return list1
# infinite iterator
# cycle('ABCD') --> A B C D A B C D A B C D ...
# implementation comes from:
# http://docs.python.org/2/library/itertools.html#itertools.cycle
def cycle(self,iterable):
saved = []
for element in iterable:
yield element
saved.append(element)
# added to see that saved is only filled once
print saved
while saved:
# while saved variable is still set
# elements are looped here over and over
for element in saved:
yield element
obj = Generators()
print obj.testGenerator() # [0, 1, 4, 9, 16]