Advertisement
LOVEGUN

Projet Python

May 4th, 2022
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. import csv
  2. import mailbox
  3. from random import randint
  4. import string
  5.  
  6. class bcolors: #Déclaration d'une classe qui va nous permettre d'afficher des message en couleur
  7.     OK = '\033[92m' #VERT
  8.     WARNING = '\033[93m' #JAUNE
  9.     FAIL = '\033[91m' #ROUGE
  10.     RESET = '\033[0m' #PARDEFAUT
  11.  
  12. def random (): #Fonction qui génére un code aléatoire pour confirmer le reset du repertoire
  13.     ch=""
  14.     for i in range (6):
  15.         if (randint(1,2)==1):
  16.             ch=ch+chr(randint(48,57))
  17.         else:
  18.             ch=ch+chr(randint(65,90))
  19.     return (ch)
  20.  
  21. def choix (): #Fonction menu
  22.     print ("Que voulez-vous faire ?")
  23.     print ("1. Ajouter un contact")
  24.     print ("2. Modifier un contact")
  25.     print ("3. Rechercher un contact")
  26.     print ("4. Supprimer un contact")
  27.     print ("5. Voir la liste des contacts")
  28.     print ("6. Effacer l'ensemble des contacts")
  29.     print ("7. Quitter")
  30.  
  31. def veriftel (ch): #Vérifie si numéro est uniquement cmposé de nombre
  32.     if len (ch)!=8 or ch[0]=='0':
  33.         return 0
  34.     for i in range(len(ch)):
  35.         if (ord(ch[i])>57 or ord(ch[i])<48):
  36.             return 0
  37.     return 1
  38.  
  39. def verifmail (ch): #Vérifie le format d'un email
  40.     if (ch.find("@")!=ch.rfind("@") or ch.find("@")==-1 or ch.find("@")>ch.find(".") or ch.find(".")==-1):
  41.         return 0
  42.     return 1
  43.  
  44. def ajout ():   #Fonction ajout d'un contact
  45.     T=[]
  46.     T.append(input ("Nom: "))
  47.     mail=""
  48.     while verifmail(mail)==0:
  49.         mail=input ("Email: ")
  50.     T.append(mail)
  51.     tel=""
  52.     while veriftel(tel)==0:
  53.         tel=input ("Téléphone: ")
  54.     T.append(tel)
  55.     with open('tableur.csv','a') as f:
  56.         writer=csv.writer(f, lineterminator = '\n')
  57.         writer.writerow(T)
  58.         f.close ()
  59.  
  60. def modif (): #Fonction qui modifie un contact
  61.     ch=input ("Quel contact voulez-vous modifier ? : ")
  62.     rep=-1
  63.     while (rep !=1 and rep!=2 and rep!=0):
  64.         print ("0: E-mail")
  65.         print ("1: Téléphone")
  66.         print ("2: Les deux")
  67.         rep=int(input ("Quel donnée voulez-vous modifier ?"))
  68.     if (rep==0):
  69.         new1=input("Donner le nouveau email: ")
  70.     elif (rep==1):
  71.         new1= input("Donner le nouveau numéro: ")
  72.     elif (rep==2):
  73.         new1= input ("Donner le nouveau email: ")
  74.         new2= input ("Donner le nouveau téléphone: ")
  75.     l=list()
  76.     x=0
  77.     with open ('tableur.csv','r') as f:
  78.         obj = csv.reader(f)
  79.         for ligne in obj:
  80.             if (ligne[0]==ch):
  81.                 if (rep==0):
  82.                     ligne[1]=new1
  83.                 elif (rep==1):
  84.                     ligne[2]=new2
  85.                 elif (rep==2):
  86.                     ligne[1]=new1
  87.                     ligne[2]=new2
  88.                 x=1
  89.             if (x!=1 and x!=2):
  90.                 x=2
  91.             l.append(ligne)
  92.         f.close()    
  93.     with open ('tableur.csv','w') as f:
  94.         writer = csv.writer(f,lineterminator = '\n')
  95.         writer.writerows(l)
  96.         f.close()
  97.     if (x==1):
  98.         print (f"{bcolors.OK}Modification effectuée avec succès !{bcolors.RESET}")
  99.     elif (x==2):
  100.         print (f"{bcolors.WARNING}Contact introuvable !{bcolors.RESET}")
  101.     elif (x==0):
  102.         print (f"{bcolors.FAIL}Erreur système !{bcolors.RESET}")
  103.  
  104. def rech (): #Fonction qui recherche un contact
  105.     ch=input ("Quel contact rechercher-vous ? :")
  106.     with open ('tableur.csv','r') as f:
  107.         obj = csv.reader(f)
  108.         x=0
  109.         for ligne in obj:
  110.             if ligne[0]==ch :
  111.                 print ("Nom: ",ligne[0]," /// Email: ",ligne[1]," /// Num: ",ligne[2])
  112.                 x=1
  113.                 break
  114.         if (x==0):
  115.             print (f"{bcolors.FAIL}Contact introuvable.{bcolors.RESET}")
  116.         f.close()
  117.  
  118. def supp (): #Fonction qui supprime un contact
  119.     ch=input("Quel contact voulez-vous supprimer? : ")
  120.     l=list()
  121.     x=0
  122.     with open ('tableur.csv','r') as f:
  123.         obj = csv.reader(f)
  124.         for ligne in obj:
  125.             l.append(ligne)
  126.             for field in ligne:
  127.                 if field==ch :
  128.                     l.remove(ligne)
  129.                     x=1
  130.         f.close()
  131.     with open ('tableur.csv','w') as f:
  132.         writer = csv.writer(f,lineterminator = '\n')
  133.         writer.writerows(l)
  134.         f.close()
  135.     if (x==1):
  136.         print (f"{bcolors.OK}Contact supprimé avec succès !.{bcolors.RESET}")
  137.        
  138. def affiche (): #Fonction qui affiche tout les contacts
  139.     with open ('tableur.csv','r') as f:
  140.         obj = csv.reader(f)
  141.         for ligne in obj:
  142.                 print("Nom: ",ligne[0],"/Email: ",ligne[1],"/Num: ",ligne[2])
  143.     f.close()
  144.  
  145. def delall (): #RESET
  146.    rep=input ("Voulez-vous vraiment tout effacer? (Y/N) : ")
  147.    if (rep=='Y'):
  148.     ch=""
  149.     while (rep!=ch):
  150.             ch=random()
  151.             print (ch)
  152.             rep=input ("Saisir le code : ")
  153.             if (rep!=ch):
  154.                 print ("Mauvais code un autre code va être généré")
  155.     with open ('tableur.csv','w') as f:
  156.         f.close()
  157.  
  158. n=0
  159. while (n!=7):
  160.     while (n>7 or n<1):
  161.             choix()
  162.             n=int(input ("Votre choix: "))
  163.     if (n==1):
  164.         ajout()
  165.     elif (n==2):
  166.         modif()
  167.     elif (n==3):
  168.         rech()
  169.     elif (n==4):
  170.         supp()
  171.     elif (n==5):
  172.         affiche()
  173.     elif (n==6):
  174.         delall()
  175.     if (n!=7):
  176.         n=0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement