Skip to content

Commit a0b3575

Browse files
committed
Remove unnecessary DB calls
1 parent 24a72f6 commit a0b3575

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

weight_predict/weight_predict.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from time import sleep
33
import serial
4-
from typing import List, Tuple
4+
from typing import List, Tuple, Dict
55
from data_classes import Item
66
import math
77
import 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

Comments
 (0)