From 790cf5477368b8d84d62dbd4cf23de33011f9ae5 Mon Sep 17 00:00:00 2001 From: Chris Gilmer Date: Wed, 12 Oct 2016 10:08:35 -0700 Subject: [PATCH] Add methods to test individual style matches --- brew/styles.py | 60 ++++++++++++++++++++++-- docs/source/tutorial/matching_styles.rst | 45 ++++++++++++++++++ tests/test_styles.py | 20 ++++++++ 3 files changed, 120 insertions(+), 5 deletions(-) diff --git a/brew/styles.py b/brew/styles.py index 1e9986d..5ca73ed 100644 --- a/brew/styles.py +++ b/brew/styles.py @@ -91,6 +91,56 @@ def __eq__(self, other): def __ne__(self, other): return not self.__eq__(other) + def og_matches(self, og): + """ + Determine if og matches the style + + :param float og: Original Gravity + :return: True if matches style, otherwise False + :rtyle: bool + """ + return (self.og[0] <= og <= self.og[1]) + + def fg_matches(self, fg): + """ + Determine if fg matches the style + + :param float fg: Final Gravity + :return: True if matches style, otherwise False + :rtyle: bool + """ + return (self.fg[0] <= fg <= self.fg[1]) + + def abv_matches(self, abv): + """ + Determine if abv matches the style + + :param float abv: Alcohol by Volume + :return: True if matches style, otherwise False + :rtyle: bool + """ + return (self.abv[0] <= abv <= self.abv[1]) + + def ibu_matches(self, ibu): + """ + Determine if ibu matches the style + + :param float ibu: IBU + :return: True if matches style, otherwise False + :rtyle: bool + """ + return (self.ibu[0] <= ibu <= self.ibu[1]) + + def color_matches(self, color): + """ + Determine if color matches the style + + :param float color: Color in SRM + :return: True if matches style, otherwise False + :rtyle: bool + """ + return (self.color[0] <= color <= self.color[1]) + def recipe_matches(self, recipe): """ Determine if a recipe matches the style @@ -104,11 +154,11 @@ def recipe_matches(self, recipe): recipe_abv = alcohol_by_volume_standard(recipe_og, recipe_fg) recipe_ibu = recipe.get_total_ibu() recipe_color = recipe.get_total_wort_color() - if (self.og[0] <= recipe_og <= self.og[1]) and \ - (self.fg[0] <= recipe_fg <= self.fg[1]) and \ - (self.abv[0] <= recipe_abv <= self.abv[1]) and \ - (self.ibu[0] <= recipe_ibu <= self.ibu[1]) and \ - (self.color[0] <= recipe_color <= self.color[1]): + if self.og_matches(recipe_og) and \ + self.fg_matches(recipe_fg) and \ + self.abv_matches(recipe_abv) and \ + self.ibu_matches(recipe_ibu) and \ + self.color_matches(recipe_color): return True return False diff --git a/docs/source/tutorial/matching_styles.rst b/docs/source/tutorial/matching_styles.rst index 837dfc1..3696bf5 100644 --- a/docs/source/tutorial/matching_styles.rst +++ b/docs/source/tutorial/matching_styles.rst @@ -52,6 +52,21 @@ In order to match the recipe we use a method on the class: >>> style.recipe_matches(recipe) False + >>> recipe_og = recipe.get_original_gravity() + >>> style.og_matches(recipe_og) + False + >>> recipe_fg = recipe.get_final_gravity() + >>> style._matches(recipe_fg) + False + >>> recipe_abv = alcohol_by_volume_standard(recipe_og, recipe_fg) + >>> style.abv_matches(recipe_abv) + False + >>> recipe_ibu = recipe.get_total_ibu() + >>> style.ibu_matches(recipe_ibu) + True + >>> recipe_color = recipe.get_total_wort_color() + >>> style.color_matches(recipe_color) + False Interestingly the recipe used in the examples does not match the BJCP style! The only feature that matches the style is the IBUs, but the remaining values @@ -104,6 +119,21 @@ crystal 20L has come down from 0.78 lbs to 0.51 lbs. Let's try this again. >>> style.recipe_matches(recipe) False + >>> recipe_og = recipe.get_original_gravity() + >>> style.og_matches(recipe_og) + True + >>> recipe_fg = recipe.get_final_gravity() + >>> style._matches(recipe_fg) + True + >>> recipe_abv = alcohol_by_volume_standard(recipe_og, recipe_fg) + >>> style.abv_matches(recipe_abv) + True + >>> recipe_ibu = recipe.get_total_ibu() + >>> style.ibu_matches(recipe_ibu) + True + >>> recipe_color = recipe.get_total_wort_color() + >>> style.color_matches(recipe_color) + False It turns out the recipe still doesn't match. Why? It appears that our color is off. @@ -145,6 +175,21 @@ recipe and check the style: >>> style.recipe_matches(recipe) True + >>> recipe_og = recipe.get_original_gravity() + >>> style.og_matches(recipe_og) + True + >>> recipe_fg = recipe.get_final_gravity() + >>> style._matches(recipe_fg) + True + >>> recipe_abv = alcohol_by_volume_standard(recipe_og, recipe_fg) + >>> style.abv_matches(recipe_abv) + True + >>> recipe_ibu = recipe.get_total_ibu() + >>> style.ibu_matches(recipe_ibu) + True + >>> recipe_color = recipe.get_total_wort_color() + >>> style.color_matches(recipe_color) + True Nice job, now your have a beer recipe that matches the style of an American Pale Ale. diff --git a/tests/test_styles.py b/tests/test_styles.py index ca43e6a..9cc6dd8 100644 --- a/tests/test_styles.py +++ b/tests/test_styles.py @@ -146,6 +146,26 @@ def test_ne_color(self): def test_ne_style_class(self): self.assertTrue(self.style != recipe) + def test_og_matches(self): + out = self.style.og_matches(1.050) + self.assertTrue(out) + + def test_fg_matches(self): + out = self.style.fg_matches(1.012) + self.assertTrue(out) + + def test_abv_matches(self): + out = self.style.abv_matches(0.050) + self.assertTrue(out) + + def test_ibu_matches(self): + out = self.style.ibu_matches(33.0) + self.assertTrue(out) + + def test_color_matches(self): + out = self.style.color_matches(7.5) + self.assertTrue(out) + def test_recipe_matches(self): pale_add = GrainAddition(pale, weight=8.69)