From b2c2943830bd98b7b825d289657f1c0a51bae1b7 Mon Sep 17 00:00:00 2001 From: SyedHassanUlHaq <21b-029-cs@students.uit.edu> Date: Wed, 13 Sep 2023 11:24:45 +0500 Subject: [PATCH 1/4] Added Library Management System in Python --- Library Management System/main.py | 391 ++++++++++++++++++++++++++++++ 1 file changed, 391 insertions(+) create mode 100644 Library Management System/main.py diff --git a/Library Management System/main.py b/Library Management System/main.py new file mode 100644 index 00000000..638fce03 --- /dev/null +++ b/Library Management System/main.py @@ -0,0 +1,391 @@ +from abc import ABC, abstractmethod +from datetime import datetime, timedelta + +# Super Class representing a person +class Person: + def __init__(self, name, age, ID): + """ + Initialize a person with their name, age, and ID. + + Args: + name (str): The name of the person. + age (int): The age of the person. + ID (str): The unique ID of the person. + """ + self.name = name + self.age = age + self.ID = ID + + def get_details(self): + """ + Get details of the person, including ID, name, and age. + """ + print("ID: {}\nName: {}\nAge: {}".format(self.ID, self.name, self.age)) + + +# Sub Class of Person representing a student +class Student(Person): + def __init__(self, name, age, ID, student_ID): + """ + Initialize a student with additional attributes. + + Args: + name (str): The name of the student. + age (int): The age of the student. + ID (str): The unique ID of the student. + student_ID (str): The student's unique identification code. + """ + super().__init__(name, age, ID) + self.student_ID = student_ID + self.issued_books = [] # List of books issued to the student + self.borrowed_books = [] # List of books currently borrowed by the student + + def borrow_book(self, book): + """ + Allow the student to borrow a book if not exceeding the borrowing limit. + + Args: + book (LibraryItem): The book to be borrowed. + """ + if len(self.borrowed_books) < 3: # Student can borrow up to 3 books + self.borrowed_books.append(book) + book.checkout(self) + else: + print("You have reached the borrowing limit.") + + def return_book(self, book): + """ + Allow the student to return a borrowed book. + + Args: + book (LibraryItem): The book to be returned. + """ + if book in self.borrowed_books: + self.borrowed_books.remove(book) + book.return_item(self) + else: + print("You did not borrow this book.") + + def get_details(self): + """ + Get details of the student, including ID, name, age, student ID, and issued books. + """ + super().get_details() + print("Student ID: {}".format(self.student_ID)) + print("Issued Books: {}".format(", ".join(book.title for book in self.issued_books))) + + +# SubClass of Person representing a staff member +class Staff(Person): + def __init__(self, name, age, ID, staff_id, designation): + """ + Initialize a staff member with additional attributes. + + Args: + name (str): The name of the staff member. + age (int): The age of the staff member. + ID (str): The unique ID of the staff member. + staff_id (str): The staff member's unique identification code. + designation (str): The job designation of the staff member. + """ + super().__init__(name, age, ID) + self.staff_id = staff_id + self.designation = designation + self.issued_books = [] # List of books issued to the staff member + self.borrowed_books = [] # List of books currently borrowed by the staff member + + def borrow_book(self, book): + """ + Allow the staff member to borrow a book if not exceeding the borrowing limit. + + Args: + book (LibraryItem): The book to be borrowed. + """ + if len(self.borrowed_books) < 5: # Staff can borrow up to 5 books + self.borrowed_books.append(book) + book.checkout(self) + else: + print("You have reached the borrowing limit.") + + def return_book(self, book): + """ + Allow the staff member to return a borrowed book. + + Args: + book (LibraryItem): The book to be returned. + """ + if book in self.borrowed_books: + self.borrowed_books.remove(book) + book.return_item(self) + else: + print("You did not borrow this book.") + + def get_details(self): + """ + Get details of the staff member, including ID, name, age, staff ID, and designation. + """ + super().get_details() + print("Staff ID: {}".format(self.staff_id)) + print("Designation: {}".format(self.designation)) + + +# Abstract Class representing a library item +class LibraryItem(ABC): + def __init__(self, book_ID, title, author): + """ + Initialize a library item with its unique ID, title, and author. + + Args: + book_ID (int): The unique ID of the library item. + title (str): The title of the library item. + author (str): The author of the library item. + """ + self.book_ID = book_ID + self.title = title + self.author = author + self.borrowed_by = None # Reference to the person who borrowed the item + + # Abstract method for checking out an item + @abstractmethod + def checkout(self, person): + pass + + # Abstract method for returning an item + @abstractmethod + def return_item(self, person): + pass + + +# Sub class of Library Item representing a physical book +# Static Class +class Book(LibraryItem): + def checkout(self, person): + """ + Allow a person to check out a physical book if it's available. + + Args: + person (Person): The person who wants to check out the book. + """ + if not self.borrowed_by: + self.borrowed_by = person + person.issued_books.append(self) + print(f"Checking out the book: {self.title} by {self.author}") + due_date = datetime.now() + timedelta(days=14) # Due date is set to 14 days from now + print("Due Date: {}".format(due_date.strftime('%Y-%m-%d'))) + else: + print("This book is already borrowed by {}.".format(self.borrowed_by.name)) + + def return_item(self, person): + """ + Allow a person to return a borrowed physical book. + + Args: + person (Person): The person who wants to return the book. + """ + if self.borrowed_by == person: + self.borrowed_by = None + person.issued_books.remove(self) + print("Returning the book: {} by {}".format(self.title, self.author)) + else: + print("You did not borrow this book or it was borrowed by someone else.") + + def read(self): + """ + Simulate reading a physical book. + """ + print("Reading the book: {} by {}".format(self.title, self.author)) + + +# SubClass of LibraryItem representing an eBook +class Ebook(LibraryItem): + def __init__(self, book_ID, title, author, file_size, format): + """ + Initialize an eBook with additional attributes. + + Args: + book_ID (int): The unique ID of the eBook. + title (str): The title of the eBook. + author (str): The author of the eBook. + file_size (int): The size of the eBook file in megabytes. + format (str): The format of the eBook (e.g., PDF, EPUB). + """ + super().__init__(book_ID, title, author) + self.file_size = file_size + self.format = format + + def checkout(self, person): + """ + Allow a person to check out an eBook if it's available. + + Args: + person (Person): The person who wants to check out the eBook. + """ + if not self.borrowed_by: + self.borrowed_by = person + person.issued_books.append(self) + print(f"Checking out the eBook: {self.title} by {self.author}") + print(f"File Size: {self.file_size} MB") + print(f"Format: {self.format}") + due_date = datetime.now() + timedelta(days=7) # Due date is set to 7 days from now for eBooks + print("Due Date: {}".format(due_date.strftime('%Y-%m-%d'))) + else: + print(f"This eBook is already borrowed by {self.borrowed_by.name}.") + + def return_item(self, person): + """ + Allow a person to return a borrowed eBook. + + Args: + person (Person): The person who wants to return the eBook. + """ + if self.borrowed_by == person: + self.borrowed_by = None + person.issued_books.remove(self) + print("Returning the eBook: {} by {}".format(self.title, self.author)) + else: + print("You did not borrow this eBook or it was borrowed by someone else.") + + def read(self): + """ + Simulate reading an eBook. + """ + print("Reading the eBook: {} by {}".format(self.title, self.author)) + print("File Size: {} MB".format(self.file_size)) + print("Format: {}".format(self.format)) + + +class Library: + def __init__(self): + self.books = [] # List of physical books in the library + self.ebooks = [] # List of eBooks in the library + self.members = [] # List of library members (students and staff) + + def add_book(self, book): + """ + Add a physical book to the library. + + Args: + book (Book): The physical book to be added. + """ + self.books.append(book) + + def add_ebook(self, ebook): + """ + Add an eBook to the library. + + Args: + ebook (Ebook): The eBook to be added. + """ + self.ebooks.append(ebook) + + def add_member(self, member): + """ + Add a library member (student or staff) to the library. + + Args: + member (Person): The library member to be added. + """ + self.members.append(member) + + def search_book_by_title(self, title): + """ + Search for books in the library by title by List Comprehension. + + Args: + title (str): The title of the book to search for. + + Returns: + list: A list of matching books. + """ + matching_books = [book for book in self.books if title.lower() in book.title.lower()] + return matching_books + + def search_book_by_author(self, author): + """ + Search for books in the library by author by List Comprehension. + + Args: + author (str): The author of the book to search for. + + Returns: + list: A list of matching books. + """ + matching_books = [book for book in self.books if author.lower() in book.author.lower()] + return matching_books + + def search_book_by_id(self, book_id): + """ + Search for a book in the library by its unique ID by List Comprehension. + + Args: + book_id (int): The unique ID of the book to search for. + + Returns: + list: A list containing the matching book or an empty list if not found. + """ + matching_books = [book for book in self.books if book.book_ID == book_id] + return matching_books + + def list_all_books(self): + """ + List all physical books in the library. + """ + for book in self.books: + print("Book ID: {}\nTitle: {}\nAuthor: {}\n".format(book.book_ID, book.title, book.author)) + + def list_all_issued_books(self): + """ + List all books issued by library members. + """ + for member in self.members: + print(f"{member.name}'s Issued Books:") + for book in member.issued_books: + print("Title: {}\nAuthor: {}\n".format(book.title, book.author)) + + def list_all_members(self): + """ + List all library members along with their details. + """ + print("Library Members:") + for member in self.members: + member.get_details() + print("\n") + + def download_ebook(self, ebook): + print(f"Downloading {ebook.title}.") + +# Usage example: +# Created instances of Person, Student, Staff, Book, Ebook, and Library classes and perform operations. +p1 = Person("Hassan", 21, "029") +p2 = Person("Hamza", 20, "214") + +# Created Book objects +b1 = Book(1, "The Kite Runner", "Khaled Hosseini") +b2 = Book(2, "The Apocalypse", "Unknown Author") + +# Created Ebook objects +e1 = Ebook(3, "Python Programming", "John Smith", 5, "PDF") +e2 = Ebook(4, "Data Science Essentials", "Alice Johnson", 3, "EPUB") + +# Created Student object and perform operations +s1 = Student("Hassan", 21, "029", "CT-21121") +s1.borrow_book(b1) +s1.borrow_book(b2) + +# Created Staff object and perform operations +staff1 = Staff("Alice", 30, "123", "STF-001", "Librarian") +staff1.borrow_book(e1) +staff1.borrow_book(e2) + +# Created Library object and add books, ebooks, and members +library = Library() +library.add_book(b1) +library.add_book(b2) +library.add_ebook(e1) +library.add_ebook(e2) +library.add_member(s1) +library.add_member(staff1) + +library.search_book_by_title("The") +library.list_all_issued_books() +library.list_all_members() \ No newline at end of file From a50fd1954e7d816dd0e0b0c80768b5e11b29e035 Mon Sep 17 00:00:00 2001 From: SyedHassanUlHaq <21b-029-cs@students.uit.edu> Date: Mon, 22 Jan 2024 17:49:55 +0500 Subject: [PATCH 2/4] Added OS.py, A python based Lightweight Operating System gui --- OS.py | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 OS.py diff --git a/OS.py b/OS.py new file mode 100644 index 00000000..0c53d3e5 --- /dev/null +++ b/OS.py @@ -0,0 +1,235 @@ +import sys +from PyQt5.QtWidgets import QApplication, QFileDialog, QInputDialog, QMainWindow, QMessageBox, QPlainTextEdit, QTreeView, QFileSystemModel, QVBoxLayout, QMenu, QAction, QWidget +from PyQt5.QtCore import QThread, pyqtSignal +import os +import subprocess +import time + +# CustomThread class inherits from QThread to create a separate thread for executing tasks +class CustomThread(QThread): + progress = pyqtSignal(str) + finished = pyqtSignal(str) + + def __init__(self, thread_name, shared_data, text_output): + super().__init__() + self.thread_name = thread_name + self.shared_data = shared_data + self.text_output = text_output + + def run(self): + for i in range(5): + time.sleep(1) + # Emit progress updates during the thread's execution + progress_message = f"{self.thread_name}: Working... {i + 1}/5" + self.progress.emit(progress_message) + + # Emit finished signal when the thread completes its task + result_message = f"{self.thread_name}: Task completed!" + self.finished.emit(result_message) + self.text_output.appendPlainText(result_message) + +# CustomProcess class inherits from QThread to manage external processes +class CustomProcess(QThread): + finished = pyqtSignal(str) + + def __init__(self, process_name, shared_data, text_output): + super().__init__() + self.process_name = process_name + self.shared_data = shared_data + self.text_output = text_output + + def run(self): + # Simulating an external process + result_message = subprocess.check_output(['echo', 'Hello from ' + self.process_name]).decode('utf-8') + # Emit finished signal when the process completes + self.finished.emit(result_message) + self.text_output.appendPlainText(result_message) + +# ProcessManager class inherits from QThread to manage process creation and termination +class ProcessManager(QThread): + process_created = pyqtSignal(str) + process_ended = pyqtSignal(str) + + def __init__(self, text_output): + super().__init__() + self.text_output = text_output + self.process_id = 1 + self.processes = {} + + def run(self): + while True: + time.sleep(1) + + def create_process(self, process_name): + custom_process = CustomProcess(process_name, {}, self.text_output) + custom_process.finished.connect(self.process_ended) + custom_process.start() + self.processes[self.process_id] = custom_process + self.process_created.emit(f"Process {self.process_id} created.") + self.process_id += 1 + + def end_process(self, process_id): + process = self.processes.get(process_id) + if process: + process.terminate() + result_message = f"Process {process_id} terminated." + self.process_ended.emit(result_message) + self.processes.pop(process_id) + else: + result_message = f"Process {process_id} not found." + self.process_ended.emit(result_message) + +# CustomOS class provides the main application window +class CustomOS(QMainWindow): + def __init__(self): + super().__init__() + self.init_ui() + + def init_ui(self): + self.setWindowTitle('Custom OS') + + # Setting up the file system model and view + file_model = QFileSystemModel() + file_model.setRootPath(os.path.expanduser('~')) + file_tree = QTreeView() + file_tree.setModel(file_model) + file_tree.setRootIndex(file_model.index(os.path.expanduser('~'))) + + # Creating menus and actions for the main window + process_menu = QMenu('Processes', self) + create_process_action = QAction('Create Process', self) + create_process_action.triggered.connect(self.create_process_dialog) + process_menu.addAction(create_process_action) + + kill_process_action = QAction('Kill Process', self) + kill_process_action.triggered.connect(self.kill_process_dialog) + process_menu.addAction(kill_process_action) + + thread_menu = QMenu('Threads', self) + create_thread_action = QAction('Create Thread', self) + create_thread_action.triggered.connect(self.create_thread) + thread_menu.addAction(create_thread_action) + + # Adding menus to the menu bar + main_menu = self.menuBar() + file_menu = main_menu.addMenu('File') + file_menu.addAction('Create Folder', self.create_folder) + file_menu.addAction('Create File', self.create_file) + file_menu.addAction('Change Rights', self.change_rights) + file_menu.addAction('Search Files', self.search_files) + file_menu.addAction('Open Application', self.open_application) + + main_menu.addMenu(process_menu) + main_menu.addMenu(thread_menu) + + layout = QVBoxLayout() + layout.addWidget(file_tree) + + self.text_output = QPlainTextEdit(self) + layout.addWidget(self.text_output) + + central_widget = QWidget() + central_widget.setLayout(layout) + self.setCentralWidget(central_widget) + + # Shared data between processes and threads + self.shared_data = {"Process1": "", "Process2": "", "Thread1": "", "Thread2": ""} + + # Store custom threads and processes + self.custom_threads = [] + self.custom_processes = [] + + # Create and start the process manager + self.process_manager = ProcessManager(self.text_output) + self.process_manager.process_created.connect(self.process_created) + self.process_manager.process_ended.connect(self.process_ended) + self.process_manager.start() + + def create_folder(self): + folder_name, ok_pressed = QInputDialog.getText(self, 'Create Folder', 'Enter folder name:') + if ok_pressed and folder_name: + try: + os.makedirs(folder_name) + result_message = f"Folder '{folder_name}' created successfully." + self.text_output.appendPlainText(result_message) + except Exception as e: + error_message = f"Error creating folder: {e}" + self.text_output.appendPlainText(error_message) + + def create_file(self): + file_name, ok_pressed = QInputDialog.getText(self, 'Create File', 'Enter file name:') + if ok_pressed and file_name: + try: + with open(file_name, 'w'): + pass + result_message = f"File '{file_name}' created successfully." + self.text_output.appendPlainText(result_message) + except Exception as e: + error_message = f"Error creating file: {e}" + self.text_output.appendPlainText(error_message) + + def change_rights(self): + file_name, ok_pressed = QInputDialog.getText(self, 'Change Rights', 'Enter file name:') + if ok_pressed and file_name: + try: + permissions, ok_pressed = QInputDialog.getText(self, 'Change Rights', 'Enter new permissions (e.g., 755):') + if ok_pressed and permissions: + os.chmod(file_name, int(permissions, 8)) + result_message = f"Rights for '{file_name}' changed successfully." + self.text_output.appendPlainText(result_message) + except Exception as e: + error_message = f"Error changing file rights: {e}" + self.text_output.appendPlainText(error_message) + + def search_files(self): + file_path, _ = QFileDialog.getOpenFileName(self, 'Search Files', os.path.expanduser('~')) + if file_path: + result_message = f"File selected: {file_path}" + self.text_output.appendPlainText(result_message) + + def open_application(self): + app_name, ok_pressed = QInputDialog.getText(self, 'Open Application', 'Enter application name:') + if ok_pressed and app_name: + subprocess.run([app_name]) + + def create_process_dialog(self): + process_name, ok = QInputDialog.getText(self, 'Create Process', 'Enter process name:') + if ok: + self.process_manager.create_process(process_name) + + def kill_process_dialog(self): + process_id, ok_pressed = QInputDialog.getInt(self, 'Kill Process', 'Enter process ID:') + if ok_pressed: + self.process_manager.end_process(process_id) + + def create_thread(self): + thread_name, ok = QInputDialog.getText(self, 'Create Thread', 'Enter thread name:') + if ok: + custom_thread = CustomThread(thread_name, self.shared_data, self.text_output) + custom_thread.finished.connect(self.thread_finished) + custom_thread.progress.connect(self.update_progress) + custom_thread.start() + self.custom_threads.append(custom_thread) + + def process_created(self, result): + self.text_output.appendPlainText(result) + + def process_ended(self, result): + self.text_output.appendPlainText(result) + + def thread_finished(self, result): + # Update shared data when a thread finishes + sender_thread_name = self.sender().thread_name + self.shared_data[sender_thread_name] = result + result_message = f'Thread {sender_thread_name} finished with result: {result}' + self.text_output.appendPlainText(result_message) + + def update_progress(self, progress_message): + # Update progress messages from threads + self.text_output.appendPlainText(progress_message) + +if __name__ == '__main__': + app = QApplication(sys.argv) + custom_os = CustomOS() + custom_os.show() + sys.exit(app.exec_()) From 718b24e5ee689d2400a5d80c1bc85291bdea08c5 Mon Sep 17 00:00:00 2001 From: SyedHassanUlHaq <21b-029-cs@students.uit.edu> Date: Mon, 22 Jan 2024 17:53:28 +0500 Subject: [PATCH 3/4] Revert "Added Library Management System in Python" This reverts commit b2c2943830bd98b7b825d289657f1c0a51bae1b7. --- Library Management System/main.py | 391 ------------------------------ 1 file changed, 391 deletions(-) delete mode 100644 Library Management System/main.py diff --git a/Library Management System/main.py b/Library Management System/main.py deleted file mode 100644 index 638fce03..00000000 --- a/Library Management System/main.py +++ /dev/null @@ -1,391 +0,0 @@ -from abc import ABC, abstractmethod -from datetime import datetime, timedelta - -# Super Class representing a person -class Person: - def __init__(self, name, age, ID): - """ - Initialize a person with their name, age, and ID. - - Args: - name (str): The name of the person. - age (int): The age of the person. - ID (str): The unique ID of the person. - """ - self.name = name - self.age = age - self.ID = ID - - def get_details(self): - """ - Get details of the person, including ID, name, and age. - """ - print("ID: {}\nName: {}\nAge: {}".format(self.ID, self.name, self.age)) - - -# Sub Class of Person representing a student -class Student(Person): - def __init__(self, name, age, ID, student_ID): - """ - Initialize a student with additional attributes. - - Args: - name (str): The name of the student. - age (int): The age of the student. - ID (str): The unique ID of the student. - student_ID (str): The student's unique identification code. - """ - super().__init__(name, age, ID) - self.student_ID = student_ID - self.issued_books = [] # List of books issued to the student - self.borrowed_books = [] # List of books currently borrowed by the student - - def borrow_book(self, book): - """ - Allow the student to borrow a book if not exceeding the borrowing limit. - - Args: - book (LibraryItem): The book to be borrowed. - """ - if len(self.borrowed_books) < 3: # Student can borrow up to 3 books - self.borrowed_books.append(book) - book.checkout(self) - else: - print("You have reached the borrowing limit.") - - def return_book(self, book): - """ - Allow the student to return a borrowed book. - - Args: - book (LibraryItem): The book to be returned. - """ - if book in self.borrowed_books: - self.borrowed_books.remove(book) - book.return_item(self) - else: - print("You did not borrow this book.") - - def get_details(self): - """ - Get details of the student, including ID, name, age, student ID, and issued books. - """ - super().get_details() - print("Student ID: {}".format(self.student_ID)) - print("Issued Books: {}".format(", ".join(book.title for book in self.issued_books))) - - -# SubClass of Person representing a staff member -class Staff(Person): - def __init__(self, name, age, ID, staff_id, designation): - """ - Initialize a staff member with additional attributes. - - Args: - name (str): The name of the staff member. - age (int): The age of the staff member. - ID (str): The unique ID of the staff member. - staff_id (str): The staff member's unique identification code. - designation (str): The job designation of the staff member. - """ - super().__init__(name, age, ID) - self.staff_id = staff_id - self.designation = designation - self.issued_books = [] # List of books issued to the staff member - self.borrowed_books = [] # List of books currently borrowed by the staff member - - def borrow_book(self, book): - """ - Allow the staff member to borrow a book if not exceeding the borrowing limit. - - Args: - book (LibraryItem): The book to be borrowed. - """ - if len(self.borrowed_books) < 5: # Staff can borrow up to 5 books - self.borrowed_books.append(book) - book.checkout(self) - else: - print("You have reached the borrowing limit.") - - def return_book(self, book): - """ - Allow the staff member to return a borrowed book. - - Args: - book (LibraryItem): The book to be returned. - """ - if book in self.borrowed_books: - self.borrowed_books.remove(book) - book.return_item(self) - else: - print("You did not borrow this book.") - - def get_details(self): - """ - Get details of the staff member, including ID, name, age, staff ID, and designation. - """ - super().get_details() - print("Staff ID: {}".format(self.staff_id)) - print("Designation: {}".format(self.designation)) - - -# Abstract Class representing a library item -class LibraryItem(ABC): - def __init__(self, book_ID, title, author): - """ - Initialize a library item with its unique ID, title, and author. - - Args: - book_ID (int): The unique ID of the library item. - title (str): The title of the library item. - author (str): The author of the library item. - """ - self.book_ID = book_ID - self.title = title - self.author = author - self.borrowed_by = None # Reference to the person who borrowed the item - - # Abstract method for checking out an item - @abstractmethod - def checkout(self, person): - pass - - # Abstract method for returning an item - @abstractmethod - def return_item(self, person): - pass - - -# Sub class of Library Item representing a physical book -# Static Class -class Book(LibraryItem): - def checkout(self, person): - """ - Allow a person to check out a physical book if it's available. - - Args: - person (Person): The person who wants to check out the book. - """ - if not self.borrowed_by: - self.borrowed_by = person - person.issued_books.append(self) - print(f"Checking out the book: {self.title} by {self.author}") - due_date = datetime.now() + timedelta(days=14) # Due date is set to 14 days from now - print("Due Date: {}".format(due_date.strftime('%Y-%m-%d'))) - else: - print("This book is already borrowed by {}.".format(self.borrowed_by.name)) - - def return_item(self, person): - """ - Allow a person to return a borrowed physical book. - - Args: - person (Person): The person who wants to return the book. - """ - if self.borrowed_by == person: - self.borrowed_by = None - person.issued_books.remove(self) - print("Returning the book: {} by {}".format(self.title, self.author)) - else: - print("You did not borrow this book or it was borrowed by someone else.") - - def read(self): - """ - Simulate reading a physical book. - """ - print("Reading the book: {} by {}".format(self.title, self.author)) - - -# SubClass of LibraryItem representing an eBook -class Ebook(LibraryItem): - def __init__(self, book_ID, title, author, file_size, format): - """ - Initialize an eBook with additional attributes. - - Args: - book_ID (int): The unique ID of the eBook. - title (str): The title of the eBook. - author (str): The author of the eBook. - file_size (int): The size of the eBook file in megabytes. - format (str): The format of the eBook (e.g., PDF, EPUB). - """ - super().__init__(book_ID, title, author) - self.file_size = file_size - self.format = format - - def checkout(self, person): - """ - Allow a person to check out an eBook if it's available. - - Args: - person (Person): The person who wants to check out the eBook. - """ - if not self.borrowed_by: - self.borrowed_by = person - person.issued_books.append(self) - print(f"Checking out the eBook: {self.title} by {self.author}") - print(f"File Size: {self.file_size} MB") - print(f"Format: {self.format}") - due_date = datetime.now() + timedelta(days=7) # Due date is set to 7 days from now for eBooks - print("Due Date: {}".format(due_date.strftime('%Y-%m-%d'))) - else: - print(f"This eBook is already borrowed by {self.borrowed_by.name}.") - - def return_item(self, person): - """ - Allow a person to return a borrowed eBook. - - Args: - person (Person): The person who wants to return the eBook. - """ - if self.borrowed_by == person: - self.borrowed_by = None - person.issued_books.remove(self) - print("Returning the eBook: {} by {}".format(self.title, self.author)) - else: - print("You did not borrow this eBook or it was borrowed by someone else.") - - def read(self): - """ - Simulate reading an eBook. - """ - print("Reading the eBook: {} by {}".format(self.title, self.author)) - print("File Size: {} MB".format(self.file_size)) - print("Format: {}".format(self.format)) - - -class Library: - def __init__(self): - self.books = [] # List of physical books in the library - self.ebooks = [] # List of eBooks in the library - self.members = [] # List of library members (students and staff) - - def add_book(self, book): - """ - Add a physical book to the library. - - Args: - book (Book): The physical book to be added. - """ - self.books.append(book) - - def add_ebook(self, ebook): - """ - Add an eBook to the library. - - Args: - ebook (Ebook): The eBook to be added. - """ - self.ebooks.append(ebook) - - def add_member(self, member): - """ - Add a library member (student or staff) to the library. - - Args: - member (Person): The library member to be added. - """ - self.members.append(member) - - def search_book_by_title(self, title): - """ - Search for books in the library by title by List Comprehension. - - Args: - title (str): The title of the book to search for. - - Returns: - list: A list of matching books. - """ - matching_books = [book for book in self.books if title.lower() in book.title.lower()] - return matching_books - - def search_book_by_author(self, author): - """ - Search for books in the library by author by List Comprehension. - - Args: - author (str): The author of the book to search for. - - Returns: - list: A list of matching books. - """ - matching_books = [book for book in self.books if author.lower() in book.author.lower()] - return matching_books - - def search_book_by_id(self, book_id): - """ - Search for a book in the library by its unique ID by List Comprehension. - - Args: - book_id (int): The unique ID of the book to search for. - - Returns: - list: A list containing the matching book or an empty list if not found. - """ - matching_books = [book for book in self.books if book.book_ID == book_id] - return matching_books - - def list_all_books(self): - """ - List all physical books in the library. - """ - for book in self.books: - print("Book ID: {}\nTitle: {}\nAuthor: {}\n".format(book.book_ID, book.title, book.author)) - - def list_all_issued_books(self): - """ - List all books issued by library members. - """ - for member in self.members: - print(f"{member.name}'s Issued Books:") - for book in member.issued_books: - print("Title: {}\nAuthor: {}\n".format(book.title, book.author)) - - def list_all_members(self): - """ - List all library members along with their details. - """ - print("Library Members:") - for member in self.members: - member.get_details() - print("\n") - - def download_ebook(self, ebook): - print(f"Downloading {ebook.title}.") - -# Usage example: -# Created instances of Person, Student, Staff, Book, Ebook, and Library classes and perform operations. -p1 = Person("Hassan", 21, "029") -p2 = Person("Hamza", 20, "214") - -# Created Book objects -b1 = Book(1, "The Kite Runner", "Khaled Hosseini") -b2 = Book(2, "The Apocalypse", "Unknown Author") - -# Created Ebook objects -e1 = Ebook(3, "Python Programming", "John Smith", 5, "PDF") -e2 = Ebook(4, "Data Science Essentials", "Alice Johnson", 3, "EPUB") - -# Created Student object and perform operations -s1 = Student("Hassan", 21, "029", "CT-21121") -s1.borrow_book(b1) -s1.borrow_book(b2) - -# Created Staff object and perform operations -staff1 = Staff("Alice", 30, "123", "STF-001", "Librarian") -staff1.borrow_book(e1) -staff1.borrow_book(e2) - -# Created Library object and add books, ebooks, and members -library = Library() -library.add_book(b1) -library.add_book(b2) -library.add_ebook(e1) -library.add_ebook(e2) -library.add_member(s1) -library.add_member(staff1) - -library.search_book_by_title("The") -library.list_all_issued_books() -library.list_all_members() \ No newline at end of file From 8fffdaacd00eda1fce6c1c0d2f3bf28cf21fcc57 Mon Sep 17 00:00:00 2001 From: SyedHassanUlHaq <21b-029-cs@students.uit.edu> Date: Mon, 22 Jan 2024 17:54:22 +0500 Subject: [PATCH 4/4] reverted commit --- OS.py | 235 ---------------------------------------------------------- 1 file changed, 235 deletions(-) delete mode 100644 OS.py diff --git a/OS.py b/OS.py deleted file mode 100644 index 0c53d3e5..00000000 --- a/OS.py +++ /dev/null @@ -1,235 +0,0 @@ -import sys -from PyQt5.QtWidgets import QApplication, QFileDialog, QInputDialog, QMainWindow, QMessageBox, QPlainTextEdit, QTreeView, QFileSystemModel, QVBoxLayout, QMenu, QAction, QWidget -from PyQt5.QtCore import QThread, pyqtSignal -import os -import subprocess -import time - -# CustomThread class inherits from QThread to create a separate thread for executing tasks -class CustomThread(QThread): - progress = pyqtSignal(str) - finished = pyqtSignal(str) - - def __init__(self, thread_name, shared_data, text_output): - super().__init__() - self.thread_name = thread_name - self.shared_data = shared_data - self.text_output = text_output - - def run(self): - for i in range(5): - time.sleep(1) - # Emit progress updates during the thread's execution - progress_message = f"{self.thread_name}: Working... {i + 1}/5" - self.progress.emit(progress_message) - - # Emit finished signal when the thread completes its task - result_message = f"{self.thread_name}: Task completed!" - self.finished.emit(result_message) - self.text_output.appendPlainText(result_message) - -# CustomProcess class inherits from QThread to manage external processes -class CustomProcess(QThread): - finished = pyqtSignal(str) - - def __init__(self, process_name, shared_data, text_output): - super().__init__() - self.process_name = process_name - self.shared_data = shared_data - self.text_output = text_output - - def run(self): - # Simulating an external process - result_message = subprocess.check_output(['echo', 'Hello from ' + self.process_name]).decode('utf-8') - # Emit finished signal when the process completes - self.finished.emit(result_message) - self.text_output.appendPlainText(result_message) - -# ProcessManager class inherits from QThread to manage process creation and termination -class ProcessManager(QThread): - process_created = pyqtSignal(str) - process_ended = pyqtSignal(str) - - def __init__(self, text_output): - super().__init__() - self.text_output = text_output - self.process_id = 1 - self.processes = {} - - def run(self): - while True: - time.sleep(1) - - def create_process(self, process_name): - custom_process = CustomProcess(process_name, {}, self.text_output) - custom_process.finished.connect(self.process_ended) - custom_process.start() - self.processes[self.process_id] = custom_process - self.process_created.emit(f"Process {self.process_id} created.") - self.process_id += 1 - - def end_process(self, process_id): - process = self.processes.get(process_id) - if process: - process.terminate() - result_message = f"Process {process_id} terminated." - self.process_ended.emit(result_message) - self.processes.pop(process_id) - else: - result_message = f"Process {process_id} not found." - self.process_ended.emit(result_message) - -# CustomOS class provides the main application window -class CustomOS(QMainWindow): - def __init__(self): - super().__init__() - self.init_ui() - - def init_ui(self): - self.setWindowTitle('Custom OS') - - # Setting up the file system model and view - file_model = QFileSystemModel() - file_model.setRootPath(os.path.expanduser('~')) - file_tree = QTreeView() - file_tree.setModel(file_model) - file_tree.setRootIndex(file_model.index(os.path.expanduser('~'))) - - # Creating menus and actions for the main window - process_menu = QMenu('Processes', self) - create_process_action = QAction('Create Process', self) - create_process_action.triggered.connect(self.create_process_dialog) - process_menu.addAction(create_process_action) - - kill_process_action = QAction('Kill Process', self) - kill_process_action.triggered.connect(self.kill_process_dialog) - process_menu.addAction(kill_process_action) - - thread_menu = QMenu('Threads', self) - create_thread_action = QAction('Create Thread', self) - create_thread_action.triggered.connect(self.create_thread) - thread_menu.addAction(create_thread_action) - - # Adding menus to the menu bar - main_menu = self.menuBar() - file_menu = main_menu.addMenu('File') - file_menu.addAction('Create Folder', self.create_folder) - file_menu.addAction('Create File', self.create_file) - file_menu.addAction('Change Rights', self.change_rights) - file_menu.addAction('Search Files', self.search_files) - file_menu.addAction('Open Application', self.open_application) - - main_menu.addMenu(process_menu) - main_menu.addMenu(thread_menu) - - layout = QVBoxLayout() - layout.addWidget(file_tree) - - self.text_output = QPlainTextEdit(self) - layout.addWidget(self.text_output) - - central_widget = QWidget() - central_widget.setLayout(layout) - self.setCentralWidget(central_widget) - - # Shared data between processes and threads - self.shared_data = {"Process1": "", "Process2": "", "Thread1": "", "Thread2": ""} - - # Store custom threads and processes - self.custom_threads = [] - self.custom_processes = [] - - # Create and start the process manager - self.process_manager = ProcessManager(self.text_output) - self.process_manager.process_created.connect(self.process_created) - self.process_manager.process_ended.connect(self.process_ended) - self.process_manager.start() - - def create_folder(self): - folder_name, ok_pressed = QInputDialog.getText(self, 'Create Folder', 'Enter folder name:') - if ok_pressed and folder_name: - try: - os.makedirs(folder_name) - result_message = f"Folder '{folder_name}' created successfully." - self.text_output.appendPlainText(result_message) - except Exception as e: - error_message = f"Error creating folder: {e}" - self.text_output.appendPlainText(error_message) - - def create_file(self): - file_name, ok_pressed = QInputDialog.getText(self, 'Create File', 'Enter file name:') - if ok_pressed and file_name: - try: - with open(file_name, 'w'): - pass - result_message = f"File '{file_name}' created successfully." - self.text_output.appendPlainText(result_message) - except Exception as e: - error_message = f"Error creating file: {e}" - self.text_output.appendPlainText(error_message) - - def change_rights(self): - file_name, ok_pressed = QInputDialog.getText(self, 'Change Rights', 'Enter file name:') - if ok_pressed and file_name: - try: - permissions, ok_pressed = QInputDialog.getText(self, 'Change Rights', 'Enter new permissions (e.g., 755):') - if ok_pressed and permissions: - os.chmod(file_name, int(permissions, 8)) - result_message = f"Rights for '{file_name}' changed successfully." - self.text_output.appendPlainText(result_message) - except Exception as e: - error_message = f"Error changing file rights: {e}" - self.text_output.appendPlainText(error_message) - - def search_files(self): - file_path, _ = QFileDialog.getOpenFileName(self, 'Search Files', os.path.expanduser('~')) - if file_path: - result_message = f"File selected: {file_path}" - self.text_output.appendPlainText(result_message) - - def open_application(self): - app_name, ok_pressed = QInputDialog.getText(self, 'Open Application', 'Enter application name:') - if ok_pressed and app_name: - subprocess.run([app_name]) - - def create_process_dialog(self): - process_name, ok = QInputDialog.getText(self, 'Create Process', 'Enter process name:') - if ok: - self.process_manager.create_process(process_name) - - def kill_process_dialog(self): - process_id, ok_pressed = QInputDialog.getInt(self, 'Kill Process', 'Enter process ID:') - if ok_pressed: - self.process_manager.end_process(process_id) - - def create_thread(self): - thread_name, ok = QInputDialog.getText(self, 'Create Thread', 'Enter thread name:') - if ok: - custom_thread = CustomThread(thread_name, self.shared_data, self.text_output) - custom_thread.finished.connect(self.thread_finished) - custom_thread.progress.connect(self.update_progress) - custom_thread.start() - self.custom_threads.append(custom_thread) - - def process_created(self, result): - self.text_output.appendPlainText(result) - - def process_ended(self, result): - self.text_output.appendPlainText(result) - - def thread_finished(self, result): - # Update shared data when a thread finishes - sender_thread_name = self.sender().thread_name - self.shared_data[sender_thread_name] = result - result_message = f'Thread {sender_thread_name} finished with result: {result}' - self.text_output.appendPlainText(result_message) - - def update_progress(self, progress_message): - # Update progress messages from threads - self.text_output.appendPlainText(progress_message) - -if __name__ == '__main__': - app = QApplication(sys.argv) - custom_os = CustomOS() - custom_os.show() - sys.exit(app.exec_())