-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
45 lines (39 loc) · 1.74 KB
/
models.py
File metadata and controls
45 lines (39 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from datetime import datetime
from typing import Dict, Union
class Task:
"""Represents a single to-do task."""
def __init__(self, task_id: int, name: str, finished: bool, created_at: datetime):
"""
Initializes a new Task object.
Args:
task_id (int): The unique identifier for the task.
name (str): The description of the task.
finished (bool): The completion status of the task.
created_at (datetime): The timestamp when the task was created.
"""
self.task_id = task_id
self.name = name
self.finished = finished
self.created_at = created_at
def __repr__(self) -> str:
"""Provides a developer-friendly string representation of the Task object."""
status = "Done" if self.finished else "Pending"
return (f"Task(id={self.task_id}, name='{self.name}', status='{status}', "
f"created='{self.created_at.strftime('%Y-%m-%d %H:%M')}')")
def to_dict(self) -> Dict[str, Union[int, str, bool]]:
"""Converts the Task object to a dictionary for CSV writing."""
return {
"id": self.task_id,
"name": self.name,
"finished": "Y" if self.finished else "N",
"created_at": self.created_at.strftime("%Y-%m-%d %H:%M:%S")
}
@staticmethod
def from_dict(data: Dict[str, str]) -> 'Task':
"""Creates a Task object from a dictionary (e.g., a row from a CSV)."""
return Task(
task_id=int(data["id"]),
name=data["name"],
finished=True if data["finished"] == "Y" else False,
created_at=datetime.strptime(data["created_at"], "%Y-%m-%d %H:%M:%S")
)