recipemd.data module¶
Defines the RecipeMD data structures, provides parser, serializer and recipe scaling functions.
- class recipemd.data.RecipeParser[source]¶
Bases:
objectParses strings to a
RecipeorAmount.The markdown format is described in the RecipeMD Specification.
- parse(src)[source]¶
Parses a markdown string into a
Recipe.>>> recipe_parser = RecipeParser() >>> recipe = recipe_parser.parse(''' ... # Guacamole ... --- ... - *1* avocado ... - *.5 teaspoon* salt ... - *1 1/2 pinches* red pepper flakes ... - lemon juice ... --- ... Remove flesh from avocado and roughly mash with fork. Season to taste with salt pepper and lemon juice. ... ''') >>> recipe.title 'Guacamole' >>> recipe.ingredients[0].name 'avocado'
- Raises:
RuntimeException – If src is not a valid RecipeMD recipe.
- Parameters:
src (
str)- Return type:
- static parse_amount(amount_str)[source]¶
Parses an amount string to an
Amount.>>> RecipeParser.parse_amount('3.5 l') Amount(factor=Decimal('3.5'), unit='l')
Will recognize different number formats:
>>> RecipeParser.parse_amount('3 1/2 l') Amount(factor=Decimal('3.5'), unit='l') >>> RecipeParser.parse_amount('3 ½ l') Amount(factor=Decimal('3.5'), unit='l') >>> RecipeParser.parse_amount('3,5 l') Amount(factor=Decimal('3.5'), unit='l')
- Parameters:
amount_str (
str)- Return type:
Optional[Amount]
- recipemd.data.multiply_recipe(recipe, multiplier)[source]¶
Multiplies a recipe by the given multiplier.
Creates a new recipe where the factor of yield and ingredient is changed according to the multiplier.
>>> recipe = Recipe( ... ingredients=[ ... Ingredient(name='Eggs', amount=Amount(factor=Decimal('5'), unit=None), link=None), ... Ingredient(name='Butter', amount=Amount(factor=Decimal('200'), unit='g'), link=None), ... ] ... ) >>> multiplied_recipe = multiply_recipe(recipe, 3) >>> multiplied_recipe.ingredients[0] Ingredient(name='Eggs', amount=Amount(factor=Decimal('15'), unit=None), link=None) >>> multiplied_recipe.ingredients[1] Ingredient(name='Butter', amount=Amount(factor=Decimal('600'), unit='g'), link=None)
- recipemd.data.get_recipe_with_yield(recipe, required_yield)[source]¶
Scale the given recipe to a required yield.
Creates a new recipe, which has the yield given by required_yield. A recipe can only be scaled if a yield with a matching unit is present.
- class recipemd.data.Recipe(ingredients=<factory>, ingredient_groups=<factory>, title=None, description=None, yields=<factory>, tags=<factory>, instructions=None)[source]¶
Bases:
IngredientListRepresents a recipe.
- Parameters:
ingredients (
List[Ingredient])ingredient_groups (
List[IngredientGroup])title (
Optional[str])description (
Optional[str])yields (
List[Amount])tags (
List[str])instructions (
Optional[str])
-
title:
Optional[str] = None¶
-
description:
Optional[str] = None¶
-
tags:
List[str]¶
-
instructions:
Optional[str] = None¶
- class recipemd.data.Ingredient(name, amount=None, link=None)[source]¶
Bases:
objectRepresents an ingredient with name and optional amount and link.
- Parameters:
name (
str)amount (
Optional[Amount])link (
Optional[str])
-
name:
str¶
-
link:
Optional[str] = None¶
- class recipemd.data.IngredientGroup(ingredients=<factory>, ingredient_groups=<factory>, title='')[source]¶
Bases:
IngredientListAn ingredient group is a list of ingredients and ingredient groups with a title.
- Parameters:
ingredients (
List[Ingredient])ingredient_groups (
List[IngredientGroup])title (
str)
-
title:
str= ''¶
- class recipemd.data.Amount(factor, unit=None)[source]¶
Bases:
objectRepresents an amount, which is a factor with an associated unit.
- Parameters:
factor (
Decimal)unit (
Optional[str])
-
factor:
Decimal¶
-
unit:
Optional[str] = None¶
- class recipemd.data.IngredientList(ingredients=<factory>, ingredient_groups=<factory>)[source]¶
Bases:
objectRepresents a list of ingredients.
This is used as a base class for
RecipeandIngredientGroup, allowing common algorithm implementations for both.- Parameters:
ingredients (
List[Ingredient])ingredient_groups (
List[IngredientGroup])
-
ingredients:
List[Ingredient]¶
-
ingredient_groups:
List[IngredientGroup]¶
- property leaf_ingredients: Generator[Ingredient, None, None]¶
- property all_ingredients: Generator[Ingredient | IngredientGroup, None, None]¶
Edit on GitHub