50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
|
from pydantic import BaseModel, RootModel
|
||
|
from typing import Optional, List
|
||
|
import json
|
||
|
|
||
|
class Discipline(BaseModel):
|
||
|
"""
|
||
|
Modèle Discipline
|
||
|
"""
|
||
|
id: Optional[int] = 0
|
||
|
intitule: str
|
||
|
type: str
|
||
|
description: str
|
||
|
logo: str
|
||
|
|
||
|
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 ListeDiscipline(RootModel):
|
||
|
root: List[Discipline] = []
|
||
|
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(Discipline(**athlete))
|
||
|
except FileNotFoundError:
|
||
|
print("Fichier introuvable")
|
||
|
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(Discipline(**athlete))
|