From 009a556c32a84a3045d9b08cc6aa02507903e00b Mon Sep 17 00:00:00 2001 From: Emi Vasilek Date: Mon, 6 Nov 2023 23:38:52 +0100 Subject: [PATCH] small cleanup --- recipes.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/recipes.py b/recipes.py index e2af7a0..8dfe898 100644 --- a/recipes.py +++ b/recipes.py @@ -101,7 +101,7 @@ class Units: for unit in self.units: if unit["name"] == name or "aliases" in unit and name in unit["aliases"]: return unit - raise RuntimeError(f"unit {name} not found") + return None class Ingredient(Element): @@ -133,7 +133,7 @@ class Ingredients: for ing in self.ingredients: if ing["name"] == name or "aliases" in ing and name in ing["aliases"]: return ing - raise RuntimeError(f"ingredient {name} not found") + return None class PriceDBs: @@ -154,28 +154,28 @@ class PriceDB(Element): self["amount"] = 1.0 if "unit" in dct: - try: - self["unit"] = self.ctx.units.get(dct["unit"]) - except RuntimeError as e: - self.ctx.issues.append(str(e)) + unitstr = dct["unit"] + self["unit"] = self.ctx.units.get(unitstr) + if self["unit"] == None: + self.ctx.issues.append(f"unknown unit {unitstr}") else: self["unit"] = None + class IngredientInstance(Element): def load(self, dct: Dict[str, Any]) -> None: - try: - self["ingredient"] = self.ctx.ingredients.get(dct["name"]) - except RuntimeError as e: - self.ctx.issues.append(str(e)) + self["ingredient"] = self.ctx.ingredients.get(dct["name"]) + if self["ingredient"] == None: + self.ctx.issues.append(f"unknown ingredient {dct['name']}") if "amount" not in dct: self["amount"] = 1.0 if "unit" in dct: - try: - self["unit"] = self.ctx.units.get(dct["unit"]) - except RuntimeError as e: - self.ctx.issues.append(str(e)) + unitstr = dct["unit"] + self["unit"] = self.ctx.units.get(unitstr) + if self["unit"] == None: + self.ctx.issues.append("unknown unit {unitstr}") else: self["unit"] = None