From 89174a2838e703fdd86a8ccf9dce2a97a6d21f7e Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Thu, 27 Dec 2018 17:26:37 -0700 Subject: [PATCH] Add fermentable table --- src/synthale/recipes.py | 40 +++++++++++++++++++++++++++++++++++++++- tests/test_recipes.py | 22 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/synthale/recipes.py b/src/synthale/recipes.py index 2de7b7a..124055b 100644 --- a/src/synthale/recipes.py +++ b/src/synthale/recipes.py @@ -12,7 +12,11 @@ from synthale import markdown, convert class MarkdownRecipe: """A recipe in markdown form.""" - def __init__(self, recipe, vol_unit='gallons', hop_unit='ounces'): + def __init__(self, + recipe, + vol_unit='gallons', + hop_unit='ounces', + fermentable_unit='pounds'): """Create a MarkdownRecipe object. `recipe` is a recipe object from the pybeerxml package. @@ -23,10 +27,15 @@ class MarkdownRecipe: `hop_unit` specifies the unit for hop amounts. Can be one of 'ounces', 'pounds', 'grams', or 'kilograms'. If specified unit is not matched, default is 'ounces'. + + `fermentable_unit` specifies the unit for fermentable amounts. Can be + one of 'ounces', 'pounds', 'grams', or 'kilograms'. If specified unit + is not matched, default is 'pounds'. """ self.recipe = recipe self.vol_unit = vol_unit self.hop_unit = hop_unit + self.fermentable_unit = fermentable_unit @property def filename(self): @@ -54,6 +63,8 @@ class MarkdownRecipe: '', self.details, '', + self.fermentables, + '', self.hops, '', )) @@ -109,6 +120,33 @@ class MarkdownRecipe: self.recipe.abv) )) + @property + def fermentables(self): + """Return markdown to represent the recipe's fermentables.""" + headers = ('Name', 'Type', 'Color', 'Amount') + rows = [] + for fermentable in self.recipe.fermentables: + if self.fermentable_unit == 'ounces': + amt = convert.ounces(fermentable.amount, '.1f') + elif self.fermentable_unit == 'grams': + amt = convert.grams(fermentable.amount, '.1f') + elif self.fermentable_unit == 'kilograms': + amt = convert.kilograms(fermentable.amount, '.2f') + else: + amt = convert.pounds(fermentable.amount, '.2f') + rows.append(( + fermentable.name, + fermentable.type, + '{:.1f} °L'.format(fermentable.color), + amt + )) + return ( + '{}\n{}'.format( + markdown.setext_heading('Fermentables', level=2), + markdown.table(headers, rows) + ) + ) + @property def hops(self): """Return markdown to represent the recipe's hops.""" diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 098ac62..2973ba2 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -85,6 +85,28 @@ def test_recipe_details(md_weizen): assert '**Batch size**: 20.8 L' in md_weizen.details +def test_recipe_fermentables(md_weizen): + """Test valid fermentable table is generated.""" + md_weizen.fermentable_unit = 'pounds' + assert md_weizen.fermentables == ( + 'Fermentables\n' + '------------\n' + '| Name | Type | Color | Amount |\n' + '| --------------- | --------- | ------ | ------- |\n' + '| Pilsner (DE) | Base Malt | 1.0 °L | 5.00 lb |\n' + '| Wheat Malt (DE) | Adjunct | 2.0 °L | 5.00 lb |' + ) + + md_weizen.fermentable_unit = 'ounces' + assert '| 80.0 oz |' in md_weizen.fermentables + + md_weizen.fermentable_unit = 'grams' + assert '| 2268.0 g |' in md_weizen.fermentables + + md_weizen.fermentable_unit = 'kilograms' + assert '| 2.27 kg |' in md_weizen.fermentables + + def test_recipe_hops(md_weizen): """Test valid hop table is generated.""" md_weizen.hop_unit = 'ounces'