generateur_v3/backend/api/generateur/generateur_main.py

97 lines
3.1 KiB
Python
Raw Normal View History

2022-10-10 01:34:38 +02:00
import random
2022-09-16 21:50:55 +02:00
import re
2022-10-10 01:34:38 +02:00
import string
from typing import List
2023-02-22 12:43:39 +01:00
2022-10-10 01:34:38 +02:00
import sympy
2023-02-22 12:43:39 +01:00
from pydantic import BaseModel
2022-09-16 21:50:55 +02:00
2022-10-10 01:34:38 +02:00
class GeneratorOut(BaseModel):
calcul: str
correction: str | None = None
2023-02-22 12:43:39 +01:00
2022-10-10 01:34:38 +02:00
def parseOut(calcul):
2023-02-22 12:43:39 +01:00
"""Fait en sorte de séparer la correction présente dans le calcul"""
regex = r"\[(.*?)\]"
calculEx = calcul['calcul'].replace('[', ' [').replace(']', '] ')
splitted = calculEx.split()
2022-10-10 01:34:38 +02:00
2023-02-22 12:43:39 +01:00
if len(list(filter(lambda e: e.startswith("[") and e.endswith(']'), splitted))) == 0:
splitted.append('[]')
inputs = []
for i in range(len(splitted)):
c = splitted[i]
match = re.findall(regex, c)
if len(match) != 0:
correction = c[1:-1]
if correction == "":
correction = None
splitted[i] = f'[{len(inputs)}]'
inputs.append(
{'index': len(inputs), 'correction': correction, 'value': ""})
calculEx = ' '.join(splitted)
return {'calcul': calculEx, 'inputs': inputs}
2022-10-10 01:34:38 +02:00
def parseGeneratorOut(out: List[GeneratorOut]):
2023-02-22 12:43:39 +01:00
return [parseOut(c) for c in out]
2022-09-16 21:50:55 +02:00
def getObjectKey(obj, key):
if obj[key] == None:
return None
return key if obj[key] != False else 'calcul' if obj['calcul'] != False else None
def getCorrectionKey(obj, key):
2023-02-22 12:43:39 +01:00
return key if (obj[key] != False and obj['correction'] == False) else 'calcul' if (
obj['calcul'] != False and obj['correction'] == False) else 'correction' if obj[
'correction'] != False else None
2022-09-16 21:50:55 +02:00
def parseCorrection(calc, replacer='...'):
2022-10-10 01:34:38 +02:00
exp_list = re.findall(r"\[(.*?)\]", calc)
2022-09-16 21:50:55 +02:00
for exp in exp_list:
calc = calc.replace(f'[{exp}]', replacer)
return calc
2023-02-22 12:43:39 +01:00
def generate_from_data(data, quantity, key, forced_correction=False):
2022-10-10 01:34:38 +02:00
locs = {}
exec(data, {"random": random, "string": string, "sympy": sympy}, locs)
2022-09-16 21:50:55 +02:00
try:
2022-10-10 01:34:38 +02:00
main_func = locs['main']
2023-02-22 12:43:39 +01:00
except KeyError:
2022-09-16 21:50:55 +02:00
return None
main_result = main_func()
default_object = {"calcul": False, 'pdf': False, 'csv': False,
'web': False, 'correction': False} # les valeurs par défaut
# Si l'utilisateur n'a pas entré une valeur, elle est définie à False
2023-02-22 12:43:39 +01:00
2022-09-16 21:50:55 +02:00
result_object = {**default_object, **main_result}
object_key = getObjectKey(result_object, key)
correction_key = getCorrectionKey(result_object, key)
op_list = []
try:
2022-10-10 01:34:38 +02:00
replacer = locs["CORRECTION_REPLACER"]
2023-02-22 12:43:39 +01:00
except KeyError:
2022-09-16 21:50:55 +02:00
replacer = '...'
2023-02-22 12:43:39 +01:00
2022-09-16 21:50:55 +02:00
for i in range(quantity):
main_result = main_func()
main = {**default_object, **main_result}
op_list.append({'calcul': parseCorrection(main[
2023-02-22 12:43:39 +01:00
object_key], replacer) if (
forced_correction or (key != 'web' and main['correction'] == False)) else main[object_key],
"correction": main[correction_key]})
2022-09-16 21:50:55 +02:00
return op_list
2022-10-10 01:34:38 +02:00
2023-02-22 12:43:39 +01:00
def generate_from_path(path, quantity, key, forced_correction=False):
2022-10-10 01:34:38 +02:00
data = open(path, "r").read()
2023-02-22 12:43:39 +01:00
return generate_from_data(data, quantity, key, forced_correction)