|
| 1 | +from collections import deque |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from typing import NoReturn, Collection, Optional, Callable |
| 4 | +from enum import StrEnum, Enum |
| 5 | + |
| 6 | + |
| 7 | +class HistoryEmptyException(Exception): |
| 8 | + pass |
| 9 | + |
| 10 | + |
| 11 | +def raise_history_empty_exception(msg: str) -> NoReturn: |
| 12 | + raise HistoryEmptyException(msg) |
| 13 | + |
| 14 | + |
| 15 | +class Structure[T](ABC): |
| 16 | + @abstractmethod |
| 17 | + def push(self, element: T) -> None: |
| 18 | + pass |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def pop(self) -> T: |
| 22 | + pass |
| 23 | + |
| 24 | + @property |
| 25 | + @abstractmethod |
| 26 | + def elements(self) -> Collection[T]: |
| 27 | + pass |
| 28 | + |
| 29 | + def __str__(self) -> str: |
| 30 | + return str(self.elements) |
| 31 | + |
| 32 | + @property |
| 33 | + def is_empty(self) -> bool: |
| 34 | + return len(self.elements) == 0 |
| 35 | + |
| 36 | + |
| 37 | +class Queue[T](Structure[T]): |
| 38 | + def __init__(self) -> None: |
| 39 | + self.__elements: deque[T] = deque() |
| 40 | + |
| 41 | + def push(self, element: T) -> None: |
| 42 | + self.__elements += [element] |
| 43 | + |
| 44 | + def pop(self) -> T: |
| 45 | + return self.__elements.popleft() |
| 46 | + |
| 47 | + @property |
| 48 | + def elements(self) -> deque[T]: |
| 49 | + return self.__elements |
| 50 | + |
| 51 | + |
| 52 | +class Stack[T](Structure[T]): |
| 53 | + def __init__(self) -> None: |
| 54 | + self.__elements: list[T] = [] |
| 55 | + |
| 56 | + def push(self, element: T) -> None: |
| 57 | + self.__elements += [element] |
| 58 | + |
| 59 | + def pop(self) -> T: |
| 60 | + return self.elements.pop() |
| 61 | + |
| 62 | + @property |
| 63 | + def elements(self) -> list[T]: |
| 64 | + return self.__elements |
| 65 | + |
| 66 | + |
| 67 | +class Printable(ABC): |
| 68 | + @abstractmethod |
| 69 | + def print(self) -> None: |
| 70 | + pass |
| 71 | + |
| 72 | + @property |
| 73 | + @abstractmethod |
| 74 | + def document_history(self) -> Structure[str]: |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +class PrinterActions(Enum): |
| 79 | + PRINT: int = 1 |
| 80 | + EXIT: int = 2 |
| 81 | + SHOW_DOCUMENT_HISTORY: int = 3 |
| 82 | + |
| 83 | + |
| 84 | +class Printer(Printable): |
| 85 | + def __init__(self) -> None: |
| 86 | + self.__document_history: Queue[str] = Queue[str]() |
| 87 | + |
| 88 | + def print(self) -> None: |
| 89 | + if self.document_history.is_empty: |
| 90 | + raise_history_empty_exception("No documents to print") |
| 91 | + |
| 92 | + print("Printing...") |
| 93 | + print(self.document_history.pop()) |
| 94 | + |
| 95 | + @property |
| 96 | + def document_history(self) -> Queue[str]: |
| 97 | + return self.__document_history |
| 98 | + |
| 99 | + |
| 100 | +class Navegable(ABC): |
| 101 | + @abstractmethod |
| 102 | + def go_to_page(self, page: str) -> None: |
| 103 | + pass |
| 104 | + |
| 105 | + @abstractmethod |
| 106 | + def undo(self) -> None: |
| 107 | + pass |
| 108 | + |
| 109 | + @abstractmethod |
| 110 | + def redo(self) -> None: |
| 111 | + pass |
| 112 | + |
| 113 | + @abstractmethod |
| 114 | + def run(self) -> None: |
| 115 | + pass |
| 116 | + |
| 117 | + |
| 118 | +class WebBrowserActions(StrEnum): |
| 119 | + EXIT: str = "exit" |
| 120 | + UNDO: str = "undo" |
| 121 | + REDO: str = "redo" |
| 122 | + GO_TO_PAGE: str = "go_to_page" |
| 123 | + |
| 124 | + |
| 125 | +class WebBrowser(Navegable): |
| 126 | + def __init__(self) -> None: |
| 127 | + self.page_history: Stack[str] = Stack[str]() |
| 128 | + self.current_page: str = "inicio" |
| 129 | + self.current_page_index: int = 0 |
| 130 | + |
| 131 | + def go_to_page(self, page: str) -> None: |
| 132 | + self.page_history.push(self.current_page) |
| 133 | + self.current_page = page |
| 134 | + self.current_page_index += 1 |
| 135 | + |
| 136 | + print(f"Going to {page}") |
| 137 | + |
| 138 | + def undo(self) -> None: |
| 139 | + if self.page_history.is_empty: |
| 140 | + raise_history_empty_exception(msg="there are not previous pages to go back") |
| 141 | + |
| 142 | + self.current_page = self.page_history.elements.pop() |
| 143 | + print(f"Going back to {self.current_page}") |
| 144 | + |
| 145 | + def redo(self) -> None: |
| 146 | + if self.page_history.is_empty: |
| 147 | + raise_history_empty_exception(msg="there are not next pages to go forward") |
| 148 | + |
| 149 | + def run(self) -> None: |
| 150 | + pass |
| 151 | + |
| 152 | + @property |
| 153 | + def operations(self) -> dict[str, Callable]: |
| 154 | + return {"go_to_page": self.go_to_page, "undo": self.undo, "redo": self.redo} |
| 155 | + |
| 156 | + |
| 157 | +def execute_printable(printable: Printable) -> None: |
| 158 | + printable.print() |
| 159 | + |
| 160 | + |
| 161 | +def execute_navegable(navegable: Navegable) -> None: |
| 162 | + navegable.run() |
| 163 | + |
| 164 | + |
| 165 | +def main() -> None: |
| 166 | + # Generic queue |
| 167 | + queue = Queue[int]() |
| 168 | + queue.push(1) |
| 169 | + queue.push(2) |
| 170 | + queue.push(3) |
| 171 | + print(queue.elements) |
| 172 | + print(queue.pop()) |
| 173 | + print(queue) |
| 174 | + print(queue.is_empty) |
| 175 | + |
| 176 | + # Generic stack |
| 177 | + stack = Stack[int]() |
| 178 | + stack.push(1) |
| 179 | + stack.push(2) |
| 180 | + stack.push(3) |
| 181 | + print(stack.elements) |
| 182 | + print(stack) |
| 183 | + print(stack.pop()) |
| 184 | + |
| 185 | + print(stack.elements) |
| 186 | + print(stack) |
| 187 | + print(stack.is_empty) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + main() |
0 commit comments