From 5498693b04da46332818d368b38b2525b4552995 Mon Sep 17 00:00:00 2001 From: Melnyk1990 Date: Thu, 11 Apr 2024 19:55:42 +0300 Subject: [PATCH 1/2] add tast_1 --- Lesson_2.py | 194 ++++++++++++++++++++++++++++++++++++ json_list.json | 7 ++ list_books_and_journal.json | 4 + 3 files changed, 205 insertions(+) create mode 100644 Lesson_2.py create mode 100755 json_list.json create mode 100755 list_books_and_journal.json diff --git a/Lesson_2.py b/Lesson_2.py new file mode 100644 index 0000000..e7c147b --- /dev/null +++ b/Lesson_2.py @@ -0,0 +1,194 @@ +import json + +from pydantic import BaseModel +from abc import ABC, abstractmethod + + +def logger(func): + def wrapper(*args, **kwargs): + result = func(*args, **kwargs) + print(f'Add book or journal: {result.item_info()}') + return result + return wrapper +def delet(func): + def wrapper(*args, **kwargs): + print('Trying to find a book or magazine to remove from this library list:') + lib.show_library() + result = func(*args, **kwargs) + return result + return wrapper + +class BookModel(BaseModel): + name: str + author: str + year: int + +class JournalModel(BookModel): + edition: int + +class AbstractLibraryItem(ABC): + @abstractmethod + def item_info(self): + raise NotImplementedError + +class Book(AbstractLibraryItem): + def __init__(self, model: BookModel): + self.model = model + def item_info(self): + return {self.model.name:{'author':self.model.author, "year":self.model.year}} + + +class Journal(AbstractLibraryItem): + def __init__(self, model: JournalModel): + self.model = model + + def item_info(self): + return {self.model.name:{'author':self.model.author, "year":self.model.year, "edition":self.model.edition}} + + + +class Library: + def __init__(self): + self.item_list = [] + + @logger + def add_book(self, item: AbstractLibraryItem): + self.item_list.append(item) + return item + + + def get_item_by_author(self, name:str): + list_books_journal = [] + gener = (i for i in self.item_list) + for book in gener: + if book.model.author == name: + list_books_journal.append(book.item_info()) + if list_books_journal: + return print(list_books_journal) + else: + print(f'Нет книг автора {name} в списке!') + + + def show_library(self): + for book_and_journal in self.item_list: + print(book_and_journal.item_info()) + + def save_list_book_journal(self): + try: + with open('list_books_and_journal.json', 'w', encoding='utf-8') as file: + for i in self.item_list: + file.write(str(i.item_info()) + '\n') + except Exception as e: + print(f'Error add: {e}') + @delet + def delete_book_or_journal(self, name:str): + for item in self.item_list: + if item.model.name == name: + self.item_list.remove(item) + print(f'Book of journal "{name}" has been removed from the library') + break + else: + print(f'Book of journal "{name}" not in the library') + # + def show_list_books_after_deletion(self): + self.item_list.clear() + print('Library cleared') + + def add_books_is_file(self): + try: + with open('json_list.json', 'r', encoding='utf-8') as file_json: + data = json.load(file_json) # Загрузка всего файла как JSON массив + for line in data: + if 'edition' in line: + journal_model = JournalModel(**line) + journal = Journal(journal_model) + self.add_book(journal) + else: + book_model = BookModel(**line) + book = Book(book_model) + self.add_book(book) + except Exception as e: + print(f'Error add: {e}') + +journal_1 = {'name': 'Forbes', + 'author': 'Charlz', + 'year': 2005, + 'edition':88 +} + +book_1 = {'name': 'Mavka', + 'author': 'Ukrayinka', + 'year': 2020, +} +book_2 = {'name': 'Zapovit', + 'author': 'Shevchenko', + 'year': 2020, +} + +book_3 = {'name': 'Kobzar', + 'author': 'Shevchenko', + 'year': 2021, +} + + if __name__ == '__main__': + print('**** Add book or journal *****') + lib = Library() + journal_model = JournalModel(**journal_1) + journal = Journal(journal_model) + lib.add_book(journal) + print() + + print('**** Add book or journal *****') + book_model = BookModel(**book_1) + book = Book(book_model) + lib.add_book(book) + print() + + print('**** Add book or journal *****') + book_model = BookModel(**book_2) + book = Book(book_model) + lib.add_book(book) + print() + print() + print('**** Add book or journal *****') + book_model = BookModel(**book_3) + book = Book(book_model) + lib.add_book(book) + print( '\t' * 3, ' **** Show entire library*****') + lib.show_library() + + + # + print() + print('**** Search for a book or journal by author *****') + lib.get_item_by_author('Shevchenko') #input АВТОРА + # + print() + print('**** Saving to file *****') + lib.save_list_book_journal() + # + print() + print('**** Removing a book from the library *****') + lib.delete_book_or_journal('Mavka') + # + print() + print('**** Displaying a list of books or journals after deletion *****') + lib.show_library() + # + print() + print('**** Clear entire library *****') + lib.show_list_books_after_deletion() + # + print() + print('**** Displaying a list of books of journals after deletion *****') + lib.show_library() + # + print() + print('**** Adding books of journals from a file *****') + lib.add_books_is_file() + + print() + print('**** Show entire library *****') + lib.show_library() + + diff --git a/json_list.json b/json_list.json new file mode 100755 index 0000000..d82bd4c --- /dev/null +++ b/json_list.json @@ -0,0 +1,7 @@ +[{"name": "Zapovit", "author": "Shevchenko", "year": 2015}, +{"name": "Zhaga", "author": "Vulf", "year": 2018}, +{"name": "Ubivchij vpliv", "author": "Omer", "year": 2020}, +{"name": "Grona gnivu", "author": "Stejnbek", "year": 2023}, +{"name": "Den popelu", "author": "Granzheвна", "year": 2024}, +{"name": "New Look", "author": "Rey", "year": 512, "edition": 123}, +{"name": "Vogue", "author": "Boucher", "year": 512, "edition": 245}] \ No newline at end of file diff --git a/list_books_and_journal.json b/list_books_and_journal.json new file mode 100755 index 0000000..25688be --- /dev/null +++ b/list_books_and_journal.json @@ -0,0 +1,4 @@ +{'Forbes': {'author': 'Charlz', 'year': 2005, 'edition': 88}} +{'Mavka': {'author': 'Ukrayinka', 'year': 2020}} +{'Zapovit': {'author': 'Shevchenko', 'year': 2020}} +{'Kobzar': {'author': 'Shevchenko', 'year': 2021}} From f631c7416ad053851f124c23b18ecd5536070fa7 Mon Sep 17 00:00:00 2001 From: Melnyk1990 <143398509+Melnyk1990@users.noreply.github.com> Date: Fri, 12 Apr 2024 08:26:02 +0300 Subject: [PATCH 2/2] Update Lesson_2.py --- Lesson_2.py | 161 ++++++++++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 81 deletions(-) diff --git a/Lesson_2.py b/Lesson_2.py index e7c147b..70f3963 100644 --- a/Lesson_2.py +++ b/Lesson_2.py @@ -109,86 +109,85 @@ def add_books_is_file(self): self.add_book(book) except Exception as e: print(f'Error add: {e}') - -journal_1 = {'name': 'Forbes', - 'author': 'Charlz', - 'year': 2005, - 'edition':88 -} - -book_1 = {'name': 'Mavka', - 'author': 'Ukrayinka', - 'year': 2020, -} -book_2 = {'name': 'Zapovit', - 'author': 'Shevchenko', - 'year': 2020, -} - -book_3 = {'name': 'Kobzar', - 'author': 'Shevchenko', - 'year': 2021, -} - - if __name__ == '__main__': - print('**** Add book or journal *****') - lib = Library() - journal_model = JournalModel(**journal_1) - journal = Journal(journal_model) - lib.add_book(journal) - print() - - print('**** Add book or journal *****') - book_model = BookModel(**book_1) - book = Book(book_model) - lib.add_book(book) - print() - - print('**** Add book or journal *****') - book_model = BookModel(**book_2) - book = Book(book_model) - lib.add_book(book) - print() - print() - print('**** Add book or journal *****') - book_model = BookModel(**book_3) - book = Book(book_model) - lib.add_book(book) - print( '\t' * 3, ' **** Show entire library*****') - lib.show_library() - - - # - print() - print('**** Search for a book or journal by author *****') - lib.get_item_by_author('Shevchenko') #input АВТОРА - # - print() - print('**** Saving to file *****') - lib.save_list_book_journal() - # - print() - print('**** Removing a book from the library *****') - lib.delete_book_or_journal('Mavka') - # - print() - print('**** Displaying a list of books or journals after deletion *****') - lib.show_library() - # - print() - print('**** Clear entire library *****') - lib.show_list_books_after_deletion() - # - print() - print('**** Displaying a list of books of journals after deletion *****') - lib.show_library() - # - print() - print('**** Adding books of journals from a file *****') - lib.add_books_is_file() - - print() - print('**** Show entire library *****') - lib.show_library() +if __name__ == '__main__': + journal_1 = {'name': 'Forbes', + 'author': 'Charlz', + 'year': 2005, + 'edition':88 + } + + book_1 = {'name': 'Mavka', + 'author': 'Ukrayinka', + 'year': 2020, + } + book_2 = {'name': 'Zapovit', + 'author': 'Shevchenko', + 'year': 2020, + } + + book_3 = {'name': 'Kobzar', + 'author': 'Shevchenko', + 'year': 2021, + } + + print('**** Add book or journal *****') + lib = Library() + journal_model = JournalModel(**journal_1) + journal = Journal(journal_model) + lib.add_book(journal) + print() + + print('**** Add book or journal *****') + book_model = BookModel(**book_1) + book = Book(book_model) + lib.add_book(book) + print() + + print('**** Add book or journal *****') + book_model = BookModel(**book_2) + book = Book(book_model) + lib.add_book(book) + print() + print() + print('**** Add book or journal *****') + book_model = BookModel(**book_3) + book = Book(book_model) + lib.add_book(book) + print( '\t' * 3, ' **** Show entire library*****') + lib.show_library() + + + # + print() + print('**** Search for a book or journal by author *****') + lib.get_item_by_author('Shevchenko') #input АВТОРА + # + print() + print('**** Saving to file *****') + lib.save_list_book_journal() + # + print() + print('**** Removing a book from the library *****') + lib.delete_book_or_journal('Mavka') + # + print() + print('**** Displaying a list of books or journals after deletion *****') + lib.show_library() + # + print() + print('**** Clear entire library *****') + lib.show_list_books_after_deletion() + # + print() + print('**** Displaying a list of books of journals after deletion *****') + lib.show_library() + # + print() + print('**** Adding books of journals from a file *****') + lib.add_books_is_file() + + print() + print('**** Show entire library *****') + lib.show_library()