35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
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}" |