move data types to classes
This commit is contained in:
parent
18cd0c6f75
commit
231a44c44c
|
@ -0,0 +1,45 @@
|
||||||
|
import sqlite3
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
class Chemical :
|
||||||
|
def __init__ (self, conn) :
|
||||||
|
self.conn = conn
|
||||||
|
self.cursor = conn.cursor()
|
||||||
|
|
||||||
|
def uuid (self) :
|
||||||
|
return str(uuid4())
|
||||||
|
|
||||||
|
def get (self, name) :
|
||||||
|
query = "SELECT chemical_id FROM chemicals WHERE (name = LOWER(?)) LIMIT 1;"
|
||||||
|
res = self.cursor.execute(query, (name,))
|
||||||
|
return self.cursor.fetchone()
|
||||||
|
|
||||||
|
def exists (self, name) :
|
||||||
|
chemical = self.get(name)
|
||||||
|
if chemical is not None:
|
||||||
|
return chemical[0]
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def create (self, name) :
|
||||||
|
id = self.uuid()
|
||||||
|
query = "INSERT OR IGNORE INTO chemicals (chemical_id, name) VALUES (?, LOWER(?));"
|
||||||
|
self.cursor.execute(query, (id, name,))
|
||||||
|
self.conn.commit()
|
||||||
|
return id
|
||||||
|
|
||||||
|
def identify (self, c) :
|
||||||
|
chemical_id = self.exists(c)
|
||||||
|
if chemical_id == '' :
|
||||||
|
chemical_id = self.create(c)
|
||||||
|
return chemical_id
|
||||||
|
|
||||||
|
def table (self) :
|
||||||
|
query = "SELECT * FROM chemicals;"
|
||||||
|
res = self.cursor.execute(query, ())
|
||||||
|
for row in self.cursor.fetchall() :
|
||||||
|
print(row)
|
||||||
|
|
||||||
|
if __name__ == '__main__' :
|
||||||
|
con = sqlite3.connect('developers.sqlite')
|
||||||
|
c = Chemical(con)
|
||||||
|
c.table()
|
130
import.py
130
import.py
|
@ -5,124 +5,40 @@ import csv
|
||||||
import os
|
import os
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from os import path
|
from os import path
|
||||||
|
import chemical as chem
|
||||||
|
import recipe as rec
|
||||||
|
import supply as sup
|
||||||
|
|
||||||
RECIPES='recipes/'
|
RECIPES='recipes/'
|
||||||
SUPPLY='supply.csv'
|
SUPPLY='supply.csv'
|
||||||
|
|
||||||
con = sqlite3.connect('developers.sqlite')
|
con = sqlite3.connect('developers.sqlite')
|
||||||
c = con.cursor()
|
cursor = con.cursor()
|
||||||
|
|
||||||
|
chems = chem.Chemical(con)
|
||||||
|
recs = rec.Recipe(con)
|
||||||
|
sups = sup.Supply(con)
|
||||||
|
|
||||||
def uuid () :
|
def uuid () :
|
||||||
return str(uuid4())
|
return str(uuid4())
|
||||||
|
|
||||||
def getRecipe (name) :
|
|
||||||
query="SELECT recipe_id FROM recipes WHERE (name = LOWER(?)) LIMIT 1;"
|
|
||||||
res = c.execute(query, (name,))
|
|
||||||
return c.fetchone()
|
|
||||||
|
|
||||||
def hasRecipe (name) :
|
|
||||||
recipe = getRecipe(name)
|
|
||||||
if recipe is not None:
|
|
||||||
return recipe[0]
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def createRecipe (name) :
|
|
||||||
id=uuid()
|
|
||||||
query="INSERT OR IGNORE INTO recipes (recipe_id, name) VALUES (?, LOWER(?));"
|
|
||||||
c.execute(query, (id, name,))
|
|
||||||
con.commit()
|
|
||||||
return id
|
|
||||||
|
|
||||||
def getChemical (chemical) :
|
|
||||||
query="SELECT chemical_id FROM chemicals WHERE (name = LOWER(?)) LIMIT 1;"
|
|
||||||
res = c.execute(query, (chemical,))
|
|
||||||
return c.fetchone()
|
|
||||||
|
|
||||||
def hasChemical (chemical) :
|
|
||||||
chemical = getChemical(chemical)
|
|
||||||
if chemical is not None:
|
|
||||||
return chemical[0]
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def createChemical (chemical) :
|
|
||||||
id=uuid()
|
|
||||||
query="INSERT OR IGNORE INTO chemicals (chemical_id, name) VALUES (?, LOWER(?));"
|
|
||||||
c.execute(query, (id, chemical,))
|
|
||||||
con.commit()
|
|
||||||
return id
|
|
||||||
|
|
||||||
def ensureChemical (chemical) :
|
|
||||||
chemical_id = hasChemical(chemical)
|
|
||||||
if chemical_id == '' :
|
|
||||||
chemical_id = createChemical(chemical)
|
|
||||||
return chemical_id
|
|
||||||
|
|
||||||
def getSupply (url) :
|
|
||||||
query="SELECT supply_id FROM supply WHERE (url = ?) LIMIT 1;"
|
|
||||||
res = c.execute(query, (url,))
|
|
||||||
return c.fetchone()
|
|
||||||
|
|
||||||
def hasSupply (url) :
|
|
||||||
supply = getSupply(url)
|
|
||||||
if supply is not None:
|
|
||||||
return supply[0]
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def createSupply (chemical_id, url, g, ml, price) :
|
|
||||||
id=uuid()
|
|
||||||
query="INSERT OR IGNORE INTO supply (supply_id,chemical_id,url,grams,milliliters,price) VALUES (?,?,?,?,?,?);"
|
|
||||||
c.execute(query, (id,chemical_id,url,g,ml,price,))
|
|
||||||
con.commit()
|
|
||||||
return id
|
|
||||||
|
|
||||||
def ensureSupply (chemical_id, url, g, ml, price) :
|
|
||||||
supply_id = hasSupply(url)
|
|
||||||
if supply_id == '' :
|
|
||||||
supply_id = createSupply(chemical_id, url, g, ml, price)
|
|
||||||
return supply_id
|
|
||||||
|
|
||||||
def displaySupply (supply_id, chemical) :
|
|
||||||
query="SELECT round((price/100.0)/grams, 2) FROM supply WHERE (supply_id = ?);"
|
|
||||||
res = c.execute(query, (supply_id,))
|
|
||||||
data = c.fetchone()
|
|
||||||
ratio = data[0]
|
|
||||||
print(f"{chemical}: {ratio} $/g")
|
|
||||||
|
|
||||||
def getRecipe (recipe) :
|
|
||||||
query="SELECT recipe_id FROM recipes WHERE (name = LOWER(?)) LIMIT 1;"
|
|
||||||
res = c.execute(query, (recipe,))
|
|
||||||
return c.fetchone()
|
|
||||||
|
|
||||||
def hasRecipe (recipe) :
|
|
||||||
recipe = getRecipe(recipe)
|
|
||||||
if recipe is not None:
|
|
||||||
return recipe[0]
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def createComponent (chemical, recipe_id, chemical_id, g, ml, makes, note) :
|
def createComponent (chemical, recipe_id, chemical_id, g, ml, makes, note) :
|
||||||
id = uuid()
|
id = uuid()
|
||||||
query="INSERT OR IGNORE INTO components (component_id,recipe_id,chemical_id,grams,milliliters,makes,note) VALUES (?,?,?,?,?,?,?);"
|
query="INSERT OR IGNORE INTO components (component_id,recipe_id,chemical_id,grams,milliliters,makes,note) VALUES (?,?,?,?,?,?,?);"
|
||||||
c.execute(query, (id, recipe_id, chemical_id, g, ml, makes, note))
|
cursor.execute(query, (id, recipe_id, chemical_id, g, ml, makes, note))
|
||||||
con.commit()
|
con.commit()
|
||||||
val=f"{g}g" if g != 'NULL' else f"{ml}ml"
|
val=f"{g}g" if g != 'NULL' else f"{ml}ml"
|
||||||
print(f"Added component {chemical} {val}")
|
print(f"Added component {chemical} {val}")
|
||||||
|
|
||||||
def createRecipe (recipe) :
|
|
||||||
id=uuid()
|
|
||||||
query="INSERT OR IGNORE INTO recipes (recipe_id, name) VALUES (?, ?);"
|
|
||||||
c.execute(query, (id, recipe,))
|
|
||||||
con.commit()
|
|
||||||
return id
|
|
||||||
|
|
||||||
def importRecipe (filePath) :
|
def importRecipe (filePath) :
|
||||||
name = path.basename(filePath).replace('.csv', '').replace('_', ' ')
|
name = path.basename(filePath).replace('.csv', '').replace('_', ' ')
|
||||||
recipe_id = hasRecipe(name)
|
recipe_id = recs.exists(name)
|
||||||
|
|
||||||
if recipe_id != '' :
|
if recipe_id != '' :
|
||||||
print(f"Recipe {name} already exists")
|
print(f"Recipe {name} already exists")
|
||||||
return
|
return
|
||||||
|
|
||||||
recipe_id = createRecipe(name)
|
recipe_id = recs.create(name)
|
||||||
|
|
||||||
with open(filePath, newline='') as csvfile:
|
with open(filePath, newline='') as csvfile:
|
||||||
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
||||||
|
@ -134,7 +50,7 @@ def importRecipe (filePath) :
|
||||||
ml=float(row[2]) if row[2] != '' else 'NULL'
|
ml=float(row[2]) if row[2] != '' else 'NULL'
|
||||||
makes=int(row[3])
|
makes=int(row[3])
|
||||||
note=row[4]
|
note=row[4]
|
||||||
chemical_id=ensureChemical(chemical)
|
chemical_id=chems.identify(chemical)
|
||||||
createComponent(chemical, recipe_id, chemical_id, g, ml, makes, note)
|
createComponent(chemical, recipe_id, chemical_id, g, ml, makes, note)
|
||||||
print(f"Imported {name}")
|
print(f"Imported {name}")
|
||||||
|
|
||||||
|
@ -143,8 +59,8 @@ def displayCents (val) :
|
||||||
|
|
||||||
def displayCost (ml) :
|
def displayCost (ml) :
|
||||||
query = "SELECT recipe_id,UPPER(name) FROM recipes ORDER BY name ASC;"
|
query = "SELECT recipe_id,UPPER(name) FROM recipes ORDER BY name ASC;"
|
||||||
c.execute(query)
|
cursor.execute(query)
|
||||||
rows = c.fetchall()
|
rows = cursor.fetchall()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
recipe = row[1]
|
recipe = row[1]
|
||||||
print(f"{recipe}: {ml/1000} liters")
|
print(f"{recipe}: {ml/1000} liters")
|
||||||
|
@ -155,8 +71,8 @@ def displayCost (ml) :
|
||||||
ON c.chemical_id = a.chemical_id
|
ON c.chemical_id = a.chemical_id
|
||||||
WHERE c.recipe_id = ?;"""
|
WHERE c.recipe_id = ?;"""
|
||||||
|
|
||||||
c.execute(query, (row[0],))
|
cursor.execute(query, (row[0],))
|
||||||
components = c.fetchall()
|
components = cursor.fetchall()
|
||||||
|
|
||||||
total = 0
|
total = 0
|
||||||
for component in components:
|
for component in components:
|
||||||
|
@ -176,17 +92,19 @@ print('-------')
|
||||||
with open(SUPPLY, newline='') as csvfile:
|
with open(SUPPLY, newline='') as csvfile:
|
||||||
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
||||||
for row in reader:
|
for row in reader:
|
||||||
chemical=row[0]
|
print(row)
|
||||||
if chemical == 'chemical' :
|
c=row[0]
|
||||||
|
if c == 'chemical' :
|
||||||
continue
|
continue
|
||||||
url=row[1]
|
url=row[1]
|
||||||
g=float(row[2]) if row[2] != '' else 'NULL'
|
g=float(row[2]) if row[2] != '' else 'NULL'
|
||||||
ml=float(row[3]) if row[3] != '' else 'NULL'
|
ml=float(row[3]) if row[3] != '' else 'NULL'
|
||||||
price=int(row[4])
|
price=int(row[4])
|
||||||
chemical_id = ensureChemical(chemical)
|
chemical_id = chems.identify(c)
|
||||||
supply_id = ensureSupply(chemical_id, url, g, ml, price)
|
#problem area
|
||||||
displaySupply(supply_id, chemical)
|
supply_id = sups.identify(chemical_id, url, g, ml, price)
|
||||||
|
sups.display(supply_id, c)
|
||||||
|
exit()
|
||||||
print('-------')
|
print('-------')
|
||||||
print('RECIPES')
|
print('RECIPES')
|
||||||
print('-------')
|
print('-------')
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
import sqlite3
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
class Recipe :
|
||||||
|
def __init__ (self, conn) :
|
||||||
|
self.conn = conn
|
||||||
|
self.cursor = conn.cursor()
|
||||||
|
|
||||||
|
def uuid (self) :
|
||||||
|
return str(uuid4())
|
||||||
|
|
||||||
|
def get (self, name) :
|
||||||
|
query = "SELECT recipe_id FROM recipes WHERE (name = LOWER(?)) LIMIT 1;"
|
||||||
|
res = self.cursor.execute(query, (name,))
|
||||||
|
return self.cursor.fetchone()
|
||||||
|
|
||||||
|
def exists (self, name) :
|
||||||
|
recipe = self.get(name)
|
||||||
|
if recipe is not None:
|
||||||
|
return recipe[0]
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def create (self, name) :
|
||||||
|
id = self.uuid()
|
||||||
|
query = "INSERT OR IGNORE INTO recipes (recipe_id, name) VALUES (?, LOWER(?));"
|
||||||
|
self.cursor.execute(query, (id, name,))
|
||||||
|
self.conn.commit()
|
||||||
|
return id
|
||||||
|
|
||||||
|
def table (self) :
|
||||||
|
query = "SELECT * FROM recipes;"
|
||||||
|
res = self.cursor.execute(query, ())
|
||||||
|
for row in self.cursor.fetchall() :
|
||||||
|
print(row)
|
||||||
|
|
||||||
|
if __name__ == '__main__' :
|
||||||
|
con = sqlite3.connect('developers.sqlite')
|
||||||
|
r = Recipe(con)
|
||||||
|
r.table()
|
|
@ -0,0 +1,52 @@
|
||||||
|
import sqlite3
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
class Supply :
|
||||||
|
def __init__ (self, conn) :
|
||||||
|
self.conn = conn
|
||||||
|
self.cursor = conn.cursor()
|
||||||
|
|
||||||
|
def uuid (self) :
|
||||||
|
return str(uuid4())
|
||||||
|
|
||||||
|
def get (self, name) :
|
||||||
|
query = "SELECT supply_id FROM supply WHERE (url = ?) LIMIT 1;"
|
||||||
|
res = self.cursor.execute(query, (name,))
|
||||||
|
return self.cursor.fetchone()
|
||||||
|
|
||||||
|
def exists (self, name) :
|
||||||
|
supply = self.get(name)
|
||||||
|
if supply is not None:
|
||||||
|
return supply[0]
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def create (self, chemical_id, url, g, ml, price) :
|
||||||
|
id = self.uuid()
|
||||||
|
query = "INSERT OR IGNORE INTO supply (supply_id,chemical_id,url,grams,milliliters,price) VALUES (?,?,?,?,?,?);"
|
||||||
|
self.cursor.execute(query, (id,chemical_id,url,g,ml,price,))
|
||||||
|
self.conn.commit()
|
||||||
|
return id
|
||||||
|
|
||||||
|
def identify (self, chemical_id, url, g, ml, price) :
|
||||||
|
supply_id = self.exists(url)
|
||||||
|
if supply_id == '' :
|
||||||
|
supply_id = self.create(chemical_id, url, g, ml, price)
|
||||||
|
return supply_id
|
||||||
|
|
||||||
|
def table (self) :
|
||||||
|
query = "SELECT * FROM supply;"
|
||||||
|
res = self.cursor.execute(query, ())
|
||||||
|
for row in self.cursor.fetchall() :
|
||||||
|
print(row)
|
||||||
|
|
||||||
|
def display (self, supply_id, chemical) :
|
||||||
|
query="SELECT round((price/100.0)/grams, 2) as ppg FROM supply WHERE (supply_id = ?) ORDER BY ppg ASC;"
|
||||||
|
res = self.cursor.execute(query, (supply_id,))
|
||||||
|
data = self.cursor.fetchone()
|
||||||
|
ratio = data[0]
|
||||||
|
print(f'{chemical}: {ratio} $/g')
|
||||||
|
|
||||||
|
if __name__ == '__main__' :
|
||||||
|
con = sqlite3.connect('developers.sqlite')
|
||||||
|
s = Supply(con)
|
||||||
|
s.table()
|
Loading…
Reference in New Issue