|
| 1 | +# pylint: disable=pointless-string-statement,missing-module-docstring,missing-class-docstring,missing-function-docstring |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from time import time |
| 6 | +from typing import TypedDict, Self |
| 7 | + |
| 8 | + |
| 9 | +""" |
| 10 | + Logging... |
| 11 | +""" |
| 12 | + |
| 13 | +print("Logging...") |
| 14 | + |
| 15 | +logging.basicConfig( |
| 16 | + level=logging.DEBUG, format="%(asctime)s (%(levelname)s): %(message)s" |
| 17 | +) |
| 18 | + |
| 19 | +print("\nlogging.critical(msg=<MESSAGE>)...\n") |
| 20 | +logging.critical(msg="Critical log message!") |
| 21 | + |
| 22 | +print("\nlogging.debug(msg=<MESSAGE>)...\n") |
| 23 | +logging.debug(msg="Debug log message!") |
| 24 | + |
| 25 | +print("\nlogging.error(msg=<MESSAGE>)...\n") |
| 26 | +logging.error(msg="Error log message!") |
| 27 | + |
| 28 | +print("\nlogging.info(msg=<MESSAGE>)...\n") |
| 29 | +logging.info(msg="Information log message!") |
| 30 | + |
| 31 | +print("\nlogging.warning(msg=<MESSAGE>)...\n") |
| 32 | +logging.warning(msg="Warning log message!") |
| 33 | + |
| 34 | + |
| 35 | +print( |
| 36 | + "\n# ---------------------------------------------------------------------------------- #\n" |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +""" |
| 41 | + Additional challenge... |
| 42 | +""" |
| 43 | + |
| 44 | +print("Additional challenge...") |
| 45 | + |
| 46 | +type Description = str |
| 47 | +type Title = str |
| 48 | + |
| 49 | +Task = TypedDict( |
| 50 | + "Task", |
| 51 | + { |
| 52 | + "description": Description, |
| 53 | + "title": Title, |
| 54 | + }, |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +class TaskManager: |
| 59 | + __debug: bool |
| 60 | + __tasks: list[Task] |
| 61 | + |
| 62 | + def __init__( |
| 63 | + self, *, initial_tasks: None | list[Task] = None, enableDebug=False |
| 64 | + ) -> None: |
| 65 | + self.__debug = enableDebug |
| 66 | + self.__tasks = [] if initial_tasks is None else initial_tasks |
| 67 | + |
| 68 | + def add_task(self, *, new_task: Task) -> Self: |
| 69 | + if self.__debug: |
| 70 | + print() |
| 71 | + logging.debug(msg="addTask (method) start execution...") |
| 72 | + start_time: float = time() |
| 73 | + |
| 74 | + self.__tasks.append(new_task) |
| 75 | + |
| 76 | + if self.__debug: |
| 77 | + logging.debug(msg="addTask (method) ends execution!") |
| 78 | + print(f"addTask: {time()-start_time} seconds") |
| 79 | + |
| 80 | + return self |
| 81 | + |
| 82 | + def delete_task_by_title(self, *, title: Title) -> Self: |
| 83 | + if self.__debug: |
| 84 | + print() |
| 85 | + start_time: float = time() |
| 86 | + logging.debug(msg="delete_task_by_title (method) start execution...") |
| 87 | + |
| 88 | + sanitized_tasks: list[Task] = [] |
| 89 | + sanitized_title: str = title.strip().upper() |
| 90 | + |
| 91 | + for task in self.__tasks: |
| 92 | + if task["title"].strip().upper() != sanitized_title: |
| 93 | + sanitized_tasks.append(task) |
| 94 | + |
| 95 | + self.__tasks = sanitized_tasks |
| 96 | + |
| 97 | + if self.__debug: |
| 98 | + logging.debug(msg="delete_task_by_title (method) ends execution!") |
| 99 | + print(f"delete_task_by_title: {time()-start_time} seconds") |
| 100 | + |
| 101 | + return self |
| 102 | + |
| 103 | + def print_tasks(self) -> Self: |
| 104 | + if self.__debug: |
| 105 | + print() |
| 106 | + logging.debug(msg="print_tasks (method) start execution...") |
| 107 | + start_time: float = time() |
| 108 | + print() |
| 109 | + |
| 110 | + for task in self.__tasks: |
| 111 | + print(task) |
| 112 | + |
| 113 | + if self.__debug: |
| 114 | + print() |
| 115 | + logging.debug(msg="print_tasks (method) ends execution!") |
| 116 | + print(f"print_tasks: {time()-start_time} seconds") |
| 117 | + |
| 118 | + return self |
| 119 | + |
| 120 | + |
| 121 | +task_manager: TaskManager = TaskManager(enableDebug=True) |
| 122 | + |
| 123 | +EXIT_: bool = False |
| 124 | + |
| 125 | +while not EXIT_: |
| 126 | + operation: str = input( |
| 127 | + "\nWrite an operation ('Add task', 'Delete task by title', 'Print tasks', or 'Exit'): " |
| 128 | + ) |
| 129 | + |
| 130 | + sanitized_operation: str = operation.strip().upper() |
| 131 | + |
| 132 | + match sanitized_operation: |
| 133 | + case "ADD TASK": |
| 134 | + |
| 135 | + task_title: str = input("\nTask title: ") |
| 136 | + task_description: str = input("Task description: ") |
| 137 | + |
| 138 | + task_manager.add_task( |
| 139 | + new_task={ |
| 140 | + "title": task_title.strip(), |
| 141 | + "description": task_description.strip(), |
| 142 | + } |
| 143 | + ) |
| 144 | + |
| 145 | + case "DELETE TASK BY TITLE": |
| 146 | + task_title: str = input("\nTask title: ") |
| 147 | + |
| 148 | + task_manager.delete_task_by_title(title=task_title) |
| 149 | + |
| 150 | + case "PRINT TASKS": |
| 151 | + task_manager.print_tasks() |
| 152 | + |
| 153 | + case "EXIT": |
| 154 | + EXIT_ = True |
| 155 | + print("\nApplication closed!") |
| 156 | + |
| 157 | + case _: |
| 158 | + print("\nInvalid operation! Try again...") |
0 commit comments