07. Foundations Project

The project will involve creating a command-line application that manages a personal library of books. This project will allow you to practice Python installation, syntax, data structures, object-oriented programming, file handling, and module management.

Personal Library Management System

Project Overview

The goal of this project is to build a simple command-line application that allows users to:

  • Add books to their personal library
  • View all books in their library
  • Search for a specific book
  • Save the library data to a file and load it from a file

Step 1: Python Installation

For Ubuntu/Parrot OS/Kali Linux

1. Update Package List:

sudo apt update

2. Install Python:

sudo apt install python3 python3-pip

3. Verify Installation:

python3 --version
pip3 --version

For Windows

  1. Download Python:
  2. Run Installer:
    • Check the box that says “Add Python to PATH” and click “Install Now”.
  3. Verify Installation:
    • Open Command Prompt and type:
python --version
pip --version

Step 2: Python Syntax & Basics

Create a Simple Script

1. Create a new directory for your project:

mkdir personal_library
cd personal_library

2. Create a Python file:

touch library.py  # For Linux
type nul > library.py # For Windows

3. Write a simple script in library.py:

print("Welcome to your Personal Library!")

4. Run the script:

python3 library.py  # For Linux
python library.py # For Windows

Step 3: Python Data Structures & Algorithms

Implement a Book Class

1. Define the Book class:

# library.py

class Book:
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year

def __str__(self):
return f"{self.title} by {self.author} ({self.year})"

Step 4: Python Object-Oriented Programming

Create a Library Class

  1. Implement the Library class:
# library.py

class Library:
def __init__(self):
self.books = []

def add_book(self, book):
self.books.append(book)

def view_books(self):
for book in self.books:
print(book)

def find_book(self, title):
for book in self.books:
if book.title.lower() == title.lower():
return book
return None

Step 5: Python File Handling

Implement Saving and Loading Library Data

1. Add file handling methods to your Library class

import json

class Library:
# Existing methods...

def save_to_file(self, filename):
with open(filename, 'w') as f:
json.dump([book.__dict__ for book in self.books], f)

def load_from_file(self, filename):
with open(filename, 'r') as f:
book_data = json.load(f)
self.books = [Book(**data) for data in book_data]

Step 6: Python Modules and Virtual Environments

Set Up a Virtual Environment

1. Create a virtual environment:

python3 -m venv venv  # For Linux
python -m venv venv # For Windows

2. Activate the virtual environment:

source venv/bin/activate  # For Linux
venv\Scripts\activate # For Windows

3. Install necessary modules (if any):

pip install json5

4. Create the main logic for user interaction:

# library.py

def main():
library = Library()

while True:
print("\nOptions:")
print("1. Add Book")
print("2. View Books")
print("3. Find Book")
print("4. Save Library")
print("5. Load Library")
print("6. Exit")

choice = input("Choose an option: ")

if choice == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
year = input("Enter book year: ")
library.add_book(Book(title, author, year))
elif choice == '2':
library.view_books()
elif choice == '3':
title = input("Enter the title of the book to find: ")
book = library.find_book(title)
if book:
print(f"Found: {book}")
else:
print("Book not found.")
elif choice == '4':
filename = input("Enter filename to save library: ")
library.save_to_file(filename)
elif choice == '5':
filename = input("Enter filename to load library: ")
library.load_from_file(filename)
elif choice == '6':
break
else:
print("Invalid option, please try again.")

if __name__ == "__main__":
main()

Step 7: Final Testing

1. Run your application:

python library.py

2. Test all functionalities:

  • Add books.
  • View all books.
  • Search for a specific book.
  • Save to and load from a file.

With this project, you will have created a simple but functional personal library management system that demonstrates your proficiency in various aspects of Python programming. This project not only solidifies your understanding of Python installation, syntax, data structures, object-oriented programming, file handling, and module management, but it also serves as a foundational project that you can expand upon in the future.

Our recommendation:

  • Expand your library management system to include features like editing book details, removing books, or categorizing books by genre.
  • Implement a graphical user interface (GUI) using libraries like Tkinter or PyQt.
  • Explore using databases like SQLite for storing your library data instead of JSON files.