-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(books): improve find_by_author with casefold and partial matching #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,8 @@ def save_books(self): | |||||||||
| json.dump([asdict(b) for b in self.books], f, indent=2) | ||||||||||
|
|
||||||||||
| def add_book(self, title: str, author: str, year: int) -> Book: | ||||||||||
| if year < 1 or year > 2100: | ||||||||||
| raise ValueError(f"Year must be between 1 and 2100, got {year}.") | ||||||||||
|
Comment on lines
38
to
+40
|
||||||||||
| book = Book(title=title, author=author, year=year) | ||||||||||
| self.books.append(book) | ||||||||||
| self.save_books() | ||||||||||
|
|
@@ -68,5 +70,15 @@ def remove_book(self, title: str) -> bool: | |||||||||
| return False | ||||||||||
|
|
||||||||||
| def find_by_author(self, author: str) -> List[Book]: | ||||||||||
| """Find all books by a given author.""" | ||||||||||
| return [b for b in self.books if b.author.lower() == author.lower()] | ||||||||||
| """Find all books where author contains the given string (case-insensitive partial match).""" | ||||||||||
| normalized = author.casefold() | ||||||||||
| return [b for b in self.books if normalized in b.author.casefold()] | ||||||||||
|
|
||||||||||
| def search_books(self, query: str) -> List[Book]: | ||||||||||
| """Find books where query matches (partial, case-insensitive) title or author.""" | ||||||||||
| q = query.lower() | ||||||||||
| return [b for b in self.books if q in b.title.lower() or q in b.author.lower()] | ||||||||||
|
Comment on lines
+79
to
+80
|
||||||||||
| q = query.lower() | |
| return [b for b in self.books if q in b.title.lower() or q in b.author.lower()] | |
| q = query.casefold() | |
| return [b for b in self.books if q in b.title.casefold() or q in b.author.casefold()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The help text says
finddoes an exact match, but BookCollection.find_by_author() now performs a partial substring match. Please update the CLI help text (or change the implementation) so user-facing documentation matches actual behavior.