Add calendar implementation: models, storage, CLI
This commit is contained in:
35
models.py
Normal file
35
models.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
class Event:
|
||||
"""Класс события календаря"""
|
||||
|
||||
def __init__(self, title: str, date: str, time: str = "", description: str = ""):
|
||||
self.title = title
|
||||
self.date = date # формат YYYY-MM-DD
|
||||
self.time = time # формат HH:MM
|
||||
self.description = description
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Преобразует событие в словарь для JSON"""
|
||||
return {
|
||||
"title": self.title,
|
||||
"date": self.date,
|
||||
"time": self.time,
|
||||
"description": self.description
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> 'Event':
|
||||
"""Создаёт событие из словаря"""
|
||||
return cls(
|
||||
title=data["title"],
|
||||
date=data["date"],
|
||||
time=data.get("time", ""),
|
||||
description=data.get("description", "")
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
time_str = f" в {self.time}" if self.time else ""
|
||||
return f"{self.date}{time_str}: {self.title} - {self.description}"
|
||||
Reference in New Issue
Block a user