|
| 1 | +import random |
| 2 | + |
| 3 | + |
| 4 | +class Session: |
| 5 | + _instance = None |
| 6 | + |
| 7 | + def __new__(cls, *args, **kwargs): |
| 8 | + if cls._instance is None: |
| 9 | + cls._instance = super(Session, cls).__new__(cls) |
| 10 | + cls._instance.__init__(*args, **kwargs) |
| 11 | + return cls._instance |
| 12 | + |
| 13 | + def __init__(self, id=None, username=None, name=None, email=None): |
| 14 | + if not hasattr(self, 'id'): |
| 15 | + self.id = id |
| 16 | + if not hasattr(self, 'username'): |
| 17 | + self.username = username |
| 18 | + if not hasattr(self, 'name'): |
| 19 | + self.name = name |
| 20 | + if not hasattr(self, 'email'): |
| 21 | + self.email = email |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def clear_session(cls): |
| 25 | + cls._instance = None |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + print("Iniciando sesión...") |
| 30 | + name = input("Nombre: ") |
| 31 | + username = input("Username: ") |
| 32 | + email = input("Email: ") |
| 33 | + |
| 34 | + session = Session(random.randint(1, 1000), username, name, email) |
| 35 | + |
| 36 | + quit = False |
| 37 | + |
| 38 | + while not quit: |
| 39 | + print("\nSeleccione una opción: " |
| 40 | + "\n1- Ver datos de sesión" |
| 41 | + "\n2- Borrar sesión\n") |
| 42 | + |
| 43 | + choice = input("> ") |
| 44 | + |
| 45 | + match choice: |
| 46 | + case "1": |
| 47 | + print(f"Id: {session.id}") |
| 48 | + print(f"Username: {session.username}") |
| 49 | + print(f"Name: {session.name}") |
| 50 | + print(f"Email: {session.email}") |
| 51 | + case "2": |
| 52 | + Session.clear_session() |
| 53 | + quit = True |
| 54 | + print("Hasta pronto") |
| 55 | + case _: |
| 56 | + print("Opción no válida") |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + main() |
0 commit comments