python_by_example/common/common.py

75 lines
1.8 KiB
Python
Raw Permalink Normal View History

2023-11-09 17:45:00 -03:00
import os
from time import sleep
clear = lambda: os.system('clear') if os.name == 'posix' else os.system('cls')
2023-11-13 02:26:58 -03:00
tab='\n'+' '*7
2023-11-09 17:45:00 -03:00
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
2023-11-11 00:09:22 -03:00
def opcs_default(spaces):
sep = ' ' * spaces
print(f""" v){sep}Volver
s){sep}Salir""")
2023-11-09 17:45:00 -03:00
def enter_continue():
input("\n\tPresiona Enter para continuar\n")
2023-11-11 00:09:22 -03:00
def enter_run():
input("\n\tPresiona Enter para correr el programa\n")
2023-11-09 17:45:00 -03:00
@excp_handler
def run_func(func):
print(tab, func.__doc__, '\n')
func()
enter_continue()
2023-11-11 00:09:22 -03:00
def print_run_func(func):
2023-11-13 02:26:58 -03:00
print(" ", func.__doc__, '\n')
2023-11-11 00:09:22 -03:00
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
2023-11-09 17:45:00 -03:00
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
2023-11-11 00:09:22 -03:00
opcs_default(2)
2023-11-09 17:45:00 -03:00
return i, ex