from dataclasses import dataclass import json from typing import Iterable @dataclass class Poem: name: str authors: str | list[str] body: str def load_data() -> list[Poem]: with open("task1_poems.json", "r") as file: return [ Poem(**obj) for obj in json.load(file) ] def ask_author_to_search() -> str: return input("Имя автора для поиска: ") def search_by_author(poems: list[Poem], author: str) -> list[Poem]: return [ poem for poem in poems if isinstance(poem.authors, str) and poem.authors == author\ or author in poem.authors ] def get_sorted_poems(poems: list[Poem]) -> list[Poem]: return sorted(poems, key=lambda poem: len(poem.body)) def print_poems(poems: Iterable[Poem]) -> None: for poem in poems: authors: str = poem.authors if isinstance(poem.authors, str) \ else ", ".join(poem.authors) print(f"{authors} - {poem.name} ({len(poem.body)} символов)") poems = load_data() author: str = ask_author_to_search() poems = search_by_author(poems, author) poems = get_sorted_poems(poems) print_poems(poems)