Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import mailbox
- from random import randint
- import string
- class bcolors: #Déclaration d'une classe qui va nous permettre d'afficher des message en couleur
- OK = '\033[92m' #VERT
- WARNING = '\033[93m' #JAUNE
- FAIL = '\033[91m' #ROUGE
- RESET = '\033[0m' #PARDEFAUT
- def random (): #Fonction qui génére un code aléatoire pour confirmer le reset du repertoire
- ch=""
- for i in range (6):
- if (randint(1,2)==1):
- ch=ch+chr(randint(48,57))
- else:
- ch=ch+chr(randint(65,90))
- return (ch)
- def choix (): #Fonction menu
- print ("Que voulez-vous faire ?")
- print ("1. Ajouter un contact")
- print ("2. Modifier un contact")
- print ("3. Rechercher un contact")
- print ("4. Supprimer un contact")
- print ("5. Voir la liste des contacts")
- print ("6. Effacer l'ensemble des contacts")
- print ("7. Quitter")
- def veriftel (ch): #Vérifie si numéro est uniquement cmposé de nombre
- if len (ch)!=8 or ch[0]=='0':
- return 0
- for i in range(len(ch)):
- if (ord(ch[i])>57 or ord(ch[i])<48):
- return 0
- return 1
- def verifmail (ch): #Vérifie le format d'un email
- if (ch.find("@")!=ch.rfind("@") or ch.find("@")==-1 or ch.find("@")>ch.find(".") or ch.find(".")==-1):
- return 0
- return 1
- def ajout (): #Fonction ajout d'un contact
- T=[]
- T.append(input ("Nom: "))
- mail=""
- while verifmail(mail)==0:
- mail=input ("Email: ")
- T.append(mail)
- tel=""
- while veriftel(tel)==0:
- tel=input ("Téléphone: ")
- T.append(tel)
- with open('tableur.csv','a') as f:
- writer=csv.writer(f, lineterminator = '\n')
- writer.writerow(T)
- f.close ()
- def modif (): #Fonction qui modifie un contact
- ch=input ("Quel contact voulez-vous modifier ? : ")
- rep=-1
- while (rep !=1 and rep!=2 and rep!=0):
- print ("0: E-mail")
- print ("1: Téléphone")
- print ("2: Les deux")
- rep=int(input ("Quel donnée voulez-vous modifier ?"))
- if (rep==0):
- new1=input("Donner le nouveau email: ")
- elif (rep==1):
- new1= input("Donner le nouveau numéro: ")
- elif (rep==2):
- new1= input ("Donner le nouveau email: ")
- new2= input ("Donner le nouveau téléphone: ")
- l=list()
- x=0
- with open ('tableur.csv','r') as f:
- obj = csv.reader(f)
- for ligne in obj:
- if (ligne[0]==ch):
- if (rep==0):
- ligne[1]=new1
- elif (rep==1):
- ligne[2]=new2
- elif (rep==2):
- ligne[1]=new1
- ligne[2]=new2
- x=1
- if (x!=1 and x!=2):
- x=2
- l.append(ligne)
- f.close()
- with open ('tableur.csv','w') as f:
- writer = csv.writer(f,lineterminator = '\n')
- writer.writerows(l)
- f.close()
- if (x==1):
- print (f"{bcolors.OK}Modification effectuée avec succès !{bcolors.RESET}")
- elif (x==2):
- print (f"{bcolors.WARNING}Contact introuvable !{bcolors.RESET}")
- elif (x==0):
- print (f"{bcolors.FAIL}Erreur système !{bcolors.RESET}")
- def rech (): #Fonction qui recherche un contact
- ch=input ("Quel contact rechercher-vous ? :")
- with open ('tableur.csv','r') as f:
- obj = csv.reader(f)
- x=0
- for ligne in obj:
- if ligne[0]==ch :
- print ("Nom: ",ligne[0]," /// Email: ",ligne[1]," /// Num: ",ligne[2])
- x=1
- break
- if (x==0):
- print (f"{bcolors.FAIL}Contact introuvable.{bcolors.RESET}")
- f.close()
- def supp (): #Fonction qui supprime un contact
- ch=input("Quel contact voulez-vous supprimer? : ")
- l=list()
- x=0
- with open ('tableur.csv','r') as f:
- obj = csv.reader(f)
- for ligne in obj:
- l.append(ligne)
- for field in ligne:
- if field==ch :
- l.remove(ligne)
- x=1
- f.close()
- with open ('tableur.csv','w') as f:
- writer = csv.writer(f,lineterminator = '\n')
- writer.writerows(l)
- f.close()
- if (x==1):
- print (f"{bcolors.OK}Contact supprimé avec succès !.{bcolors.RESET}")
- def affiche (): #Fonction qui affiche tout les contacts
- with open ('tableur.csv','r') as f:
- obj = csv.reader(f)
- for ligne in obj:
- print("Nom: ",ligne[0],"/Email: ",ligne[1],"/Num: ",ligne[2])
- f.close()
- def delall (): #RESET
- rep=input ("Voulez-vous vraiment tout effacer? (Y/N) : ")
- if (rep=='Y'):
- ch=""
- while (rep!=ch):
- ch=random()
- print (ch)
- rep=input ("Saisir le code : ")
- if (rep!=ch):
- print ("Mauvais code un autre code va être généré")
- with open ('tableur.csv','w') as f:
- f.close()
- n=0
- while (n!=7):
- while (n>7 or n<1):
- choix()
- n=int(input ("Votre choix: "))
- if (n==1):
- ajout()
- elif (n==2):
- modif()
- elif (n==3):
- rech()
- elif (n==4):
- supp()
- elif (n==5):
- affiche()
- elif (n==6):
- delall()
- if (n!=7):
- n=0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement