forked from Rustam-Z/python-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.py
More file actions
60 lines (45 loc) Β· 1.85 KB
/
Copy pathiterator.py
File metadata and controls
60 lines (45 loc) Β· 1.85 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
"""
Iterator allows traversing the elements of collections without exposing the internal details.
+ Clean client code (Single Responsibility Principle).
+ Introducing iterators in collections is possible without changing the clientβs code (Open/Closed Principle).
+ Each iteration object has its own iteration state, so you can delay & continue iteration.
- Use of iterators with simple collections can overload the application.
"""
from __future__ import annotations
from collections.abc import Iterable, Iterator
from typing import Any
class AlphabeticalOrderIterator(Iterator):
_position: int = None
_reverse: bool = False
def __init__(self, collection: WordsCollection,
reverse: bool = False):
self._collection = collection
self._reverse = reverse
self._position = -1 if reverse else 0
def __next__(self):
try:
value = self._collection[self._position]
self._position += -1 if self._reverse else 1
except IndexError:
raise StopIteration
return value
class WordsCollection(Iterable):
def __init__(self, collection=None):
if collection is None:
collection = []
self._collection = collection
def __iter__(self) -> AlphabeticalOrderIterator:
return AlphabeticalOrderIterator(self._collection)
def get_reverse_iterator(self) -> AlphabeticalOrderIterator:
return AlphabeticalOrderIterator(self._collection, True)
def add_item(self, item: Any):
self._collection.append(item)
if __name__ == "__main__":
collection_ = WordsCollection()
collection_.add_item("First")
collection_.add_item("Second")
collection_.add_item("Third")
print("Straight traversal:")
print("\n".join(collection_))
print("Reverse traversal:")
print("\n".join(collection_.get_reverse_iterator()))