classes.c33_basket
1import random 2from loguru import logger 3 4 5class KBasket: 6 """ 7 Generic list of options to avoid repetition. 8 9 Provides methods to pick items sequentially or randomly, ensuring that each item is only picked once until the basket is reloaded. 10 """ 11 12 def __init__(self, items: list, reverse: bool = False) -> None: 13 """ 14 Initializes the KBasket with a list of items. 15 16 Args: 17 items: The list of items to manage. 18 reverse: Whether to reverse the order of items on initialization. 19 """ 20 if reverse: 21 items.reverse() 22 23 self._items = items 24 self._reload() 25 26 def __len__(self) -> int: 27 """ 28 Returns the number of remaining items in the basket. 29 30 Returns: 31 int: The number of items left to pick. 32 """ 33 return len(self.items) 34 35 def _check(self) -> None: 36 """ 37 Checks if the basket is depleted and reloads it if necessary. 38 """ 39 if not getattr(self, "items"): 40 self._reload() 41 42 def _reload(self) -> None: 43 """ 44 Reinitializes the basket to its original state. 45 """ 46 if 0: 47 logger.trace("Reloading...") 48 self.items = self._items.copy() 49 50 def _drop(self, item) -> None: 51 """ 52 Removes a specific item from the basket. 53 54 Args: 55 item: The item to remove. 56 """ 57 itemIndex = self.items.index(item) 58 self.items.pop(itemIndex) 59 60 def pickNext(self): 61 """ 62 Picks the next item in order from the basket. 63 64 Returns: 65 The next item in the basket. 66 """ 67 self._check() 68 item = self.items[0] 69 self._drop(item) 70 return item 71 72 def pickRandom(self): 73 """ 74 Picks a random item from the basket. 75 76 Returns: 77 A randomly selected item from the basket. 78 """ 79 self._check() 80 item = random.choice(self.items) 81 self._drop(item) 82 return item
class
KBasket:
6class KBasket: 7 """ 8 Generic list of options to avoid repetition. 9 10 Provides methods to pick items sequentially or randomly, ensuring that each item is only picked once until the basket is reloaded. 11 """ 12 13 def __init__(self, items: list, reverse: bool = False) -> None: 14 """ 15 Initializes the KBasket with a list of items. 16 17 Args: 18 items: The list of items to manage. 19 reverse: Whether to reverse the order of items on initialization. 20 """ 21 if reverse: 22 items.reverse() 23 24 self._items = items 25 self._reload() 26 27 def __len__(self) -> int: 28 """ 29 Returns the number of remaining items in the basket. 30 31 Returns: 32 int: The number of items left to pick. 33 """ 34 return len(self.items) 35 36 def _check(self) -> None: 37 """ 38 Checks if the basket is depleted and reloads it if necessary. 39 """ 40 if not getattr(self, "items"): 41 self._reload() 42 43 def _reload(self) -> None: 44 """ 45 Reinitializes the basket to its original state. 46 """ 47 if 0: 48 logger.trace("Reloading...") 49 self.items = self._items.copy() 50 51 def _drop(self, item) -> None: 52 """ 53 Removes a specific item from the basket. 54 55 Args: 56 item: The item to remove. 57 """ 58 itemIndex = self.items.index(item) 59 self.items.pop(itemIndex) 60 61 def pickNext(self): 62 """ 63 Picks the next item in order from the basket. 64 65 Returns: 66 The next item in the basket. 67 """ 68 self._check() 69 item = self.items[0] 70 self._drop(item) 71 return item 72 73 def pickRandom(self): 74 """ 75 Picks a random item from the basket. 76 77 Returns: 78 A randomly selected item from the basket. 79 """ 80 self._check() 81 item = random.choice(self.items) 82 self._drop(item) 83 return item
Generic list of options to avoid repetition.
Provides methods to pick items sequentially or randomly, ensuring that each item is only picked once until the basket is reloaded.
KBasket(items: list, reverse: bool = False)
13 def __init__(self, items: list, reverse: bool = False) -> None: 14 """ 15 Initializes the KBasket with a list of items. 16 17 Args: 18 items: The list of items to manage. 19 reverse: Whether to reverse the order of items on initialization. 20 """ 21 if reverse: 22 items.reverse() 23 24 self._items = items 25 self._reload()
Initializes the KBasket with a list of items.
Arguments:
- items: The list of items to manage.
- reverse: Whether to reverse the order of items on initialization.
def
pickNext(self):
61 def pickNext(self): 62 """ 63 Picks the next item in order from the basket. 64 65 Returns: 66 The next item in the basket. 67 """ 68 self._check() 69 item = self.items[0] 70 self._drop(item) 71 return item
Picks the next item in order from the basket.
Returns:
The next item in the basket.
def
pickRandom(self):
73 def pickRandom(self): 74 """ 75 Picks a random item from the basket. 76 77 Returns: 78 A randomly selected item from the basket. 79 """ 80 self._check() 81 item = random.choice(self.items) 82 self._drop(item) 83 return item
Picks a random item from the basket.
Returns:
A randomly selected item from the basket.