|
| 1 | +""""EJERCICIO: |
| 2 | +Explora el concepto de "logging" en tu lenguaje. Configúralo y muestra |
| 3 | +un ejemplo con cada nivel de "severidad" disponible.""" |
| 4 | + |
| 5 | +import logging |
| 6 | +import time |
| 7 | + |
| 8 | +logging.basicConfig(level=logging.DEBUG) |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | +logging.debug('Detailed information, typically of interest only when diagnosing problems.') |
| 12 | +logging.info('Confirmation that things are working as expected.') |
| 13 | +logging.warning('An indication that something unexpected happened, or indicative of some problem in the near future' |
| 14 | + ' (e.g. ‘disk space low’). The software is still working as expected.') |
| 15 | +logging.error('Due to a more serious problem, the software has not been able to perform some function.') |
| 16 | +logging.critical('A serious error, indicating that the program itself may be unable to continue running.') |
| 17 | + |
| 18 | +"""DIFICULTAD EXTRA (opcional): |
| 19 | +Crea un programa ficticio de gestión de tareas que permita añadir, eliminar y listar dichas tareas. |
| 20 | +- Añadir: recibe nombre y descripción. |
| 21 | +- Eliminar: por nombre de la tarea. |
| 22 | +Implementa diferentes mensajes de log que muestren información según la tarea ejecutada (a tu elección). |
| 23 | +Utiliza el log para visualizar el tiempo de ejecución de cada tarea.""" |
| 24 | + |
| 25 | + |
| 26 | +class TaskManager: |
| 27 | + def __init__(self): |
| 28 | + self.tasks = {'Some': '123', 'other': '133'} |
| 29 | + |
| 30 | + def add_task(self, name: str, description: str): |
| 31 | + start_time = time.time() |
| 32 | + try: |
| 33 | + self.tasks[name] = description |
| 34 | + logging.info(f'New task successfully added: {name}') |
| 35 | + except Exception as e: |
| 36 | + logging.error(f'Error adding a new task: {e}') |
| 37 | + end_time = time.time() |
| 38 | + self.execution_time(start_time, end_time) |
| 39 | + |
| 40 | + def list_tasks(self): |
| 41 | + start_time = time.time() |
| 42 | + logging.info(f'The available tasks are:') |
| 43 | + try: |
| 44 | + for task, description in self.tasks.items(): |
| 45 | + logging.info(f'{task}: {description}') |
| 46 | + logging.info('Task list was successfully printed') |
| 47 | + except Exception as e: |
| 48 | + logging.error(f'Error: {e}') |
| 49 | + end_time = time.time() |
| 50 | + self.execution_time(start_time, end_time) |
| 51 | + |
| 52 | + def remove_task(self, name: str): |
| 53 | + start_time = time.time() |
| 54 | + if name not in self.tasks: |
| 55 | + logging.info(f'The task "{name}" was not found in the task list') |
| 56 | + return |
| 57 | + else: |
| 58 | + del self.tasks[name] |
| 59 | + logging.info(f'The task "{name}" was successfully removed from the task list') |
| 60 | + end_time = time.time() |
| 61 | + self.execution_time(start_time, end_time) |
| 62 | + |
| 63 | + def execution_time(self, start_time, end_time): |
| 64 | + logging.debug(f'Execution time: {end_time - start_time:.6f} seconds') |
| 65 | + |
| 66 | + |
| 67 | +def select_option(): |
| 68 | + try: |
| 69 | + logging.info('Enter an option: ') |
| 70 | + option = int(input()) |
| 71 | + if option == 1: |
| 72 | + logging.info('***** TASK LIST *****') |
| 73 | + task_manager.list_tasks() |
| 74 | + show_menu() |
| 75 | + elif option == 2: |
| 76 | + logging.info('***** ADD A NEW TASK *****') |
| 77 | + logging.info('Enter the task name: ') |
| 78 | + name = input() |
| 79 | + logging.info('Enter the task description: ') |
| 80 | + description = input() |
| 81 | + task_manager.add_task(name=name, description=description) |
| 82 | + show_menu() |
| 83 | + elif option == 3: |
| 84 | + logging.info('***** REMOVE A TASK *****') |
| 85 | + logging.info('Enter the task name to remove: ') |
| 86 | + name = input() |
| 87 | + task_manager.remove_task(name=name) |
| 88 | + show_menu() |
| 89 | + elif option == 4: |
| 90 | + logging.info('Program terminated') |
| 91 | + else: |
| 92 | + logging.warning('Incorrect option was entered') |
| 93 | + show_menu() |
| 94 | + except ValueError: |
| 95 | + logging.warning('Incorrect value entered, try again') |
| 96 | + show_menu() |
| 97 | + |
| 98 | + |
| 99 | +def show_menu(): |
| 100 | + logging.info('********** MENU ************') |
| 101 | + logging.info('1. Show available tasks') |
| 102 | + logging.info('2. Add new task') |
| 103 | + logging.info('3. Remove task') |
| 104 | + logging.info('4. Exit') |
| 105 | + select_option() |
| 106 | + |
| 107 | + |
| 108 | +task_manager = TaskManager() |
| 109 | +show_menu() |
0 commit comments