11import json
22from time import sleep
33import serial
4- from typing import List , Tuple
4+ from typing import List , Tuple , Dict
55from data_classes import Item
66import math
77import database as db
@@ -14,12 +14,16 @@ class Slot:
1414
1515 _previous_weight_value : float
1616 _items : List [Item ]
17+ _items_by_id : Dict [int , Item ]
1718
1819 def __init__ (self , starting_value : float = 0.0 ):
1920 self ._previous_weight_value = starting_value
2021 self ._items = list ()
2122 self ._items = db .get_items ()
2223
24+ for item in self ._items :
25+ self ._items_by_id [item .item_id ] = item
26+
2327
2428 def predict_most_likely_item (self , new_weight : float ) -> List [Item ]:
2529 """
@@ -58,7 +62,7 @@ def predict_most_likely_item(self, new_weight: float) -> List[Item]:
5862
5963 # Iterate through the top probabilities in decreasing order
6064 for rank , ((item_id , quantity ), probability ) in enumerate (top_n_probabilities .items (), start = 1 ):
61- print (f'{ quantity } x { db .get_item (item_id ).name } (p={ probability } )' )
65+ # print(f'{quantity}x {db.get_item(item_id).name} (p={probability})')
6266 if abs (probability ) > THRESHOLD_WEIGHT_PROBABILITY :
6367 # If this probability is less than the threshold, all others will be too so return early
6468 break
@@ -77,7 +81,7 @@ def predict_most_likely_item(self, new_weight: float) -> List[Item]:
7781 # Get the quantity for this item
7882 quantity = item_ids_and_quantities [item_id ]
7983 # Get the existing item object
80- existing_item_obj = db . get_item ( item_id )
84+ existing_item_obj = self . _items_by_id [ item_id ]
8185 # Create a new item object with this quantity, and add it to the list of items to be
8286 # returned
8387 items .append (
0 commit comments