74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
import json
|
|
|
|
# Classes utilisées pour représenter des données
|
|
class TodoElement():
|
|
STATE_NOT_STARTED = 0 # Sorte d'enum qui représente l'état d'une tâche
|
|
STATE_STARTED = 1
|
|
STATE_COMPLETED = 2
|
|
|
|
name: str
|
|
state: int
|
|
|
|
def __init__(self, name:str, description:str=None, state:int=0):
|
|
self.name = name
|
|
self.description = description
|
|
self.state = state
|
|
|
|
def __str__(self)->str:
|
|
"""
|
|
Affiche la tâche, son nom et son statut
|
|
Affichera aussi la description de la tâche si elle a été définie ET est en cours
|
|
|
|
Returns:
|
|
str: Représentation écrite de la tâche
|
|
"""
|
|
return f"Tâche \"{self.name}\": {self.__getStateName()}." + \
|
|
(f" Description: {self.description}" if self.description and self.state == TodoElement.STATE_STARTED else '')
|
|
|
|
def __getStateName(self)->str:
|
|
if self.state == TodoElement.STATE_NOT_STARTED:
|
|
return "Non commencée"
|
|
elif self.state == TodoElement.STATE_STARTED:
|
|
return "En cours"
|
|
elif self.state == TodoElement.STATE_COMPLETED:
|
|
return "Terminée"
|
|
else:
|
|
return "Inconnu"
|
|
|
|
def toJSON(self, indent:int=None)->str: # Vient de https://github.com/LJ5O/Assistant/blob/main/modules/Brain/src/Json/Types.py
|
|
"""
|
|
Exporter cet objet vers une String JSON. Permet de le passer dans le State
|
|
|
|
Returns:
|
|
str: String sérialisable via la méthode statique TodoElement.strImport(string)
|
|
"""
|
|
return '{"name":"'+ str(self.name) +'", "desc": "'+str(self.description)+'", "state": ' + str(self.state) +'}'
|
|
|
|
@staticmethod
|
|
def fromJSON(json_str: str|dict) -> 'InterruptPayload':
|
|
"""
|
|
Parse a JSON string to create a TodoElement instance
|
|
|
|
Args:
|
|
json_str (str|dict): JSON string to parse, or JSON shaped dict
|
|
|
|
Returns:
|
|
TodoElement: instance created from JSON data
|
|
"""
|
|
data = json.loads(json_str) if type(json_str) is str else json_str
|
|
|
|
nom_ = data.get("name", "undefined")
|
|
desc_ = data.get("desc", "undefined")
|
|
state_ = data.get("state", TodoElement.STATE_NOT_STARTED)
|
|
|
|
return TodoElement(nom_, desc_, state_)
|
|
|
|
if __name__ == "__main__":
|
|
test = TodoElement("TEST tâche", "OUI")
|
|
test.state = TodoElement.STATE_STARTED
|
|
print(test)
|
|
print([str(test)])
|
|
print(test.toJSON())
|
|
|
|
print(TodoElement.fromJSON(test.toJSON()))
|