forked from khushe-2811/Nexus-Library-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.py
More file actions
59 lines (53 loc) · 2.03 KB
/
strategy.py
File metadata and controls
59 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ------------ strategy.py ------------
from abc import ABC, abstractmethod
class SearchStrategy(ABC):
@abstractmethod
def search(self, items, query):
pass
class KeywordSearchStrategy(SearchStrategy):
def search(self, items, query):
query = query.strip().lower()
results = []
seen_items = set()
for item in items:
try:
# Check all relevant fields including author
matches = any([
query in item.title.lower(),
query in item.author.lower(),
(hasattr(item, 'genre') and query in item.genre.lower())
])
if matches and item.item_id not in seen_items:
results.append(item)
seen_items.add(item.item_id)
except AttributeError as e:
print(f"Skipping invalid item: {str(e)}")
continue
print(f"Found {len(results)} genre matches")
return results
class AuthorSearchStrategy(SearchStrategy):
"""Search by author name (case-insensitive partial match)"""
def search(self, items, query):
query = query.strip().lower()
results = []
for item in items:
try:
if query in item.author.lower():
results.append(item)
except AttributeError:
continue
print(f"Found {len(results)} genre matches")
return results
class GenreSearchStrategy(SearchStrategy):
"""Search by genre (case-insensitive partial match)"""
def search(self, items, query):
query = query.strip().lower()
results = []
for item in items:
try:
if hasattr(item, 'genre') and query in item.genre.lower():
results.append(item)
except AttributeError:
continue
print(f"Found {len(results)} genre matches")
return results