Bonjour,
Voilà, j'ai démarré un projet où j'ai besoin de convertir des conditions if elseif else vers un autre format plus linéaire je dirai.
Je pars du principe que j'ai catch la ligne "if ..." car c'est à partir de cet instant que mes problèmes apparaissent.
Exemple :
input -> Type STRING -> "if ((dataA) and ((dataB) or (dataC))) then"
L'output souhaité doit grouper les data. Un peu à l'image d'une équation mathématique (A * (B + C)) qui donnerait AB + AC, le + serait le "or" et le * le "and".
output souhaité :
item 1 - dataA
item 1 - dataB
item 2 - dataA
item 2 - dataC
J'ai pensé à récupérer les informations dans un premier temps les data avec des regex python, puis j'avais dans l'idée de compter les parenthèses pour arriver à connaitre la hiérarchie des "and" et "or", mais je ne suis pas convaincu du chemin que je suis en train de prendre et cela fait plus de 2 jours que je suis dessus, donc je me suis dis qu'il était temps d'obtenir quelques avis ou pistes pour m'aider à trouver la solution.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import re string_to_parse = "if ((dataA) and ((dataB) or (dataC))) then" PATTERN_ANDOR = r'(\(+([\S\s]+?)\)+\s(and|or|then))' if "and" in string_to_parse and "or" in string_to_parse: print("\n\nENTER AND/OR\n") PATTERN_ANDOR = r'(\(+([\S\s]+?)\)+\s(and|or|then))' list = re.findall(PATTERN_ANDOR, string_to_parse, flags=re.MULTILINE) print(f"List : {list}\n") for elmt in list: print(f"Elmt : {elmt}") print(f"String : {elmt[0]} | occ ( : {elmt[0].count('(')} | occ ) : {elmt[0].count('(')} | link : {elmt[2]}\n") str_count_parenthesis_before = elmt[0].split(elmt[1]) print(str_count_parenthesis_before)J'espère avoir assez détaillé mon besoin, merci d'avance pour vos idées/réponses ou orientation sur mon problème
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 Le résultat de l'exécution à date donne : python3 test.py ENTER AND/OR List : [('((dataA) and', 'dataA', 'and'), ('((dataB) or', 'dataB', 'or'), ('(dataC))) then', 'dataC', 'then')] Elmt : ('((dataA) and', 'dataA', 'and') String : ((dataA) and | occ ( : 2 | occ ) : 2 | link : and ['((', ') and'] Elmt : ('((dataB) or', 'dataB', 'or') String : ((dataB) or | occ ( : 2 | occ ) : 2 | link : or ['((', ') or'] Elmt : ('(dataC))) then', 'dataC', 'then') String : (dataC))) then | occ ( : 1 | occ ) : 1 | link : then ['(', '))) then'![]()
Partager