TodoElement est maintenant sérializable
Requis pour le state
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
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
|
||||
@@ -7,10 +9,10 @@ class TodoElement():
|
||||
name: str
|
||||
state: int
|
||||
|
||||
def __init__(self, name:str, description:str=None):
|
||||
def __init__(self, name:str, description:str=None, state:int=0):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.state = TodoElement.STATE_NOT_STARTED
|
||||
self.state = state
|
||||
|
||||
def __str__(self)->str:
|
||||
"""
|
||||
@@ -33,8 +35,39 @@ class TodoElement():
|
||||
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()))
|
||||
|
||||
Reference in New Issue
Block a user