creating a multi page option class
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
			
		||||
from collections import defaultdict
 | 
			
		||||
from typing import Tuple, List, Set
 | 
			
		||||
from typing import Tuple, List, Set, Dict, Type
 | 
			
		||||
 | 
			
		||||
from . import page_attributes
 | 
			
		||||
from ..abstract import Page
 | 
			
		||||
@@ -7,13 +7,43 @@ from ..abstract import Page
 | 
			
		||||
from ...objects import Options
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MultiPageOptions:
 | 
			
		||||
    def __init__(
 | 
			
		||||
        self,
 | 
			
		||||
        max_displayed_options: int = 10,
 | 
			
		||||
        option_digits: int = 3
 | 
			
		||||
    ) -> None:
 | 
			
		||||
        self.max_displayed_options = max_displayed_options
 | 
			
		||||
        self.option_digits: int = option_digits
 | 
			
		||||
        
 | 
			
		||||
        self._current_option_dict: Dict[Type[Page], Options] = defaultdict(lambda: Options())
 | 
			
		||||
 | 
			
		||||
    def __getitem__(self, key: Page):
 | 
			
		||||
        return self._current_option_dict[key]
 | 
			
		||||
    
 | 
			
		||||
    def __setitem__(self, key: Page, value: Options):
 | 
			
		||||
        self._current_option_dict[key] = value
 | 
			
		||||
        
 | 
			
		||||
    def __repr__(self) -> str:
 | 
			
		||||
        lines: List[str] = []
 | 
			
		||||
        
 | 
			
		||||
        j = 0
 | 
			
		||||
        for page, options in self._current_option_dict.items():
 | 
			
		||||
            lines.append(f"----------{page.__name__}----------")
 | 
			
		||||
            
 | 
			
		||||
            
 | 
			
		||||
        return "\n".join(lines)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Search:
 | 
			
		||||
    def __init__(
 | 
			
		||||
        self,
 | 
			
		||||
        query: str,
 | 
			
		||||
        pages: Tuple[Page] = page_attributes.ALL_PAGES,
 | 
			
		||||
        exclude_pages: Set[Page] = set(),
 | 
			
		||||
        exclude_shady: bool = False
 | 
			
		||||
        exclude_shady: bool = False,
 | 
			
		||||
        max_displayed_options: int = 10,
 | 
			
		||||
        option_digits: int = 3
 | 
			
		||||
    ) -> None:
 | 
			
		||||
        _page_list: List[Page] = []
 | 
			
		||||
        _audio_page_list: List[Page] = []
 | 
			
		||||
@@ -32,10 +62,30 @@ class Search:
 | 
			
		||||
        self.pages: Tuple[Page] = tuple(_page_list)
 | 
			
		||||
        self.audio_pages: Tuple[Page] = tuple(_audio_page_list)
 | 
			
		||||
        
 | 
			
		||||
        self.current_option_dict = defaultdict(lambda: Options())
 | 
			
		||||
        self.max_displayed_options = max_displayed_options
 | 
			
		||||
        self.option_digits: int = option_digits
 | 
			
		||||
        
 | 
			
		||||
        self._option_history: List[MultiPageOptions] = []
 | 
			
		||||
        
 | 
			
		||||
        self._current_option: MultiPageOptions = self.next_options
 | 
			
		||||
        
 | 
			
		||||
        self.search(query)
 | 
			
		||||
        
 | 
			
		||||
    @property
 | 
			
		||||
    def next_options(self) -> MultiPageOptions:
 | 
			
		||||
        mpo = MultiPageOptions(
 | 
			
		||||
            max_displayed_options=self.max_displayed_options,
 | 
			
		||||
            option_digits=self.option_digits
 | 
			
		||||
        )
 | 
			
		||||
        self._option_history.append(mpo)
 | 
			
		||||
        return mpo
 | 
			
		||||
    
 | 
			
		||||
    @property
 | 
			
		||||
    def previous_options(self) -> MultiPageOptions:
 | 
			
		||||
        self._option_history.pop()
 | 
			
		||||
        return self._option_history[-1]
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
    def search(self, query: str):
 | 
			
		||||
        """
 | 
			
		||||
        # The Query
 | 
			
		||||
@@ -47,8 +97,7 @@ class Search:
 | 
			
		||||
        """
 | 
			
		||||
        
 | 
			
		||||
        for page in self.pages:
 | 
			
		||||
            self.current_option_dict[page] = page.search_by_query(query=query)
 | 
			
		||||
            self._current_option[page] = page.search_by_query(query=query)
 | 
			
		||||
            
 | 
			
		||||
            print("-"*10, page.__name__, "-"*10)
 | 
			
		||||
            print(self.current_option_dict[page])
 | 
			
		||||
        print(self._current_option)
 | 
			
		||||
                
 | 
			
		||||
		Reference in New Issue
	
	Block a user