-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjet.py
More file actions
3047 lines (2330 loc) · 92.8 KB
/
Projet.py
File metadata and controls
3047 lines (2330 loc) · 92.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
#
#IMPORT
#
#
import csv #To manage transfert between python script and csv file
import os #To verify if the size of a file(test if it is emmpty)
import time #To use sleep() function, to make the programm having short waiting time
#needed to resolve linear system(For regular expression)
#import numpy as np
#import matplotlib.pyplot as plt
#from scipy import linalg
#
#
#Variables
#Dictionnary
#
#
#Files and separator
FichierEntree="data.csv"
FichierSortie="data3.csv"
FileChoice="ChoiceFile.txt" # File containing choices
DELIMITER=";"
ARRET=0 #0 if we want to continue, 1 else
DEBUGG=0 #1 if we want to debugg, 0 else
Dictionnary={}
Dictionnary1={}
Dictionnary2={}
#Type of state
# 0-> any
# 1-> initial
# 2-> final
# 3->initial and final
TYPE=[0,1,2,3]
#Forbiden characters in differents input
RESTRICTION_CHOIX_ETAT=[";"," "]
RESTRICTION_CHOIX_EVENEMENT=[";"," "]
RESTRICTION_CHOIX_NOUVEL_ETAT=[";"," "]
#STR including conditions messages of different inputs
CONDITIONS_ETAT="A state can't contain "+str(RESTRICTION_CHOIX_ETAT)+"and be empty"
CONDITIONS_EVENEMENT="An event can't contain "+str(RESTRICTION_CHOIX_EVENEMENT)+"and be empty"
CONDITIONS_NOUVEL_ETAT="A state can't contain "+str(RESTRICTION_CHOIX_ETAT)+",be empty and the destination state must exist"
Done_State=[]
State_CoAccess=[]
#
#
#------------------------------------------------------------------------------------------------------------------------------------
#Functions
#------------------------------------------------------------------------------------------------------------------------------------
#
#
def DisplayChoices():
if os.path.exists(FileChoice):
File=open(FileChoice,"r")
print(File.read())
File.close()
return True
else:
print("Missing File:",FileChoice)
return False
def wait(a=0.8):
# take in parameter a time to wait, default time:0.8s
# make a break and then exit
time.sleep(a)
return 0
#
#Status
#OK
#
def AffichageDico(Dico):
#take as parameter a dictionnary
#print the dictionnary, index by index
print("Dictionnary:")
for i in range(len(Dico)):
print(Dico[i],"\n")
return 0
#
#status
#ok
#
def AffichageAutomateFromDico(Dico):
# Take as parameter a dictionnary
# Return False if it is empty
# Return 0 and display the dictionnary like:
# State:Event --> destination State
if DicoVide(Dico)== True:
print("Error: the dictionnary to print is empty")
return False
else:
print("Affichage sous la forme:\nETAT:évènement-->NouvelEtat\n")
field=list(FIELDNAMES(Dico))
for i in (range(len(Dico))):
for j in range(1,len(field)):
print(Dico[i][field[0]],":",field[j],"-->",Dico[i][list(FIELDNAMES(Dico))[j]])
print("\n")#to separate the display for each state
wait()
return 0
#
#status
#ok
#
def AffichageAutomateFromCSV(CSVFILES):
# Take as parameter a file name
# Return False if not existed, empty, NEED ADD VERIFICATION OF THE EXTENSION(.csv)
# Return 0 and display automate from the file
#We verify the file exist and not empty
if FichierExiste(CSVFILES)==True:
if FichierVide(CSVFILES)==False:
# We convert file into dictionnary and then display it
Dico=CSVToDico(CSVFILES)
AffichageAutomateFromDico(Dico)
return 0
else: #empty file
print("Error: the file ",CSVFILES," is empty")
return False
else: #unexistant file
print("Error: the file do not exist\n")
return False
#
#status
#ok
#
def CSVToDico(CSVFILES):
# Take as parameter a file name
# Return False if not existed, empty, NEED ADD VERIFICATION OF THE EXTENSION(.csv)
# Return the dictionnary corresponding to the file.
Dictionnaire={}
if FichierExiste(CSVFILES)==False: # unexistant file
print("Error: the file do not exist\n")
return False
else:
if FichierVide(CSVFILES)==False:
with open(CSVFILES) as csvfile:
reader = csv.DictReader(csvfile,delimiter=DELIMITER) #We open the file, take as delimitor, the global var previously defined
count=0
for row in reader: #The i-th line of our file is place in our dictionnary at the index i, le line is converted as a dictionnary
Dictionnaire[count]=row
# We want the programm to convert a multiple choice of state in the csv file into a list of state (separator of state in the file: ",")
for i in range(len(EvenementDico(Dictionnaire))):
Value=ListState(row[list(EvenementDico(Dictionnaire))[i]])
row[list(EvenementDico(Dictionnaire))[i]]=ClearState(Value)
count += 1
return (Dictionnaire)
else: # empty Dictionnary
print("Error: the file is empty")
return False
#
#status
#ok
#
def DicoToCSV(Dico,CSVFILES):
# Take as parameters a dictionnary and a file name NEED ADD VERIFICATION OF THE EXTENSION(.csv)
# Create the file if not already exist, delete the old file already exist
# Return False if empty dictionnary
# Return 0 and write the dictionnary in the file
if DicoVide(Dico)==True: # empty dictionnary
print("Error: the dictionnary is empty")
return False
else:
with open (CSVFILES,'w',newline="") as csvfiles:
#Variables
fieldnames=FIELDNAMES(Dico) #keys of dictionnary (here:it is the first line of the csv file['colonne', 'type', 'A', 'B', 'C', 'D'])
writer = csv.DictWriter(csvfiles,fieldnames,delimiter=DELIMITER)
#Writing
writer.writeheader()
for i in range(len(Dico)):
writer.writerow(Dico[i])
return 0
#
#status
#ok
#
def CreationDico():
#Function that create our dictionnary and return it
#input are integreate inside
#No parameters
#Initializing variables and objects:
Dico={}
State=[] #List of states
Type=[] # List of Type associate to each State
Event=[] #List of Event
#Variables that show if we have to stop user input
a=1
#Input States
while a != 0:
# Ask user
Answer=input("Input a state (0 to stop):")
if VerifEntier(Answer)==True : # The input can be convert as integer
if int(Answer)==0: # User want to stop input state
a=0
else: #integer but not 0 so the state name is an integer(accepted)
while VerifSaisieNewEtat(Answer,State)==False: # Verify the input is correct
print("The name of the new state do not respect the conditions.\n"+CONDITIONS_ETAT)
Answer=input("New choice:")
# We initialize Answer2 outside the list of accepted Type to access the while
Answer2=-1
while VerifType(Answer2)==False : # Verify the input Type is correct
Answer2=input("Input the type of the state"+Answer+" among: ordinary(0), initial(1), final(2) or initial and final(3):")
if VerifType(Answer2)==False:
print("The type is not correct")
# Now state and type are coorect, we can add them to lists
Type.append(Answer2)
State.append(Answer)
else: #The input can't be converted to integer, the user want to continue input
while VerifSaisieNewEtat(Answer,State)==False: # Verify the input is correct
print("The name of the new state do not respect the conditions.\n"+CONDITIONS_ETAT)
Answer=input("New choice:")
# We initialize Answer2 outside the list of accepted Type to access the while
Answer2=-1
while VerifType(Answer2)==False : # Verify the input Type is correct
Answer2=input("Input the type of the state "+Answer+" among: ordinary(0), initial(1), final(2) or initial and final(3):")
if VerifType(Answer2)==False:
print("The type is not correct")
# Now state and type are coorect, we can add them to lists
Type.append(Answer2)
State.append(Answer)
#Control Display
print("The list of input's states:",State)
print("The list of input's state's type:",Type,"\n")
wait(0.4)
# Input again, but for events, same variable a
a=1
# Input Event
while a != 0:
# Ask user
Answer=input("Input an event (0 to stop):")
if VerifEntier(Answer)==True : # The input can be convert as integer
if int(Answer)==0: # User want to stop input event
a=0
else:
while VerifSaisieNewEvenement(Answer,Event)==False: # Verify the input is correct
print("The name of the event do not respect conditions.\n"+CONDITIONS_EVENEMENT)
Answer=input("New choice:")
# Now the Eevnt is correct we can add it to his list
Event.append(Answer)
else: #The input can't be converted to integer, the user want to continue input
while VerifSaisieNewEvenement(Answer,Event)==False: # Verify the input is correct
print("The name of the event do not respect conditions.\n"+CONDITIONS_EVENEMENT)
Answer=input("New choice:")
# Now the Event is correct we can add it to his list
Event.append(Answer)
# Control Display
print("The list of input's events:",Event,"\n")
wait(0.4)
# Input Destination states
print("Inserting destination's states:\nSynthax: State:Event-->destination's state")
for i in range(len(State)): # For each State
Dico[i]={} #Create the index in the dictionnary to access later VITAL
Dico[i]["colonne"]=State[i]
Dico[i]["Type"]=Type[i]
for j in range(len(Event)): # For each Event
check=0
while check == 0:
# Ask user
Answer3=input(State[i]+":"+Event[j]+"-->")
# Convert the answer into a list if two or more states
Answer3=ListState(Answer3)
Answer3=ClearState(Answer3)
if type(Answer3)==str:
if VerifSaisieNouvelEtat(Answer3,State)==False: # Verify the input is correct
print(Answer3,": the name of the state do not respect conditions.\n"+CONDITIONS_NOUVEL_ETAT)
else:
check=2
if type(Answer3)==list:
# We have to check if each member of the list is ok
check=1
print("\n")
wait()
for k in range(len(Answer3)):
if VerifSaisieNouvelEtat(Answer3[k],State)==False and check==1:
print(Answer3[k],": the name of the state do not respect conditions.\n"+CONDITIONS_NOUVEL_ETAT)
check=0
break
# Now input is ok, add to the dictionnary
Dico[i][Event[j]]=Answer3
return Dico
#
#status
#OK
#
def FIELDNAMES(Dico):
# Take as paramter a dictionnary
# Return False if empty dictionnary
# return the list of the field (Ex:['colonne','Type','Event1','Event2'])
if DicoVide(Dico)==True:
return False
else:
return(Dico[0].keys())
# We suppose the first state of our dictionnary have the maximum of possible keys
#(ie, No other state have a keys, that the first state do not
#
#status
#ok
#
def ModifDico(Dico):
# Take as parameter a dicitonnary
# return False if the dictionnary is empty
# return the new dictionnary
if DicoVide(Dico)==True:
print("Error: the dictionnary is empty")
return False
else:
#Variables
ListeState=EtatDico(Dico)
ListEvent=EvenementDico(Dico)
OldListState=[]
OldListEvent=[]
# Copy content in backup list to compare
for i in range(len(ListeState)):
OldListState.append(ListeState[i])
for i in range(len(ListEvent)):
OldListEvent.append(ListEvent[i])
# Modify lists by asking user
modifListeEtat(ListeState)
modifListeEvenement(ListEvent)
# Delete unwanted states
print("Removing unwanted states")
wait()
for i in range(len(OldListState)):
if OldListState[i] not in ListeState: # state was here before, but not anymore
a=Dico.pop(i)
# Add wanted states
print("Adding new states")
wait()
for i in range (len(ListeState)):
Size=len(Dico)
if ListeState[i] not in OldListState: # state was not here before
Dico.setdefault(Size+1,{'colonne':ListeState[i]}) # add it to the dictionnary
# Check if the dictionnary is sorted, if not sort it, in order to balance it
if VerifTrieDico(Dico)==False:
Dico=TrieDicoCle(Dico)
Dico=ConvertIndiceDico(Dico) # Edit dictionnary to make index been succesives integers
#Delete unwanted events
print("Removing unwanted events")
wait()
for i in range(len(OldListEvent)):
if OldListEvent[i] not in ListEvent: # event was here before, but not anymore
for j in range(len(Dico)):
a=Dico[j].pop(OldListEvent[i]) # delete it
# Add wanted events
print("Adding new events")
wait()
for i in range(len(ListEvent)):
if ListEvent[i] not in OldListEvent: # event was not here before
for j in range(len(Dico)): # add the event in each state of the dictionnary
Dico[j][ListEvent[i]]=""
# Display the automaton and ask input for each destination state
print("New fields in the automaton")
print("State:Event-> destination's state")
for i in range(len(Dico)):
field=list(FIELDNAMES(Dico))
# Edit type
print(Dico[i][field[0]],":",field[1],"-->",Dico[i][list(FIELDNAMES(Dico))[1]])
Answer=input("Input the type of the state "+Dico[i][field[0]]+" among: ordinary(0), initial(1), final(2) or initial and final(3) (Enter to skip):")
while VerifType(Answer)==False and Answer!="": # We check if the type respect conditions, if empty keep the old value
print("The type is not correct")
Answer=input("Input the type of the state "+Dico[i][field[0]]+" among: ordinary(0), initial(1), final(2) or initial and final(3) (Enter to skip):")
if Answer != "":
Dico[i][list(FIELDNAMES(Dico))[1]]=Answer
# Edit destination state
for j in range(2,len(field)):
check=0
while check==0:
print(Dico[i][field[0]],":",field[j],"-->",Dico[i][list(FIELDNAMES(Dico))[j]])
Answer=input("Enter a new destination state (Enter to skip):")
Answer=ListState(Answer)
Answer=ClearState(Answer)
if type(Answer)==str:
if VerifSaisieNouvelEtat(Answer,EtatDico(Dico))== False:
print("The name of the state do no respect conditions.\n"+CONDITIONS_NOUVEL_ETAT)
else:
check=2
if Answer != "":
#print("test")
Dico[i][list(FIELDNAMES(Dico))[j]]=Answer
if type(Answer)==list:
check=1
print("\n")
wait()
for k in range(len(Answer)):
if VerifSaisieNouvelEtat(Answer[k],EtatDico(Dico))==False and check==1:
print(Answer[k],": the name of the state do not respect conditions.\n"+CONDITIONS_NOUVEL_ETAT)
check=0
break
else:
print(Answer[k],"Correct")
if type(Answer)==list and Answer[k]!="":
Dico[i][list(FIELDNAMES(Dico))[j]]=Answer
return Dico
#
#Status
#OK
#
def EquilibrageDico(Dico):
# take as parameter a dictionnary
# return - if the dictionnary is empty
# return the dictionarry with reorganized index
#
#Need to check if the keys of the dictionnary are sorted (function VeriTrieDico() and TriDicoCle())
# Suppr is a list containing index that must be delete from our dictionnary
Suppr=[]
if DicoVide(Dico)==True:
return -1
else:
if VerifTrieDico(Dico)==False:
Dico=TrieDicoCle(Dico)
for i in range(len(list(Dico.keys()))):
if list(Dico.keys())[i]!=i: # Check if each index is correctly named, else named it correctly
OldValue=list(Dico.values())[i] # identify which index we have to remove
Dico.setdefault(i,OldValue) # add new key et attribute the corresponding value, careful: index added at the end
if i not in Suppr: # we add the index only if this index is not attributed
Suppr.append(list(Dico.keys())[i])
for i in range(len(Suppr)): # delete unwanted index
a=Dico.pop(Suppr[i])
return Dico
#
#Status
#OK
#
def ConvertIndiceDico(Dico):
# Take as parameter a dictionnarie
# return False if the dictionnary is empty
# return the dictionnary with index converted successively
if DicoVide(Dico)==True:
print("Error: the dictionnary is empty")
return False
else:
FinalDico={}
for i in range(len(Dico.keys())):
FinalDico[i]=Dico[list(Dico.keys())[i]]
return FinalDico
#
#Status
#ok
#
def TrieDicoCle(Dico):
# Retourne -1 si le dictionnaire est vide
# Retourne le dictionnaire trié sinon
if DicoVide(Dico):
return -1
else:
Dico=sorted(Dico.items(),key=lambda t:t[0])
#Here it's a list not a dictionnary, let's convert it
FinalDico={}
for i in range(len(Dico)):
FinalDico[Dico[i][0]]=Dico[i][1]
return FinalDico
#
#Status
# OK
#
def ListState(string):
#Take in parameter, a string of state or a state
# Return the state if there is only one state
# Return a list of states if there is more than one
if len(string)==0 or "," not in string:
return string
else:
return( string.split(","))
#
#Status
#ok
#
def ClearState(State):
# Take as parameter a list
# Return this list without elements that are twice or more in
# Return the State if there is only one state(no matter how many times) in the list
# Return the parameter if the parameter is not a list
if type(State) ==list:
New=[]
for i in range(len(State)):
if State[i] not in New:
New.append(State[i].replace(" ",""))
if len(New)==1:
return(New[0])
else:
return(New)
else:
return State.replace(" ","")
#
#Status
#OK
#
def modifListeEtat(ListeState):
# Take as parameter liste of state
# Return the new new, edited by user
if len(ListeState) == 0: #Empty list
print("The list is empty")
return False
else:
#Variables to know when stop
stop = 0
while stop == 0:
# Ask user
answer =input(str(ListeState)+"\nDo you want to edit the state's list above (yes or no):")
# Convert to lower
answer=answer.lower()
# many case depending of answer
match answer:
# User want to edit
case "yes":
print("Edit")
answer2=input("Insert the name of the state to edit or remove it and insert the new name to add it:")
# Check answer is correct:
while VerifSaisieEtat(answer2)==False:
print("The name do not respect conditions.\n"+CONDITIONS_ETAT)
answer2=input("New choice:")
# Answer is now correct
if answer2 in ListeState: # State already exist
Choice=input("Choosen state: "+answer2+" Do you want to remove(0) or edi(1) it:")
# Check field 'Choice'
while VerifEntier(Choice)==False or int(Choice) not in [0,1]:
print("The expected answer is 0 or 1")
Choice=input("Choosen state: "+answer2+" Do you want to remove(0) or edit(1) it:")
if int(Choice)==0:
# Erasing
ListeState.remove(answer2)
else:
# Edit
new=input("Insert the new state:")
# Check field 'new'
while VerifSaisieNewEtat(new,ListeState)==False:
print("The name do not respect conditions.\n"+CONDITIONS_ETAT)
new=input("New choice:")
ListeState=ModifListe(answer2,ListeState,new)
else:
# Adding
ListeState.append(answer2)
# No more changes
case "no":
print("End of edit")
wait()
# edit value of stop variable to exit
stop=1
# other answer
case _:
print("The expected answer is yes or no")
return ListeState
#
#Status
#OK
#
def modifListeEvenement(ListeEvent):
# Take as parameter list of event
# Return the new list, edited by user
if len(ListeEvent) == 0: # Empty list
print("The list is empty")
return False
else:
# Varaiables to know when stop
stop = 0
while stop == 0:
# Ask user
answer =input(str(ListeEvent)+"\nDo you want to edit the event list above (yes or no):")
# Convert to lower
answer=answer.lower()
# Many case depending on th answer
match answer:
# user want to edit
case "yes":
print("Edit")
answer2=input("Insert the name of the event to edit or remove it and insert the new name to add it:")
# Check answer is correct:
while VerifSaisieEvenement(answer2)==False:
print("The name of the event do not respect conditions.\n"+CONDITIONS_EVENEMENT)
answer2=input("New choice:")
# field is now correct
if answer2 in ListeEvent: # Event already exist
Choice=input("Choosen event: "+answer2+" Do you want to remove(0) or edit(1) it:")
# Check field 'Choice' is correct
while VerifEntier(Choice)==False or int(Choice) not in [0,1]:
print("The expected answer is 0 or 1")
Choice=input("Choosen event"+answer2+" Do you want to remove(0) or edit(1) it:")
if int(Choice)==0:
# Erasing
ListeEvent.remove(answer2)
else:
# Edit
new=input("Insert the new event:")
# Check field 'new' is correct
while VerifSaisieNewEvenement(new,ListeEvent)==False:
print("The name of the event do not respect conditions.\n"+CONDITIONS_EVENEMENT)
new=input("New choice:")
ListeEvent=ModifListe(answer2,ListeEvent,new)
else:
#Ajout
ListeEvent.append(answer2)
# No more edit
case "no":
print("End of edit")
wait()
# Change value of variable stop to exit
stop=1
# Other answer
case _:
print("The expected answer is yes or no")
return ListeEvent
#
#Status
#OK
#
def ModifListe(old,List,new):
# Take as parameter list and two variables, one already in the list and one to replace the old one
# Return the edited list
if len(List)!= 0: # Check empty list
for i in range(len(List)):
if List[i]==old: # Cross list to find the old value
List[i]=new # Replace by new value
return List
else:
print("Error: the list is empty")
return False
#
#status
#OK
#
def DicoVide(Dico):
# Take as parameter a Dictionnary
# Return True if the Dictionnary is empty
# Return False if the Dictionnary is not empty
# Return -1 if type of parameter is incorrect
if type(Dico)!=dict: # type
print("Error: the type expected is dictionnary")
return -1
if not Dico:
return True # Empty Dictionnary
else:
return False # Not empty Dictionnary
#
#status
#ok
#
def FichierVide(CSVFILES):
# Take as parameter a file name
# Return True if the file is empty
# Return False if the file is not empty
# Return -1 if the file does not exist
# Check existing file
if FichierExiste(CSVFILES)==False:
print("Error: the file do not exist")
return -1
else:
# Look at size of the file
size=os.stat(CSVFILES).st_size
if size == 0:
return True # Empty file
else:
return False # File not empty
#
#status
#ok
#
def FichierExiste(CSVFILES):
# Take as parameter a file name
# Return True if the file do exist
# Return False if the file do not exist
if os.path.exists(CSVFILES): # look for path where file is valide
return True # File exist
else:
return False # File not exist
#
#status
#ok
#
def VerifType(Type):
# Take as parameter a type (0,1,2,3) of state
# Return True if the Type is correct
# Return False if the Type is incorrect
if VerifEntier(Type)==True: # Type var is an integer
if int(Type) in TYPE: # Type is correct #TYPE is global var defined before
return True
return False # Type incorrect
#
#status
#ok
#
def VerifEntier(var):
# Take as parameter a variable
# Return True if parameter can be converted to integer
# Return False if parameter cannot be converted to integer
try: # Try to convert to integer
int(var)
except ValueError: # If error while convertion -> var can't be converted
return False # Cannot be converted
else: # If no error -> var can be converted safely
return True # Can be converted
#
#status
#ok
#
def VerifAEF(Dico):
# Take as parameter a dictionnary
# Return True if the dictionnary do show a Final State Machine(FSM)
# Return False if the dictionnary do not show a Final State Machine(FSM)
# Return -1 if empty of incorrect parameter
if DicoVide(Dico)==True: #empty
print("Automaton is empty")
return -1
else:
# Define list of states and events
State=EtatDico(Dico)
Event=EvenementDico(Dico)
for i in range(len(Dico)): # Cross states
for j in range(len(Event)): # Cross events
# case of a list of states
if type(Dico[i][Event[j]])==list:
for k in range(len(Dico[i][Event[j]])):
if Dico[i][Event[j]][k] not in State and Dico[i][Event[j]][k]!="":
return False
# case of a single state
if type(Dico[i][Event[j]])==str:
if Dico[i][Event[j]] not in State and Dico[i][Event[j]]!="": # Check if a field already has a state or is empty
return False
# If arrive here: every fields are state or empty --> FSM
return True
#
#status
#OK
#
def VerifSaisieNewEtat(Choice,ListState):
# Take as parameter a user choice and a list of state
# return False if input not correct
# return True if input is correct
# return -1 if parameters are incorrect
if Choice == "": # Empty input
return -1
if len(ListState)==0: # empty list
return -1
else:
if Choice in ListState: # check state do not already exist
print("This state already exist")