-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.py
49 lines (36 loc) · 1.89 KB
/
products.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pandas as pd
from models import Product, Product_db
from ui import show_message
import streamlit as st
def binary_search(products_from_db: list[Product_db], product: Product) -> bool:
lo, hi = 0, len(products_from_db) - 1
while lo <= hi:
mid = (lo + hi) // 2
if int(product.cb) == products_from_db[mid].cb:
return True
if int(product.cb) > products_from_db[mid].cb:
lo = mid + 1
else:
hi = mid - 1
return False
def return_found_and_missing_cb_products(counted_products: list[Product], products_from_db: list[Product_db]) -> tuple[list[Product], list[Product]]:
found, not_found = [], []
for counted_product in counted_products:
try:
int(counted_product.cb)
except:
show_message(f"Há um código de barras errado que será desconsiderado. \n")
data = {"Código de Barras": counted_product.cb, "Nome do Produto": counted_product.name, "Supermercado": counted_product.supermarket, "Turno": counted_product.shift}
st.table(data)
continue
if binary_search(products_from_db, counted_product):
found.append(counted_product)
else:
not_found.append(counted_product)
if len({product.cb for product in not_found}) > 0:
show_message(f"\nDos {len({product.cb for product in counted_products})} códigos de barras contabilizados, {len({product.cb for product in not_found})} não estão no banco de dados.\n\n")
if "msg_not_found" not in st.session_state:
st.session_state["msg_not_found"] = f"\nDos {len({product.cb for product in counted_products})} códigos de barras contabilizados, {len({product.cb for product in not_found})} não estão no banco de dados.\n\n"
else:
show_message("Todos os cbs contabilizados estão no banco de dados!")
return found, not_found