diff --git a/Tp4/etudiants/etudiant.py b/Tp4/etudiants/etudiant.py old mode 100644 new mode 100755 index d647fa8343e455c5781b67c3955c29334c9b2f64..cc793f301bcaed1c9dfc4e23087c1c2b8e0fb375 --- a/Tp4/etudiants/etudiant.py +++ b/Tp4/etudiants/etudiant.py @@ -41,15 +41,23 @@ class Etudiant: $$$ isinstance(etu.naissance, Date) True """ + + + def __init__(self, nip: int, nom: str, prenom: str, naissance: Date, formation: str, groupe: str): """ - initialise un nouvel étudiant à partir de son nip, son nom, son + initialis-(e un nouvel étudiant à partir de son nip, son nom, son prénom, sa formation et son groupe. précondition : le nip, le nom et le prénom ne peuvent être nuls ou vides. """ - ... + self.prenom=prenom + self.nom=nom + self.nip=nip + self.naissance=naissance + self.formation=formation + self.groupe=groupe def __eq__(self, other) -> bool: """ @@ -59,26 +67,110 @@ class Etudiant: - même prénom que `self`, et False sinon. """ - ... - + if self.nip==other.nip and self.nom==other.nom and self.prenom==other.prenom: + return True + return False + + def __lt__(self, other) -> bool: """ Renvoie True si self est né avant other """ - ... - + + return self.naissance < other.naissance + + + def __str__(self) -> str: """ Renvoie une représentation textuelle de self. """ - ... + return f"{self.prenom} {self.nom}" def __repr__(self) -> str: """ Renvoie une représentation textuelle interne de self pour le shell. """ - ... + x=self.nom.upper() + return f"{self.nip} : {self.prenom} {x}" if (__name__ == "__main__"): import apl1test apl1test.testmod('etudiant.py') + + +def charge_fichier_etudiants(fname: str) -> list[Etudiant]: + """ + Renvoie la liste des étudiants présents dans le fichier dont + le nom est donné en paramètre. + + précondition: le fichier est du bon format. + """ + res = [] + with open(fname, 'r') as fin: + fin.readline() + ligne = fin.readline() + while ligne != '': + nip, nom, prenom, naissance, formation, groupe = ligne.strip().split(';') + y, m, d = naissance.split('-') + date_naiss = Date(int(d.lstrip('0')), int(m.lstrip('0')), int(y)) + res.append(Etudiant(nip, nom, prenom, date_naiss, formation, groupe)) + ligne = fin.readline() + return res + +L_ETUDIANTS=charge_fichier_etudiants("etudiants.csv") +COURTE_LISTE=L_ETUDIANTS[:10] + +def est_liste_d_etudiants(x) -> bool: + """ + Renvoie True si ``x`` est une liste de d'étudiant, False dans le cas contraire. + + Précondition: aucune + + Exemples: + + $$$ est_liste_d_etudiants(COURTE_LISTE) + True + $$$ est_liste_d_etudiants("Timoleon") + False + $$$ est_liste_d_etudiants([('12345678', 'Calbuth', 'Raymond', 'Danse', '12') ]) + False + """ + + if isinstance(x,list): + for element in x: + if not isinstance(element,Etudiant): + return False + return True + return False +#Q4: + +NBRE_ETUDIANTS=len(L_ETUDIANTS) + +#Q5: +NIP=42320929 + + + +#Q6: +def ensemble_des_formations(liste: list[Etudiant]) -> Set[str]: + """ + Renvoie un ensemble de chaînes de caractères donnant les formations + présentes dans les fiches d'étudiants + + Précondition: liste ne contient que des fiches d'étudiants + + Exemples: + + $$$ ensemble_des_formations(COURTE_LISTE) + {'MI', 'PEIP', 'MIASHS', 'LICAM'} + $$$ ensemble_des_formations(COURTE_LISTE[0:2]) + {'MI', 'MIASHS'} + """ + + formations=Set() + for etudiant in liste: + formations.add(etudiant.formation) + return formations + + \ No newline at end of file