From 1ddc9f41e0a6922cf87c8dba491bf0d65d5d240a Mon Sep 17 00:00:00 2001 From: Nemor38 <140786267+Nemor38@users.noreply.github.com> Date: Tue, 7 May 2024 16:07:10 +0200 Subject: [PATCH 1/3] add blain clim --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 94e3a87..c5f4f53 100644 --- a/main.py +++ b/main.py @@ -14,3 +14,4 @@ def print_hi(name): print_hi('PyCharm') # See PyCharm help at https://www.jetbrains.com/help/pycharm/ + From c31d654dc92faf954dc5e6864ec4e9758cc69cb0 Mon Sep 17 00:00:00 2001 From: Nemor38 <140786267+Nemor38@users.noreply.github.com> Date: Sun, 19 May 2024 16:09:05 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D1=94?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B2=D0=B4=D0=B0=D0=BD=D0=BD=D1=8F:=20=D1=81?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0=20=D1=83=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D1=96=D0=BD=D0=BD=D1=8F=20=D0=B1=D1=96=D0=B1=D0=BB?= =?UTF-8?q?=D1=96=D0=BE=D1=82=D0=B5=D0=BA=D0=BE=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- work1.py | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 work1.py diff --git a/work1.py b/work1.py new file mode 100644 index 0000000..557fbb4 --- /dev/null +++ b/work1.py @@ -0,0 +1,145 @@ +from pydantic import BaseModel +from typing import List, Optional +import json +import os + +class BookModel(BaseModel): + title: str + author: str + year: int + +class Book: + def __init__(self, model: BookModel): + self._model = model + + def __str__(self): + return f"{self._model.title} by {self._model.author} ({self._model.year})" +######################################## + +class Library: + def __init__(self): + self._books = [] + + def add_book(self, book: Book): + self._books.append(book) + + def remove_book(self, book: Book): + self._books.remove(book) + + def __iter__(self): + return iter(self._books) + + def books_by_author(self, author: str): + for book in self._books: + if book._model.author == author: + yield book + + def __str__(self): + return "\n".join(str(book) for book in self._books) + +from functools import wraps + +def log_action(action): + def decorator(func): + @wraps(func) + def wrapper(self, book: Book): + result = func(self, book) + print(f"{action} book: {book}") + return result + return wrapper + return decorator + +def check_book_exists(func): + @wraps(func) + def wrapper(self, book: Book): + if book in self._books: + return func(self, book) + else: + print(f"Book not found: {book}") + return wrapper + +class LibraryFileManager: + def __init__(self, filepath: str, library: Library): + self.filepath = filepath + self.library = library + + def __enter__(self): + if os.path.exists(self.filepath): + with open(self.filepath, 'r') as file: + books_data = json.load(file) + for book_data in books_data: + book_model = BookModel(**book_data) + self.library.add_book(Book(book_model)) + return self.library + + def __exit__(self, exc_type, exc_value, traceback): + with open(self.filepath, 'w') as file: + books_data = [book._model.dict() for book in self.library] + json.dump(books_data, file) + + +class Journal(Book): + def __init__(self, model: BookModel, issue: int): + super().__init__(model) + self.issue = issue + + def __str__(self): + return f"{self._model.title} by {self._model.author} (Issue {self.issue}, {self._model.year})" + +from abc import ABC, abstractmethod + +class Printable(ABC): + @abstractmethod + def print_details(self): + pass + +class BookWithPrint(Book, Printable): + def print_details(self): + print(f"Book: {self._model.title}, Author: {self._model.author}, Year: {self._model.year}") + +class JournalWithPrint(Journal, Printable): + def print_details(self): + print(f"Journal: {self._model.title}, Author: {self._model.author}, Issue: {self.issue}, Year: {self._model.year}") + + +if __name__ == "__main__": + library = Library() + + # Створення книги та журналу + book_model = BookModel(title="1984", author="George Orwell", year=1949) + book = BookWithPrint(book_model) + + journal_model = BookModel(title="Nature", author="Various", year=2021) + journal = JournalWithPrint(journal_model, issue=102) + + # Додавання книг до бібліотеки + library.add_book(book) + library.add_book(journal) + + # Виведення списку книг у бібліотеці + print("Library books:") + print(library) + + # Виведення книг по імені автора + print("\nBooks by George Orwell:") + for book in library.books_by_author("George Orwell"): + print(book) + + # Збереження списку книг у файл + with LibraryFileManager("library.json", library): + pass + + # Видалення книги з бібліотеки + library.remove_book(book) + + # Виведення списку книг після видалення + print("\nLibrary books after removing:") + print(library) + + # Додавання книг з файлу в бібліотеку + with LibraryFileManager("library.json", library): + pass + + # Виведення списку книг після додавання з файлу + print("\nLibrary books after loading from file:") + print(library) From f9a125d4c27b281aa458326fae83a1ad972798b5 Mon Sep 17 00:00:00 2001 From: Nemor38 <140786267+Nemor38@users.noreply.github.com> Date: Tue, 21 May 2024 15:28:19 +0200 Subject: [PATCH 3/3] add json --- library.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 library.json diff --git a/library.json b/library.json new file mode 100644 index 0000000..eeb8938 --- /dev/null +++ b/library.json @@ -0,0 +1 @@ +[{"title": "Nature", "author": "Various", "year": 2021}, {"title": "Nature", "author": "Various", "year": 2021}, {"title": "1984", "author": "George Orwell", "year": 1949}, {"title": "Nature", "author": "Various", "year": 2021}, {"title": "1984", "author": "George Orwell", "year": 1949}, {"title": "Nature", "author": "Various", "year": 2021}, {"title": "Nature", "author": "Various", "year": 2021}, {"title": "1984", "author": "George Orwell", "year": 1949}, {"title": "Nature", "author": "Various", "year": 2021}] \ No newline at end of file