developer_analysis/import.py

123 lines
3.1 KiB
Python
Raw Normal View History

2023-01-11 02:30:40 +00:00
import sqlite3
import csv
import os
from uuid import uuid4
from os import path
2024-10-13 10:51:07 +00:00
import chemical as chem
import recipe as rec
import supply as sup
2023-01-11 02:30:40 +00:00
RECIPES='recipes/'
SUPPLY='supply.csv'
con = sqlite3.connect('developers.sqlite')
2024-10-13 10:51:07 +00:00
cursor = con.cursor()
chems = chem.Chemical(con)
recs = rec.Recipe(con)
sups = sup.Supply(con)
2023-01-11 02:30:40 +00:00
def uuid () :
return str(uuid4())
def createComponent (chemical, recipe_id, chemical_id, g, ml, makes, note) :
id = uuid()
query="INSERT OR IGNORE INTO components (component_id,recipe_id,chemical_id,grams,milliliters,makes,note) VALUES (?,?,?,?,?,?,?);"
2024-10-13 10:51:07 +00:00
cursor.execute(query, (id, recipe_id, chemical_id, g, ml, makes, note))
2023-01-11 02:30:40 +00:00
con.commit()
val=f"{g}g" if g != 'NULL' else f"{ml}ml"
print(f"Added component {chemical} {val}")
def importRecipe (filePath) :
name = path.basename(filePath).replace('.csv', '').replace('_', ' ')
2024-10-13 10:51:07 +00:00
recipe_id = recs.exists(name)
2023-01-11 02:30:40 +00:00
if recipe_id != '' :
print(f"Recipe {name} already exists")
return
2024-10-13 10:51:07 +00:00
recipe_id = recs.create(name)
2023-01-11 02:30:40 +00:00
with open(filePath, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
chemical=row[0]
if chemical == 'chemical' :
continue
g=float(row[1]) if row[1] != '' else 'NULL'
ml=float(row[2]) if row[2] != '' else 'NULL'
makes=int(row[3])
note=row[4]
2024-10-13 10:51:07 +00:00
chemical_id=chems.identify(chemical)
2023-01-11 02:30:40 +00:00
createComponent(chemical, recipe_id, chemical_id, g, ml, makes, note)
print(f"Imported {name}")
def displayCents (val) :
return '${:,.2f}'.format(val / 100.0)
def displayCost (ml) :
query = "SELECT recipe_id,UPPER(name) FROM recipes ORDER BY name ASC;"
2024-10-13 10:51:07 +00:00
cursor.execute(query)
rows = cursor.fetchall()
2023-01-11 02:30:40 +00:00
for row in rows:
recipe = row[1]
print(f"{recipe}: {ml/1000} liters")
2024-11-10 02:41:51 +00:00
query = """SELECT a.name AS name, (c.grams * (s.price/s.grams)) / c.makes AS cost, (c.grams/c.makes) FROM components AS c
2023-01-11 02:30:40 +00:00
INNER JOIN supply AS s
ON c.chemical_id = s.chemical_id
INNER JOIN chemicals AS a
ON c.chemical_id = a.chemical_id
2024-11-10 02:41:51 +00:00
WHERE c.recipe_id = ?
GROUP BY name
ORDER BY cost ASC;
"""
2023-01-11 02:30:40 +00:00
2024-10-13 10:51:07 +00:00
cursor.execute(query, (row[0],))
components = cursor.fetchall()
2023-01-11 02:30:40 +00:00
total = 0
for component in components:
name = component[0]
price = component[1] * ml
grams = component[2] * ml
total += price
print(f"{name} : [{grams}g] {displayCents(price)}")
print("------")
print(f"Total: {displayCents(total)}")
print("------")
print('-------')
print('SUPPLY')
print('-------')
with open(SUPPLY, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
#print(row)
2024-10-13 10:51:07 +00:00
c=row[0]
if c == 'chemical' :
2023-01-11 02:30:40 +00:00
continue
url=row[1]
g=float(row[2]) if row[2] != '' else 'NULL'
ml=float(row[3]) if row[3] != '' else 'NULL'
price=int(row[4])
2024-10-13 10:51:07 +00:00
chemical_id = chems.identify(c)
#problem area
supply_id = sups.identify(chemical_id, url, g, ml, price)
sups.display(supply_id, c)
#exit()
2023-01-11 02:30:40 +00:00
print('-------')
print('RECIPES')
print('-------')
for recipe in os.listdir(RECIPES):
if recipe.endswith('.csv') :
filePath=f'{RECIPES}{recipe}'
importRecipe(filePath)
print('----')
print('COST')
print('----')
2024-11-10 02:41:51 +00:00
displayCost(125) #gallon