-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator_sample.py
More file actions
159 lines (128 loc) · 3.5 KB
/
iterator_sample.py
File metadata and controls
159 lines (128 loc) · 3.5 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
"""
Simple iterator examples
"""
class IterateMe_1(object):
"""
About as simple an iterator as you can get:
returns the sequence of numbers from zero to 4
( like range(4) )
"""
def __init__(self, stop=5):
self.current = 0
self.stop = stop
def __iter__(self):
return self
def next(self):
if self.current < self.stop:
self.current += 1
return self.current
else:
raise StopIteration
class IterateMe_2(object):
"""
Almost a replacement for xrange:
Iterate_2 (start, stop, step=1)
returns the sequence of numbers from start (inclusive) to stop (exclusive),
skipping every step number
( like xrange(start, stop, step) )
"""
def __init__(self, start, stop, step=1):
self.current = start
self.stop = stop
self.step = step
def __iter__(self):
return self
def next(self):
if self.current < self.stop:
self.current += self.step
return self.current
else:
raise StopIteration
class IterateMe_3(object):
"""
Almost a replacement for xrange:
Iterate_2 (start, stop, step=1)
returns the sequence of numbers from start (inclusive) to stop (exclusive),
skipping every step number
( like xrange(start, stop, step) )
This version re-sets itself when used again.
"""
def __init__(self, start, stop, step=1):
self.current = self.start = start
self.stop = stop
self.step = step
def __iter__(self):
self.current = self.start
return self
def next(self):
if self.current < self.stop:
self.current += self.step
return self.current
else:
raise StopIteration
class IterateMe(object):
"""
a re-written xrange
"""
def __init__(self, start, stop, step=1):
self.start = self.current = start
self.stop = stop
self.step = step
def __iter__(self):
print "__iter__ called"
#self.current = self.start # if you want to reset...
return self
def next(self):
print "next called"
current = self.current
if current >= self.stop:
raise StopIteration
self.current += self.step
return current
## hand-written for loop:
print "a hand_written for loop:"
x = range(5)
it = iter (x)
while True:
try:
i = it.next()
except StopIteration:
break
# do the body of the loop
print i
if __name__ == "__main__":
print "first version"
for i in IterateMe_1():
print i
print "second version"
for i in IterateMe_2(0, 5):
print i
print "second version with as different start"
for i in IterateMe_2(4, 7):
print i
print "second version with as different step"
for i in IterateMe_2(4, 15, 3):
print i
print "But what if we break out of it:"
it = IterateMe_2(2, 20, 2)
for i in it:
if i > 10:
break
print i
print "And then pick up again"
for i in it:
print i
print "This one is different when broken out of"
it = IterateMe_3(2, 20, 2)
for i in it:
if i > 10:
break
print i
print "we pick up again from the beginning"
for i in it:
print i
# print "do another loop"
# for i in my_iterator:
# print i
# #print "current:", my_iterator.current