From 570ede627f7b4c8fdc5778be1de2bca73398d723 Mon Sep 17 00:00:00 2001 From: Chris Giacofei Date: Wed, 26 Jun 2024 11:47:04 -0400 Subject: [PATCH] Some more handy helper functions. --- beer/extras.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/beer/extras.py b/beer/extras.py index 1d120c0..64ccfb0 100644 --- a/beer/extras.py +++ b/beer/extras.py @@ -9,6 +9,7 @@ RECIPE_URL = 'https://api.brewfather.app/v2/recipes' BATCH_URL = 'https://api.brewfather.app/v2/batches' PULL_LIMIT = 50 + BREWFATHER_CONVERT_LOOKUP = { # local_name: brewfather_name 'all': { 'name': 'name', @@ -87,11 +88,76 @@ def get_batches(api_user, api_key, batch=''): return data + def sg_plato(sg): """Convert specific gravtiy to °P.""" sg = float(sg) return (-1*616.868) + (1111.14*sg) - (630.272*sg**2) + (135.997*sg**3) + def plato_sg(plato): """Convert °P to specific gravtiy.""" 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']