forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.py
More file actions
45 lines (34 loc) · 988 Bytes
/
iterator.py
File metadata and controls
45 lines (34 loc) · 988 Bytes
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
from abc import ABCMeta, abstractmethod
class IIterator(metaclass=ABCMeta):
@staticmethod
@abstractmethod
def has_next():
"""Returns Boolean whether at end of collection or not"""
@staticmethod
@abstractmethod
def next():
"""Return the object in collection"""
class Iterable(IIterator):
def __init__(self):
self.index = 0
self.maximum = 7
def next(self):
if self.index < self.maximum:
x = self.index
self.index += 1
return x
else:
raise Exception("AtEndOfIteratorException", "At End of Iterator")
def has_next(self):
return self.index < self.maximum
ITERABLE = Iterable()
while ITERABLE.has_next():
print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())
# print(ITERABLE.next())