Semaine 4, jour 3, soir

This commit is contained in:
gauvainboiche
2026-01-14 22:06:43 +01:00
parent 1db1a5ab8d
commit 4c9c3b29e8
11 changed files with 275 additions and 87 deletions

View File

@@ -0,0 +1 @@
3.12

View File

View File

@@ -0,0 +1,91 @@
import json
class Contact:
def __init__(self, name: str, phone: str, email: str):
self.name = name
self.phone = phone
self.email = email
def __iter__(self):
yield self.name
yield self.phone
yield self.email
def __repr__(self) -> str:
return f"{self.name} - {self.phone} - {self.email}"
def __eq__(self, other: "Contact"):
return self.name == other.name\
and self.phone == other.phone\
and self.email == other.email
def serialized(self):
"""Converti la classe en dictionnaire JSONable"""
return {"Name": self.name, "Phone": self.phone, "Email": self.email}
@classmethod
def deserialized(cls, dictionnary):
"""Lis un JSON pour le retranscrire en classe Contact"""
return cls(name= dictionnary["Name"], phone= dictionnary["Phone"], email= dictionnary["Email"])
class Repertory:
def __init__(self, name: str):
self.name = name
self.contacts: list[Contact] = self.load()
def __iter__(self):
return iter(self.contacts)
def __getitem__(self, index: int) -> Contact:
return self.contacts[index]
def add_contact(self, contact: Contact):
if contact in self.contacts:
return f"{contact.name} est déjà dans le répertoire."
self.contacts.append(contact)
self.save()
return f"Nouveau contact :\nNom : {contact.name}\nNuméro : {contact.phone}\nCourriel : {contact.email}"
def delete_contact(self, contact_info: str):
for contact in self.contacts:
if contact.name.lower() == contact_info.lower()\
or contact.phone.lower() == contact_info.lower()\
or contact.email.lower() == contact_info.lower():
self.contacts.remove(contact)
self.save()
return f"{contact.name} a été supprimé du répertoire."
return f"{contact.name} n'est pas dans le répertoire."
def search_contact(self, research_str: str):
results = []
for contact in self.contacts:
if research_str.lower() in contact.name.lower()\
or research_str.lower() in contact.phone.lower()\
or research_str.lower() in contact.email.lower():
results.append(contact)
return results if len(results) > 0 else "Pas de résultat."
def display_contacts(self):
results = []
for idx, contact in enumerate(self.contacts):
results.append(
f"----- Contact n°{idx + 1} -----\n"
f"Nom : {contact.name}\n"
f"Numéro : {contact.phone}\n"
f"Courriel : {contact.email}\n"
" "
)
return "".join(results) if results else "Aucun contact dans le répertoire."
# Sauvegarde des livres dans un JSON pour persistance des données
def save(self):
"""Inscris dans un fichier JSON le contenu de la bibliothèque"""
contact_dict = [contact.serialized() for contact in self.contacts]
with open("contacts.json", "w") as json_file: # a pour append, w pour write
json.dump(contact_dict, json_file, indent= 4)
def load(self) -> list[Contact]:
"""Récupère le contenu d'un fichier JSON"""
with open("contacts.json") as json_file:
return json.load(json_file, object_hook= Contact.deserialized)

View File

@@ -0,0 +1,27 @@
[
{
"Name": "Alberto",
"Phone": "0612345678",
"Email": "alberto@courriel.fr"
},
{
"Name": "Alice",
"Phone": "0123456789",
"Email": "alice@gmail.com"
},
{
"Name": "Alberto",
"Phone": "0482764563",
"Email": "kfjsbdfg@sdfgj.fr"
},
{
"Name": "Giorgio",
"Phone": "857398375493",
"Email": "giorgio.armani@armagnac.it"
},
{
"Name": "Fran\u00e7ois LE GUERAND",
"Phone": "0145892367",
"Email": "fr-le-g@gmail.fr"
}
]

View File

@@ -0,0 +1,18 @@
from classes import Contact,Repertory
repertory = Repertory("Repertoire")
for i in range(1):
new_contact_name = input("Nom > ")
new_contact_phone = input("Numéro > ")
new_contact_email = input("Courriel > ")
new_contact = Contact(new_contact_name, new_contact_phone, new_contact_email)
new_contact_added = repertory.add_contact(new_contact)
print(new_contact_added)
search_criteria = input("Critères de recherche > ")
print(repertory.search_contact(search_criteria))
print(repertory.display_contacts())

View File

@@ -0,0 +1,7 @@
[project]
name = "contacts"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

8
Semaine_04/Contacts/uv.lock generated Normal file
View File

@@ -0,0 +1,8 @@
version = 1
revision = 3
requires-python = ">=3.12"
[[package]]
name = "contacts"
version = "0.1.0"
source = { virtual = "." }