Some more handy helper functions.

This commit is contained in:
Chris Giacofei 2024-06-26 11:47:04 -04:00
parent 07c881bdfe
commit 570ede627f

View File

@ -9,6 +9,7 @@ RECIPE_URL = 'https://api.brewfather.app/v2/recipes'
BATCH_URL = 'https://api.brewfather.app/v2/batches' BATCH_URL = 'https://api.brewfather.app/v2/batches'
PULL_LIMIT = 50 PULL_LIMIT = 50
BREWFATHER_CONVERT_LOOKUP = { # local_name: brewfather_name BREWFATHER_CONVERT_LOOKUP = { # local_name: brewfather_name
'all': { 'all': {
'name': 'name', 'name': 'name',
@ -87,11 +88,76 @@ def get_batches(api_user, api_key, batch=''):
return data return data
def sg_plato(sg): def sg_plato(sg):
"""Convert specific gravtiy to °P.""" """Convert specific gravtiy to °P."""
sg = float(sg) sg = float(sg)
return (-1*616.868) + (1111.14*sg) - (630.272*sg**2) + (135.997*sg**3) return (-1*616.868) + (1111.14*sg) - (630.272*sg**2) + (135.997*sg**3)
def plato_sg(plato): def plato_sg(plato):
"""Convert °P to specific gravtiy.""" """Convert °P to specific gravtiy."""
return 1 + (plato / (258.6 - (plato/258.2) * 227.1)) return 1 + (plato / (258.6 - (plato/258.2) * 227.1))
def kg_extract(v, sg):
"""Calculate kg of extract based on volume and SG."""
return float(v) * float(sg) * sg_plato(sg)/100
def convert(value, start_unit, end_unit):
"""Convert units."""
family_dict = unit_family(start_unit)
if family_dict:
start = [val for key, val in family_dict.items()
if start_unit in key.split(',')][0]
end = [val for key, val in family_dict.items()
if end_unit in key.split(',')][0]
return float(value) * end / start
def unit_family(unit_name):
"""Find unit family base on unit name."""
unit_lookup = [
{'name': 'length',
'units': {
'm,meter,meters': 1.0,
'cm,centimeter,centimeters': 100,
'mm,millimeter,millimeters': 1000,
'in,inch,inches': 39.3701,
'yd,yard,yards': 1.09361,
'ft,foot,feet': 3.28084,
'mi,mile,miles': 0.000621371,
'km,killometer,killometers': .001,
}},
{'name': 'mass',
'units': {
'kg,kilogram,kilograms': 1.0,
'g,gram,grams': 1000,
'lb,pound,pounds': 2.20462,
'oz,ounce,ounces': 35.274,
'st,stone': 0.157473,
}},
{'name': 'volume',
'units': {
'l,liter,liters': 1.0,
'ml,milliliter,milliliters': 1000,
'floz,fluid ounce,fluid ounces': 33.814,
'cup,cups': 4.22675,
'qt,quart,quarts': 1.05669,
'gal,gallon,gallons': 0.264172,
'ft^3,cubic foot,cubic feet': 0.0353147,
'pt,pint,pints': 4.22675/2,
'tsp,teaspoon,teaspoons': 202.884,
'tbsp,tablespoon,tablespoons': 202.884/3,
}},
]
for family in unit_lookup:
if [key for key in family['units'] if unit_name in key.split(',')]:
return family['units']