53 lines
No EOL
1.5 KiB
Python
53 lines
No EOL
1.5 KiB
Python
from pydantic import BaseModel, RootModel
|
|
from typing import Optional, List
|
|
import json
|
|
|
|
class Athlete(BaseModel):
|
|
"""
|
|
Modèle Athlète
|
|
"""
|
|
id: Optional[int] = 0
|
|
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"
|
|
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:
|
|
print(f"Le fichier {path} n'existe pas")
|
|
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)) |