python_by_example/common/common.py
2023-11-11 00:09:22 -03:00

75 lines
1.8 KiB
Python

import os
from time import sleep
clear = lambda: os.system('clear') if os.name == 'posix' else os.system('cls')
tab='\n'+' '*4
def excp_handler(function):
def wrapper(*args, **kwargs):
try:
function(*args, **kwargs)
except Exception:
print("Lee atentamente las instrucciones e intenta nuevamente")
sleep(3)
return wrapper
def opcs_default(spaces):
sep = ' ' * spaces
print(f""" v){sep}Volver
s){sep}Salir""")
def enter_continue():
input("\n\tPresiona Enter para continuar\n")
def enter_run():
input("\n\tPresiona Enter para correr el programa\n")
@excp_handler
def run_func(func):
print(tab, func.__doc__, '\n')
func()
enter_continue()
def print_run_func(func):
print(tab, func.__doc__, '\n')
cont = 0
retry = True
enter_continue()
while retry:
try:
func()
retry = False
except Exception:
cont += 1
if cont > 3:
print("Ocurrio un error al intentar correr el programa")
print("¿Está instalado 'tk' en el sistema?")
break
def user_input(i):
try:
selection = input("\n Ingresa una opción: ")
if selection in 'sSvV':
return selection.lower()
selection = int(selection)
if 0 < selection <= i:
clear()
return selection
else:
raise Exception
except Exception:
print("Debes ingresar una opción válida")
sleep(2)
return 0
def print_challenges(exercises):
ex = exercises()
i = 0
funcs = list(vars(exercises))[1:-3]
while i < len(funcs):
print(f"{i+1}) ".rjust(8), end='')
print(vars(exercises).get(funcs[i]).__doc__)
i+=1
opcs_default(2)
return i, ex