From df3c4d43a6242133f34a951e0a22028d7991a0ba Mon Sep 17 00:00:00 2001 From: Antonio MATTAR <antonio.mattar@etu.univ-amu.fr> Date: Tue, 28 Jun 2022 10:52:31 +0200 Subject: [PATCH] Merge branch 'main' of https://gitlab.lis-lab.fr/benjamin.monmege/alligators-python --- image_maker.py | 15 +++++ logic.py | 53 +++++++++++------ main.py | 157 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 182 insertions(+), 43 deletions(-) diff --git a/image_maker.py b/image_maker.py index d615153..783f458 100644 --- a/image_maker.py +++ b/image_maker.py @@ -26,11 +26,23 @@ def new_color(): b = random.randint(0,255) return (r,g,b) +def close_colors(a,b): + r = abs(a[0]-b[0]) + g = abs(a[1]-b[1]) + b = abs(a[2]-b[2]) + return r*r+g*g+b*b <= 70*70 + + variables_colors_couple = {} def associateVariableWithColor(variable): assert (logic.isVariable(variable)) c = new_color() + colors = list(variables_colors_couple.values()) + for color in colors: + if close_colors(c,color): + associateVariableWithColor(variable) + break if c in list(variables_colors_couple.values()): associateVariableWithColor(variable) # recursive call to not get the same color if str(variable) not in variables_colors_couple: @@ -130,6 +142,9 @@ def createAppImage(terme): left = logic.getFirstTerm(terme) right = logic.getSecondTerm(terme) im1 = createImage(left) + if len(terme) == 4: + im1 = addNumberToImage(im1,terme[3]) + left = left[:3] if len(terme) == 5: im1 = addNumberToImage(im1,terme[-1]) if logic.isApplication(right): diff --git a/logic.py b/logic.py index 81d28af..c24f4f8 100644 --- a/logic.py +++ b/logic.py @@ -191,6 +191,33 @@ def beta_reduction(t): else: return None +def annotate_reductor(t): + if isVariable(t): + return None + elif isAbstraction(t): + x = getInputFromAbs(t) + A = getOutputFromAbs(t) + B = annotate_reductor(A) + if B != None: + return new_abs(x.copy(),B) ### x.copy() sont inutiles car on ne change jamais le tableau de variables + else: + return None + elif isApplication(t): + A1 = getFirstTerm(t) + B1 = getSecondTerm(t) + A2 = annotate_reductor(A1) + if A2 != None: + return (new_app(A2,B1)) + elif A2 == None: + B2 = annotate_reductor(B1) + if B2 != None: + return (new_app(A1,B2)) + elif isAbstraction(A1): + t = t.append('*') + return t + else: + return None + import image_maker image_counter = 0 def captureImage(terme, path, counter=True, date= True): @@ -216,9 +243,15 @@ def beta_reduction_totale(terme, path, saveImages=True): return (terme) else: if path == None: - captureImage(terme, None) + if annotate_reductor(terme) != None: + captureImage(annotate_reductor(terme), None) + else: + captureImage(terme, None) else: - captureImage(terme,path) + if annotate_reductor(terme) != None: + captureImage(annotate_reductor(terme), path) + else: + captureImage(terme,path) if beta_reduction(terme) != None: return beta_reduction_totale(beta_reduction(terme), path) return (terme) @@ -432,7 +465,7 @@ def beta_reduction_choice_n(terme,n): def beta_reduction_interactive(terme, at): global counters if at != None: - # print(annotated_to_string(at)) + print(annotated_to_string(at)) choice = int(input("Choose a beta reduction: ")) while choice <= 0 or choice > counters: print("Invalid choice") @@ -454,8 +487,6 @@ def cleanReductions(l): return l def beta_reduction_interactive_totale(terme,path): - save_image_choice = int(input("Save image? (1 for yes, 0 for no): ")) - if save_image_choice == 1: if beta_reduction((terme)) != None: # print(to_string(terme)) at = (annotate_beta_reduction((terme))) @@ -470,18 +501,6 @@ def beta_reduction_interactive_totale(terme,path): else: captureImage(terme,path) return (terme) - else: - if beta_reduction((terme)) != None: - # print(to_string(terme)) - at = (annotate_beta_reduction((terme))) - choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non ")) - if choix==1: - return beta_reduction_interactive_totale(beta_reduction_interactive(terme,at),path) - else: - terme = cleanReductions(terme) - print("C'est fini, le terme obtenu est : "+to_string(terme)) - else: - return (terme) diff --git a/main.py b/main.py index 4f6bcc5..c6bb82c 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import os import time import pyfiglet import keyboard +import shutil clear = lambda: os.system('cls') welcome_banner = pyfiglet.figlet_format('LambdaCalculus Interpreter') @@ -47,6 +48,23 @@ boolean_representation= { } +def save_images(terme,nom,date): + os.makedirs("/sauvegarde", exist_ok=True) + logic.captureImage(terme,"/sauvegarde",nom,date) + +def delete_images(folder): + if os.path.exists(folder): + for the_file in os.listdir(folder): + file_path = os.path.join(folder, the_file) + try: + if os.path.isfile(file_path): + os.unlink(file_path) + except Exception as e: + print(e) +def moveImages(src, dest): + for filename in os.listdir(src): + shutil.move(src+"/"+filename, dest) + def return_main_menu(): not_pressed = True print('Press ENTER to return to the main menu') @@ -70,13 +88,15 @@ def run_beta_reduction_totale(terme,path='beta_reduction_totale'): os.makedirs(path, exist_ok=True)# cree le rep si il existe pas, le vide si non if len(os.listdir(path)) > 0: logic.image_counter = 0 - t=logic.beta_reduction_totale(terme,path) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.beta_reduction_totale(terme,path) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images? (y/n): ') if save_image_choice == 'y': - logic.captureImage(terme,path) - + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) def run_beta_reduction_interactive_totale(terme,path='beta_reduction_interactive_totale'): terme = parsing.parseTerm(terme) @@ -84,6 +104,14 @@ def run_beta_reduction_interactive_totale(terme,path='beta_reduction_interactive if len(os.listdir(path)) > 0: logic.image_counter = 0 logic.beta_reduction_interactive_totale(terme,path) + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) def run_show_numbers(path): os.makedirs(path, exist_ok=True) @@ -98,11 +126,15 @@ def run_show_numbers(path): terme = int(input('Enter a number: ')) t = app_functions.dec_to_church(terme) print('Voici le terme:',logic.to_string(t)) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'ENTIER'+str(terme),False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'ENTIER'+str(terme),False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice==2: clear() @@ -110,10 +142,15 @@ def run_show_numbers(path): t = app_functions.dec_to_lambda_relative_integers(terme) print('Voici le terme:',logic.to_string(t)) save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'ENTIER-RELATIF-'+str(terme),False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'ENTIER-RELATIF-'+str(terme),False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice==3: run_main_menu() @@ -129,48 +166,68 @@ def run_boolean_expression(path): clear() t = app_functions.NOT print('Voici le terme:',logic.to_string(t)) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'NOT',False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'NOT',False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice==2: clear() t = app_functions.AND print('Voici le terme:',logic.to_string(t)) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'AND',False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'AND',False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice==3: clear() t = app_functions.OR print('Voici le terme:',logic.to_string(t)) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'OR',False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'OR',False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice ==4: clear() t = app_functions.XOR print('Voici le terme:',logic.to_string(t)) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'XOR',False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'XOR',False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice==5: clear() t = app_functions.IS_ZERO print('Voici le terme:',logic.to_string(t)) - save_image_choice = input('Do you want to save the image? (y/n): ') + logic.captureImage(t,path,'IS_ZERO',False) + save_image_choice = input('Do you want to save the images? (y/n): ') while save_image_choice not in ['y','n']: - save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') if save_image_choice == 'y': - logic.captureImage(t,path,'IS_ZERO',False) + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) elif choice==6: run_main_menu() @@ -235,32 +292,80 @@ def run_arithmetic_operations_menu(path='arithmetic expressions'): clear() print("Voici le terme: "+ logic.to_string(app_functions.ADD)) logic.captureImage(app_functions.ADD,path,'ADD', False) + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) return_main_menu() elif choice == 2: clear() print("Voici le terme: "+ logic.to_string(app_functions.SUB)) logic.captureImage(app_functions.SUB,path,'SUB', False) + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) return_main_menu() elif choice == 3: clear() print("Voici le terme: "+ logic.to_string(app_functions.MUL)) logic.captureImage(app_functions.MUL,path,'MUL',False) + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) return_main_menu() elif choice==4: clear() print("Voici le terme: "+ logic.to_string(app_functions.POW)) logic.captureImage(app_functions.POW,path,'POWER',False) + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) return_main_menu() elif choice==5: clear() print("Voici le terme: "+ logic.to_string(app_functions.SUCCS)) logic.captureImage(app_functions.SUCCS,path,'SUCCS',False) + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) return_main_menu() elif choice==6: clear() print("Voici le terme: "+ logic.to_string(app_functions.PRED)) logic.captureImage(app_functions.PRED,path,'PRED',False) - return_main_menu + save_image_choice = input('Do you want to save the images? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the images of the reduction? (y/n): ') + if save_image_choice == 'y': + os.makedirs("./sauvegarde/"+path, exist_ok=True) + moveImages(path,'./sauvegarde/'+path) + elif save_image_choice == 'n': + delete_images(path) + return_main_menu() elif choice == 7: clear() run_main_menu() -- GitLab