-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterStrategy.py
More file actions
33 lines (27 loc) · 1.05 KB
/
Copy pathFilterStrategy.py
File metadata and controls
33 lines (27 loc) · 1.05 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
"""
class for filter strategies. Subclasses must implement 'filter' method.
"""
class FilterStrategy:
"""Abstract method for filtering books by a specific criterion."""
def filter(self, books, value):
raise NotImplementedError("This method should be implemented by subclasses.")
#Filters books by title.
class TitleFilter(FilterStrategy):
def filter(self, books, value):
return books[books['title'].str.contains(value, case=False, na=False)]
#Filters books by author.
class AuthorFilter(FilterStrategy):
def filter(self, books, value):
return books[books['author'].str.contains(value, case=False, na=False)]
#Filters books by genre.
class GenreFilter(FilterStrategy):
def filter(self, books, value):
return books[books['genre'].str.contains(value, case=False, na=False)]
#Filters books by year.
class YearFilter(FilterStrategy):
def filter(self, books, value):
try:
year = int(value)
return books[books['year'] == year]
except ValueError:
return books[0:0]