2024-03-27 14:17:05 +01:00
|
|
|
from pydantic import BaseModel, RootModel
|
|
|
|
from typing import Optional, List
|
|
|
|
import json
|
2024-03-27 11:18:57 +01:00
|
|
|
|
|
|
|
class Athlete(BaseModel):
|
|
|
|
"""
|
|
|
|
Modèle Athlète
|
|
|
|
"""
|
2024-03-27 14:17:05 +01:00
|
|
|
id: Optional[int] = 0
|
2024-03-27 11:18:57 +01:00
|
|
|
prenom: str
|
|
|
|
nom: str
|
|
|
|
pays: str
|
|
|
|
sexe: str = "N/A"
|
|
|
|
image: str = "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_640.png"
|
2024-03-27 14:17:05 +01:00
|
|
|
disciplines: Optional[List[int]] = None
|
|
|
|
records: Optional[List[int]] = None
|
|
|
|
|
|
|
|
def loadFromJsonData(self, data: str):
|
|
|
|
"""
|
|
|
|
Charge les données depuis une chaine json
|
|
|
|
|
|
|
|
:param data: Données json
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
data = json.loads(data)
|
|
|
|
for key, value in data.items():
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
class ListeAthlete(RootModel):
|
|
|
|
root: List[Athlete] = []
|
|
|
|
def loadFromJson(self, path: str):
|
|
|
|
"""
|
|
|
|
Charge les données depuis un fichier json
|
|
|
|
|
|
|
|
:param path: Chemin du fichier json
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
for athlete in data:
|
|
|
|
self.root.append(Athlete(**athlete))
|
|
|
|
except FileNotFoundError:
|
2024-03-27 15:20:10 +01:00
|
|
|
print(f"Le fichier {path} n'existe pas")
|
2024-03-27 14:17:05 +01:00
|
|
|
def loadFromJsonData(self, data: str):
|
|
|
|
"""
|
|
|
|
Charge les données depuis une chaine json
|
|
|
|
:param data: Données json
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
data = json.loads(data)
|
|
|
|
for athlete in data:
|
|
|
|
self.root.append(Athlete(**athlete))
|