Represents a book in the library system.
Book(title: str, author: str, isbn: str, category: BookCategory = BookCategory.OTHER, publication_year: Optional[int] = None)Parameters:
title: The title of the bookauthor: The author of the bookisbn: The ISBN number (validated)category: The book category (default: OTHER)publication_year: The publication year (optional)
Raises:
InvalidISBNError: If the ISBN is invalid
title(str): The book's titleauthor(str): The book's authorisbn(str): The book's ISBNcategory(BookCategory): The book's categorypublication_year(Optional[int]): Publication yearborrower(Optional[Member]): Current borrowerborrowed_date(Optional[datetime]): When the book was borroweddue_date(Optional[datetime]): When the book is due
Check if the book is available for borrowing.
Check if the book is overdue.
Get the number of days the book is overdue.
Mark the book as borrowed by a member.
Mark the book as returned.
Convert the book to a dictionary representation.
Represents a library member.
Member(name: str, email: str, member_id: str, max_books: int = 5)Parameters:
name: The member's nameemail: The member's email address (validated)member_id: Unique member identifiermax_books: Maximum books the member can borrow (default: 5)
Raises:
InvalidEmailError: If the email is invalid
name(str): The member's nameemail(str): The member's emailmember_id(str): Unique identifiermembership_date(datetime): When the member joinedmax_books(int): Maximum borrowing limit
Check if the member can borrow more books.
Borrow a book from the library.
Raises:
BookNotAvailableError: If the book is not availableValueError: If the member has reached their borrowing limit
Return a borrowed book.
Raises:
BookNotBorrowedError: If the book was not borrowed by this member
Get the list of books currently borrowed.
Check if the member has any overdue books.
Get the list of overdue books.
Calculate total late fees for overdue books.
Get a string representation of the member's details.
Convert the member to a dictionary representation.
Represents the library system.
Library(name: str = "Library")Parameters:
name: The name of the library
name(str): The library's namecreated_date(datetime): When the library was created
Add a book to the library.
Raises:
DuplicateBookError: If a book with the same ISBN already exists
Remove a book from the library.
Raises:
BookNotFoundError: If the book is not found
Get a book by ISBN.
Raises:
BookNotFoundError: If the book is not found
Find books by title.
Find books by author.
Find books by category.
Get all books in the library.
Get all available books.
Get all borrowed books.
Get all overdue books.
Add a member to the library.
Raises:
DuplicateMemberError: If a member with the same ID already exists
Remove a member from the library.
Raises:
MemberNotFoundError: If the member is not foundValueError: If the member has borrowed books
Get a member by ID.
Raises:
MemberNotFoundError: If the member is not found
Find members by name.
Get all members in the library.
Get all members with overdue books.
Get library statistics.
Returns a dictionary containing:
total_books: Total number of booksavailable_books: Number of available booksborrowed_books: Number of borrowed booksoverdue_books: Number of overdue bookstotal_members: Total number of membersmembers_with_overdue: Number of members with overdue books
Convert the library to a dictionary representation.
Service for persisting library data to JSON files.
StorageService(storage_path: Optional[Path] = None)Parameters:
storage_path: Path to storage file (default: ./data/library_data.json)
Save the library data to a JSON file.
Raises:
IOError: If there's an error writing to the file
Load library data from a JSON file.
Returns:
- Library instance or None if file doesn't exist
Raises:
IOError: If there's an error reading from the fileValueError: If the data is invalid
Delete the storage file.
Check if the storage file exists.
Configuration settings for the library system.
Settings(
library_name: str = "City Library",
max_books_per_member: int = 5,
loan_period_days: int = 14,
late_fee_per_day: float = 0.50,
storage_path: str = "data/library_data.json"
)Load settings from a JSON configuration file.
Load settings from a file or return default settings.
Save settings to a JSON configuration file.
Base exception class for all library-related exceptions.
Raised when attempting to borrow a book that is not available.
Raised when a requested book is not found in the library.
Raised when a requested member is not found in the library.
Raised when an invalid ISBN is provided.
Raised when an invalid email address is provided.
Raised when attempting to add a book that already exists.
Raised when attempting to add a member that already exists.
Raised when attempting to return a book that was not borrowed by the member.
from library_system import Library, Book, Member, BookCategory
# Create library
library = Library("My Library")
# Add a book
book = Book("1984", "George Orwell", "9780451524935", BookCategory.FICTION, 1949)
library.add_book(book)
# Add a member
member = Member("John Doe", "john@example.com", "M001")
library.add_member(member)
# Borrow a book
member.borrow_book(book)
# Return a book
member.return_book(book)from pathlib import Path
from library_system import Library, Book, Member
from library_system.services import StorageService
from library_system.config import Settings
# Load settings
settings = Settings.load_or_default(Path("config/library_config.json"))
# Create storage service
storage = StorageService(Path(settings.storage_path))
# Load or create library
library = storage.load_library() or Library(settings.library_name)
# ... perform operations ...
# Save library
storage.save_library(library)