Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions CardCollection/collectionhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pokemontcgsdk import Card
from typing import List, Tuple, Optional

class CollectionHandler:
"""
CollectionHandler manages loading and saving a collection of Pokemon cards
This class handles interactions with files or other storage mechanism to persis collections

Methods:
handle_collection_load(): Handles the public interface for loading a collection
handle_collection_save(data: List[Tuple]): Handles the public interface for saving a collection
load_collection_file(TBD): Handles internal logic for loading data from a file or source
save_collection_file(data: TBD): Handles the interal logic for saving data to a file or source
"""

def handle_collection_load(self) -> List[Card]:
"""
Public method to load a collection of Pokemon cards.

Returns:
List[Card]: A list of Card objects representing the loaded collection
"""

collection = self.load_collection_file()
return collection

def handle_collection_save(self, data: List[Tuple]) -> None;
"""
Public method to save a collection of Pokemon cards

Args:
data (List[Tuple]): A list of tuples representing card data to save
"""

self.save_collection_file(data)

def load_collection_file(self, source: Optional[str] = None) -> List[Card]:
"""
Private method to load collection data from a file or source

Args:
source (Optional[str]): The source of the collection (e.g., file path)

Returns:
List[Card]: A list of Card objects representing the collection
"""

def save_collection_file(self, data: List[Tuple], destination: Optional[str] = None) -> None:
"""
Private method to save collection data to a file or destination

Args:
data(List[Tuple]): A list of tuples representing card data to save
destination (Optional[str]): The destination for the collection (e.g., file path)
"""
42 changes: 22 additions & 20 deletions CardCollection/discoverhandler.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
import random
from collections import defaultdict

from pokemontcgsdk import Card
from backend import Backend
from cardprocessor import CardProcessor


class DiscoverHandler:
"""
DiscoverHandler Class:

Summary:
The `DiscoverHandler` class provides methods to discover a
single random card based on search criteria.
The class uses `Backend.fetch_data` to retrieve data and
`CardProcessor.process_cards` for data processing.
The `DiscoverHandler` class provides methods to discover a
single random card based on search criteria.
The class uses `Backend.fetch_data` to retrieve data and
`CardProcessor.process_cards` for data processing.

Methods:
handle_discover:
Returns a single card matching randomized criteria from a list of possible parameters.

If no cards are found for the selected criteria, the function retries with new random
criteria until a valid card is found

random_select_set:
Randomly selects a set from the input list of sets.

random_select_type:
Randomly selects a type from the input list of types.
"""

def handle_discover(self, param_list: list[tuple[str, str, str]]) -> list[dict[str, str]]:
print([element for element in param_list if element[0] != 'set'])

randomSelect = DiscoverHandler.random_select(param_list)

query = Backend.construct_query(self, randomSelect)
print("Query: " + query)
# Initialize processed_cards to empty to enter the loop
processed_cards = []

results = Backend.query_api(self, query)
while not processed_cards:
# Perform random selection of parameters
randomSelect = DiscoverHandler.random_select(param_list)

processed_cards = CardProcessor.process_cards(results)
# Check if the selected type is available within the selected set
query = Backend.construct_query(self, randomSelect)

randomChoice = []
randomChoice.append(random.choice(processed_cards))
# Run the query with the current random selection
results = Backend.query_api(self, query)
processed_cards = CardProcessor.process_cards(results)

# Select a random card from the processed results
randomChoice = [random.choice(processed_cards)]
return randomChoice

def random_select(select_list: list[tuple[str, str, str]]) -> list[tuple[str, str, str]]:
Expand Down
13 changes: 8 additions & 5 deletions CardCollection/searchhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SearchHandler:
handle_search(search_parameters): Constructs a query string using the Backend class, construct_query function.
"""

def handle_search(self, search_parameters: list[tuple[str, str, str]]) -> list[dict[str, str]]:
def handle_search(self, search_parameters: list[tuple[str, str, str]], max_results: int = 100) -> list[dict[str, str]]:
"""
A function that takes a list of tuples representing search parameters, constructs a query string using the Backend class, and retrieves a list of Card objects from the API.

Expand All @@ -28,8 +28,12 @@ def handle_search(self, search_parameters: list[tuple[str, str, str]]) -> list[d

query = Backend.construct_query(self, search_parameters)
results = Backend.query_api(self, query)
return self.handle_card_process(results)


# Limit results to max_results (100)
limited_results = results[:max_results]

return self.handle_card_process(limited_results)

def handle_card_process(self, cards: list[Card]) -> list[dict[str, str]]:
"""
A function that takes a list of tuples representing search parameters, constructs a query string using the Backend class, and retrieves a list of Card objects from the API.
Expand All @@ -41,6 +45,5 @@ def handle_card_process(self, cards: list[Card]) -> list[dict[str, str]]:
- key: card attribute defined by API
- value: card data string for that attribute
"""

return CardProcessor.process_cards(cards)

return CardProcessor.process_cards(cards)